Parent-child relationships are mainly used to deal with one-to-many relationships. 6. X starts with es abolishing the mode of multiple types in an index. In 6.x, an index can only have one type, and 7.x abolishes type. Therefore, the _parent tag is no longer used.

version

6.8

ElasticSearch website

Set up the index

PUT my_index { "mappings": { "_doc": { "properties": { "test": { "type": "join", "relations": { "question": "Answer"}}}}}} / / multiple child PUT my_index {" the mappings ": {" _doc" : {" properties ": {" test" : {" type ": "Join", "relations" : {" question: "[" answer", "comment"]}}}}}} / / multi-tiered PUT my_index {" the mappings ": {" _doc" : { "properties": { "test": { "type": "join", "relations": { "question": ["answer", "comment"], "answer": "vote" } } } } } }Copy the code

Update the data

Add a single Parent data

PUT my_index/_doc/1
{
  "text": "This is a question",
  "test": {
    "name": "question" 
  }
}
Copy the code

Add Parent data in batches

POST _bulk
{"index":{"_index":"my_index","_type":"_doc","_id":"bulk-1"}}
{"text":"bulk questions","test":{"name":"question"}}
Copy the code

Add a single child data

Routing is used when adding to the specified parent

PUT my_index/_doc/3? routing=1 { "text": "This is an answer", "test": { "name": "answer", "parent": "1" } }Copy the code

Add multiple Child data in batches

POST _bulk
{"index":{"_index":"my_index","_type":"_doc","routing":1}}
{"name":"child","test":{"name":"answer","parent":1}}
Copy the code

The query

Query the child

Query the child using has_parent

GET my_index/_search
{
  "query": {
    "has_parent": {
      "parent_type": "question",
      "query": {
        "match": {
          "text": "this is"
        }
      }
    }
  }
}
Copy the code

Run parent_id to query child

GET /my_index/_search
{
  "query": {
    "parent_id": {
      "type": "answer",
      "id": "2"
    }
  }
}
Copy the code

Query the parent

Query the parent using has_child

GET my_index/_search { "query": { "has_child": { "type": "answer", "min_children": 1, // Optional, query parent "max_children": 10,// Optional, query parent "query": {"match": {"text": 1, // Optional, query parent "max_children": 10,// Optional, query parent "match": {"text": "this is" } } } } }Copy the code