how to index questions and answers in elaticsearch how to index questions and answers in elaticsearch elasticsearch elasticsearch

how to index questions and answers in elaticsearch


Parent-Child Relationship

Use the parent-child relationship. It is similar to the nested model, and allows association of one entity with another. You can associate one document type with another, in a one-to-many relationship.More information on here: https://www.elastic.co/guide/en/elasticsearch/guide/current/parent-child.html

Child documents can be added, changed, or deleted without affecting the parent nor other children. You can do pagination on the parent documents using the Scroll API.Child documents can be retrieved using the has_parent join.

The trade-off: you do not have to take care of duplicates and pagination problems, but parent-child queries can be 5 to 10 times slower than the equivalent nested query.

Your mapping can be like the following:

PUT /my-index{  "mappings": {    "question": {      "properties": {        "title": {          "type": "text"        },        "question": {          "type": "text"        },        "questionId": {          "type": "keyword"        }      }    },    "answer": {      "_parent": {        "type": "question"      },      "properties": {        "answer": {          "type": "text"        },        "answerId": {          "type": "keyword"        },        "questionId": {          "type": "keyword"        }      }    }  }}