简体   繁体   中英

how to search child documents with the same parent id in Elasticsearch on ver_6.2.4?

I read answers in how to search child documents with the same parent id in Elasticsearch? , but none of them can solve it on 6.2.4, can someone tell me how to do the same job on 6.2.4?

Starting in Elasticsearch 6.x, storing parent/child as separate types was replaced with a join datatype . This doesn't fundamentally change how you'd go about indexing and querying those documents, but does change the syntax a bit.

NOTE: This set of changes were made in preparation for removal of type entirely, which happens in 7.x

Initialize Mapping (using a template)

Instead of specifying separate types, we'll create a join field named branch_join for all documents. The relations configuration specifies the parent -> child relationship, with the key ( branch ) being the parent, and the value ( employee ) being the child.

NOTE: I'm using the index named organization for sake of example

PUT _template/org_template
{
  "index_patterns": ["organization"],
  "settings": {
    "number_of_shards": 2
  },
  "mappings": {
    "_doc": {
      "properties": {
        "branch_join": {
          "type": "join",
          "relations": {
            "branch": "employee"
          }
        }
      }
    }
  }
}

Index your documents

Indexing the parent documents is similar, but we need to specify which side of the relation each document is on by specifying branch in the branch_join field.

POST /organization/_doc/_bulk
{"index": {"_id": "london"}}
{"name": "London Westminster", "city": "London", "country": "UK", "branch_join": {"name":"branch"}}
{"index": {"_id": "liverpool"}}
{"name": "Liverpool Central", "city": "Liverpool", "country": "UK", "branch_join": {"name":"branch"}}
{"index": {"_id": "paris"}}
{"name": "Champs Élysées", "city": "Paris", "country": "France", "branch_join": {"name":"branch"}}

To index the children, we need to do two things: 1. We need to specify the branch_join field again, but not only do we specify which side of the join this is ( employee ), we also need to specify the _id of the parent documents to join on. 2. To ensure that children get indexed on the same shards as their parents, we'll set the routing parameter equal to the _id of the parent document.

POST /organization/_doc/_bulk
{"index": { "_id": 1, "routing": "london"}}
{"name": "Alice Smith", "dob": "1970-10-24", "hobby": "hiking", "branch_join": {"name":"employee", "parent": "london"}}
{"index": { "_id": 2, "routing": "london"}}
{"name": "Mark Thomas", "dob": "1982-05-16", "hobby": "diving", "branch_join": {"name":"employee", "parent": "london"}}
{"index": { "_id": 3, "routing": "liverpool"}}
{"name": "Barry Smith", "dob": "1979-04-01", "hobby": "hiking", "branch_join": {"name":"employee", "parent": "liverpool"}}
{"index": { "_id": 4, "routing": "paris"}}
{"name": "Adrien Grand", "dob": "1987-05-11", "hobby": "horses", "branch_join": {"name":"employee", "parent": "paris"}}

Query

The query is almost identical, except that type has been renamed parent_type to be more explicit:

POST /organization/_search
{
  "query": {
    "has_parent": {
      "parent_type": "branch", 
      "query": {
        "ids": {
          "values" : ["london"]
        }
      }
    }
  }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM