Returning inner hits in results

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.

Getting ready

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.

How to do it...

To return inner hits during a query, we will perform the following steps:

  1. From the command line, we can execute a call adding 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":{}
        
                        }
                    }
                }
             }'
  2. The result returned by Elasticsearch, if everything works, should be as follows:
            { 
              "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" 
                              } 
                            } 
                          ] 
                        } 
                      } 
                    } 
                  } 
                ] 
              } 
            }  
    

How it works...

When executing nested/children queries, Elasticsearch executes a two-step query as follows:

  1. It executes the nested/children query and returns the IDs of the referred values.
  2. It executes the other part of the query filtering by the returned IDs of step 1.

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.

See also

  • The Executing a search recipe in this chapter for all the standard parameters in searches for controlling returned hits
  • The Using a has_child query, Using a top_children query, Using a has_parent query and Using a nested query recipes in Chapter 7, Relationships and Geo Queries is useful for queries that can be used for inner hits
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.15.34.154