Get data on several elements from both parent and child in one elasticsearch request

Is it possible to get field data from parent and child in one elasticsearch request? Essentially, I am trying to capture data for multiple parent fields and multiple child fields in the same iteration with filtering. I tried various methods of binding information to a single request, but could not figure out how to do this. Here is what my comparison looks like: -

Parent:

_id_parent : values {1} _source: {_date (20160316), _time (20160316010000), _id_source (test), _year (2016), _month (1)} 

child:

 _id_child : values {1} _source: {_id_child (1), _id_parent (1), _child_question (q1), _child_answer (This needs to be done.)} 

Expected Result (something similar below):

  (PARENT) "hits" : { "total" : 1, "max_score" : 1.0, "hits" : [ { "_index" : "index", "_type" : "parent", "_id" : "1", "_score" : 1.0, "_source":{_id_parent":"1","_id_source":"test","_date":"20160316","_time":"20160316010000","_year":2016,"_month":"1"} } ] } (CHILD) "hits" : { "total" : 1, "max_score" : 1.0, "hits" : [ { "_index" : "index", "_type" : "child", "_id" : "1", "_score" : 1.0, "_source":{"_id_child":"1", "_child_question":"q1","_child_answer":"This needs to be done."} } ] } 

References:

http://rore.im/posts/elasticsearch-joins/

https://github.com/elastic/elasticsearch/issues/761

https://www.elastic.co/guide/en/elasticsearch/guide/current/children-agg.html

  curl -XGET "$ELASTICSEARCH_ENDPOINT/index/parent/_search?pretty=true" -d " { "query": { "match": { "_id_parent": "1" } }, "size" : 10, "aggs": { "_id_parent": { "terms": { "field":"_id_parent", "field":"_id_source", "field":"_date", "field":"_time", "field":"_year", "field":"_month", }, "aggs": { "child": { "children": { "type": "child" }, "aggs": { "child": { "terms": { "field": "child._id_child", "field": "child._child_question", "field": "child._child_answer", } } } } } } } }" 
+5
source share
3 answers

I made some assumptions ... let me know if they are correct

  • You want the document from the parent map to have the document id = 1
  • You also need all child documents that have parent_id = 1
  • You pass this parent_id from your code to elasticsearch.
  • You do not request elasticsearch to filter through multiple parent documents. Before you click elasticsearch, you already know that you want to get the parent document with id = 1

If so, you can create two separate queries

The first request is "get parent document with id = 1"
Second request: "get all child documents with parent_id = 1"

And you can use the Elasticsearch multi-user search API to send both of these requests to elasticsearch in one network call

MultiSearch API

+2
source

It sounds like work for domestic hits . This function allows you to get hits that have a has_child or has_parent .

In your case, you will make a request against the parent with a trivial has_child (i.e. match_all) or vice versa, for example, something like

 { "query" : { "has_child" : { "type" : "child", "query" : { "match_all": {} }, "inner_hits" : {} } } } 
+7
source

You can use the example below to search by parent and child index. Hope this helps you.

  GET indexname/_search { "query": { "bool": { "must": [ { "bool": { "must": [ { "term": { "parentField": { "value": "valuetosearch" } } } ] } }, { "has_child": { "type": "childindex", "query": { "range" : { "childindexField" : { "lte": "value" } } } } } ] } } } 
0
source

Source: https://habr.com/ru/post/1245349/


All Articles