In Elasticsearch, via nested and child documents, we can have complex data models. Elasticsearch, by default, returns only documents that match the searched type and not the nested/children one that matches the query.
The inner_hits
function is introduced in Elasticsearch 5.x to provide this functionality.
You will need an up-and-running Elasticsearch installation as used in the Downloading and installing Elasticsearch recipe in Chapter 2, Downloading and Setup.
To execute curl
via a command line, you need to install curl
for your operating system.
To correctly execute the following commands, you will need an index populated with the chapter_05/populate_query.sh
script available in the online code.
To return inner hits during a query, we will perform the following steps:
inner_hits
as follows:curl -XPOST 'http://127.0.0.1:9200/test-index/test- type/_search?pretty' -d '{ "query": { "has_child" : { "type" : "test-type2", "query" : { "term" : { "value" : "value1" } }, "inner_hits":{} } } } }'
{ "took" : 82, "timed_out" : false, "_shards" : { "total" : 5, "successful" : 5, "failed" : 0 }, "hits" : { "total" : 1, "max_score" : 1.0, "hits" : [ { "_index" : "test-index", "_type" : "test-type", "_id" : "1", "_score" : 1.0, "_source" : { "position" : 1, "parsedtext" : "Joe Testere nice guy", "name" : "Joe Tester", "uuid" : "11111", "price" : 4.0 }, "inner_hits" : { "test-type2" : { "hits" : { "total" : 1, "max_score" : 0.2876821, "hits" : [ { "_type" : "test-type2", "_id" : "1", "_score" : 0.2876821, "_routing" : "1", "_parent" : "1", "_source" : { "name" : "data1", "value" : "value1" } } ] } } } } ] } }
When executing nested/children queries, Elasticsearch executes a two-step query as follows:
Generally, the results of the nested/children query are not taken, because they require memory. Using the inner_hits
, the nested/children query intermediate hits are kept and returned to the user.
To control the inner_hits
returned documents, standard parameters for the search are available such as from
, size
, sort
, highlight
, _source
, explain
, scripted_fields
, docvalues_fields
, and version
.
There is also a special property name used to name inner_hits
, which allows the user to easily determine it in case of multiple inner_hits
returning sections.
3.15.34.154