has_child query

If you want to get products based on some condition on the features, you can use has_child queries.

The outline of a has_child query is as follows:

GET <index>/_search
{
"query": {
"has_child": {
"type": <the child type against which to run the following query>,
"query": {
<any elasticsearch query like term, bool, query_string to be run against the child type>
}
}
}
}

As you can see in the preceding code, we mainly need to specify only two things:

  1. The type of the child against which to execute the query
  2. The actual query to be run against the child to get their parents

Let's learn about this through an example. We want to get all of the products where processor_series is Core i7 from the example dataset that we loaded:

GET amazon_products_with_features/_search
{
"query": {
"has_child": {
"type": "feature", 1
"query": {
"bool": { 2
"must": [
{
"term": { 3
"feature_key": {
"value": "processor_series"
}
}
},
{
"term": { 4
"feature_value": {
"value": "Core i7"
}
}
}
]
}
}
}
}
}

Please note the following points about the query:

  • The has_child query is used to query based on the child type feature.
  • The actual query to be used on the child type is specified under the query element. We can use any query supported by Elasticsearch under this query element. Whichever query is used will be run against the child type that we specified. We are using a bool query because we want to filter by all features where—feature_key = processor_series AND feature_value = Core i7.
  • We have used a bool query, as explained in the previous point. The first of the conditions is included as a term query here under the must clause. This term query checks that we filter for feature_key = processor_series.
  • This is the second condition under the must clause, in which the term query checks that we filter for feature_value = Core i5.

The result of this has_child query is that we get back all the products that satisfy the query mentioned under the has_child element executed against all the features. The response should look like the following:

{
"took" : 1,
"timed_out" : false,
"_shards" : {
"total" : 1,
"successful" : 1,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : {
"value" : 1,
"relation" : "eq"
},
"max_score" : 1.0,
"hits" : [
{
"_index" : "amazon_products_with_features",
"_type" : "doc",
"_id" : "c0002",
"_score" : 1.0,
"_source" : {
"description" : "The Acer G9-593-77WF Predator 15 Notebook comes with a 15.6 inch IPS display that makes each image and video appear sharp and crisp. The laptop has Intel Core i7-6700HQ 2.60 GHz processor with NVIDIA Geforce GTX 1070 graphics and 16 GB DDR4 SDRAM that gives lag free experience. The laptop comes with 1 TB HDD along with 256 GB SSD which makes all essential data and entertainment files handy.",
"price" : "1899.99",
"id" : "c0002",
"title" : "Acer Predator 15 G9-593-77WF Notebook",
"product_or_feature" : "product",
"manufacturer" : "Acer"
}
}
]
}
}

Next, let's look at another type of query that can be utilized when we have used the join type in Elasticsearch.

..................Content has been hidden....................

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