Search code examples
joinelasticsearchmappingparentchildren

How join parent and child in elasticsearch


How join parent and child in elasticsearch? For example my mapping:

{  
   "street":{  
      "properties":{  
         "street_name":{  
            "type":"string"
         }
      }
   }
}

and

{  
   "address":{  
      "_parent":{  
         "type":"street"
      },
      "properties":{  
         "house_number":{  
            "type":"string"
         }
      }
   }
}

Data:

{"_type":"street","_id":"AUrLQH9ZcB6int_hskH3","_source":{"street_name":"Street"}}
{"_type":"address","_id":"AUrLQH_XcB6int_hskH4","_source":{"house_number":"10"}}

How i can get similar result in one query:

{  
   "_type":"address",
   "_id":"AUrLQH_XcB6int_hskH4",
   "house_number":"10",

   "street_name":"Street"

}

Of course, for a single element, i can join types in application, but what to do with large lists? Thanks!


Solution

  • Maybe something like this, don't think there is a way of grouping them nicely:

    GET /postal_codes/street/_search?search_type=count
    {
      "aggs": {
        "street": {
          "terms": {
            "field": "street_name"
          },
          "aggs": {
            "addresses": {
              "children": {
                "type": "address"
              },
              "aggs": {
                "number": {
                  "terms": {
                    "field": "address.house_number"
                  }
                }
              }
            }
          }
        }
      }
    }