Using the has_parent query/filter

In the previous recipes, we have seen the has_child query. ElasticSearch provides a query to search a child based on the has_parent parent query.

Getting ready

You need a working ElasticSearch cluster and the data populated with the populate script.

How to do it...

For executing the has_parent query/filter, we need to perform the following steps:

  1. We want to search the test-type2 children of which the test-type parents have a term joe in the parsedtext field. We can create this kind of query as follows:
    curl -XPOST 'http://127.0.0.1:9200/test-index/test-type2/_search' -d '{
        "query": {
                "has_parent" : {
                    "type" : "test-type",
                    "query" : {
                        "term" : {
                            "parsedtext" : "joe"
                        }
                    }
                }
            }
        }
    }'
  2. If scoring is not important, it's better to reformulate the query as a filter in the following way:
    curl -XPOST 'http://127.0.0.1:9200/test-index/test-type2/_search' -d '{
        "query": {
            "filtered": {
                "filter": {
                     "has_parent" : {
                         "type" : "test-type",
                         "query" : {
                            "term" : {
                                "parsedtext" : "joe"
                            }
                         }
                     }
                },
                "query": {
                    "match_all": {}
                }
            }
        }
    }'

How it works...

This kind of query works returning children that match a parent query.

Internally this subquery is executed on the parents and all the IDs of the matching parents are used to filter the children. A system must have enough memory to store parent IDs.

The parameters that are used to control this process are as follows:

  • type: This parameter defines the type of the parent.
  • query: This parameter defines the query that can be executed for selecting the parents. Every kind of query can be used.
  • score_type (default none, available values are none and score): By using the default configuration of none, ElasticSearch ignores the scores for the parent document reducing the memory usage and increasing performance. If it's set to score, the parent query score is aggregated into the children.

See also

  • Refer to Indexing a document in Chapter 4, Standard Operations
..................Content has been hidden....................

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