Preserve predicate scope in nested and geographic search.
- One nested scope
- Negation and the parent level
- Geography and relationships
Preserve predicate scope in nested and geographic search.
The course targets Elasticsearch 9.4.3. Exercise examples use the public products dataset; other requests illustrate separate teaching indices and may require setup or privileges unavailable in the trainer. Responses are shortened fragments; scores may vary. Check deployment and license requirements for advanced features.
One nested scope
seller=A and price<60 must be inside one nested query on offers. Two independent nested queries can match different offers. inner_hits reveals matching child objects; the root _source is not automatically reduced to them. score_mode controls child contributions, while none suits purely restrictive nested matching.
Check your understandingNegation and the parent level
must_not inside nested excludes a condition on an individual offer, but another offer can still qualify the product. To exclude every product having a banned seller, place that nested query in the root must_not. Negation placement changes the quantifier: “a qualifying offer exists” differs from “no forbidden offer exists”.
Check your understandingGeography and relationships
geo_point stores a location, geo_distance filters by radius, and geo_shape models spatial shapes. Check coordinate order: GeoJSON arrays use longitude, latitude. Geographic filtering does not automatically sort by distance. Parent-child queries are appropriate only when join cost and routing requirements fit the update model.
Both predicates apply to the same seller offer.
{
"query": {
"nested": {
"path": "offers",
"score_mode": "none",
"query": {
"bool": {
"filter": [
{
"term": {
"offers.seller": "A"
}
},
{
"range": {
"offers.price": {
"lt": 60
}
}
}
]
}
},
"inner_hits": {
"size": 3
}
}
}
}How to read this result
Both predicates apply to the same seller offer.
Catalog reasoning example
With offers=[{seller:A, price:100},{seller:B, price:50}], “A below 60” must yield no product. “No seller B exists” also rejects it, while “some offer is not from B” accepts it through A. This distinction explains why inner and root must_not are not interchangeable. Inspect inner_hits to identify the actual matching child.
Check your understandingCheck your understanding
Answer before expanding each explanation.
1. Why can two nested queries be wrong?
They may match different offers.
2. What does inner_hits show?
Child objects that satisfied the nested query.
3. How do you exclude any banned offer?
Put the forbidden nested predicate in root must_not.
4. What is GeoJSON coordinate order?
Longitude, then latitude.
5. Does radius filtering sort results?
No, distance sorting is separate.
Module case study: Query DSL
Find in-stock products with brand Sony or price below 100 and names matching headphones. Adding a filter unexpectedly widens results. Diagnose should requirements and explain each scoring contribution.
Assessment criteria
Put required text in must, constraints in filter, require one alternative and include a negative example satisfying neither brand nor price.
Key takeaways
- They may match different offers.
- Child objects that satisfied the nested query.
- Put the forbidden nested predicate in root must_not.
- Longitude, then latitude.
- No, distance sorting is separate.
Official reference
Ready to put this into practice?
Confirm reading to save your place. Reading and exercise progress are tracked separately in this browser using your guest cookie.