WARNING: The 2.x versions of Elasticsearch have passed their EOL dates. If you are running a 2.x version, we strongly advise you to upgrade.
This documentation is no longer maintained and may be removed. For the latest information, see the current Elasticsearch documentation.
Finding Parents by Their Childrenedit
The has_child
query and filter can be used to find parent documents based on
the contents of their children. For instance, we could find all branches that
have employees born after 1980 with a query like this:
GET /company/branch/_search { "query": { "has_child": { "type": "employee", "query": { "range": { "dob": { "gte": "1980-01-01" } } } } } }
Like the nested
query, the has_child
query could
match several child documents, each with a different relevance
score. How these scores are reduced to a single score for the parent document
depends on the score_mode
parameter. The default setting is none
, which
ignores the child scores and assigns a score of 1.0
to the parents, but it
also accepts avg
, min
, max
, and sum
.
The following query will return both london
and liverpool
, but london
will get a better score because Alice Smith
is a better match than
Barry Smith
:
GET /company/branch/_search { "query": { "has_child": { "type": "employee", "score_mode": "max", "query": { "match": { "name": "Alice Smith" } } } } }
The default score_mode
of none
is significantly faster than the other
modes because Elasticsearch doesn’t need to calculate the score for each child
document. Set it to avg
, min
, max
, or sum
only if you care about the
score.
min_children and max_childrenedit
The has_child
query and filter both accept the min_children
and
max_children
parameters, which will return the parent document only if the
number of matching children is within the specified range.
This query will match only branches that have at least two employees:
GET /company/branch/_search { "query": { "has_child": { "type": "employee", "min_children": 2, "query": { "match_all": {} } } } }
The performance of a has_child
query or filter with the min_children
or
max_children
parameters is much the same as a has_child
query with scoring
enabled.
- Elasticsearch - The Definitive Guide:
- Foreword
- Preface
- Getting Started
- You Know, for Search…
- Installing and Running Elasticsearch
- Talking to Elasticsearch
- Document Oriented
- Finding Your Feet
- Indexing Employee Documents
- Retrieving a Document
- Search Lite
- Search with Query DSL
- More-Complicated Searches
- Full-Text Search
- Phrase Search
- Highlighting Our Searches
- Analytics
- Tutorial Conclusion
- Distributed Nature
- Next Steps
- Life Inside a Cluster
- Data In, Data Out
- What Is a Document?
- Document Metadata
- Indexing a Document
- Retrieving a Document
- Checking Whether a Document Exists
- Updating a Whole Document
- Creating a New Document
- Deleting a Document
- Dealing with Conflicts
- Optimistic Concurrency Control
- Partial Updates to Documents
- Retrieving Multiple Documents
- Cheaper in Bulk
- Distributed Document Store
- Searching—The Basic Tools
- Mapping and Analysis
- Full-Body Search
- Sorting and Relevance
- Distributed Search Execution
- Index Management
- Inside a Shard
- You Know, for Search…
- Search in Depth
- Structured Search
- Full-Text Search
- Multifield Search
- Proximity Matching
- Partial Matching
- Controlling Relevance
- Theory Behind Relevance Scoring
- Lucene’s Practical Scoring Function
- Query-Time Boosting
- Manipulating Relevance with Query Structure
- Not Quite Not
- Ignoring TF/IDF
- function_score Query
- Boosting by Popularity
- Boosting Filtered Subsets
- Random Scoring
- The Closer, The Better
- Understanding the price Clause
- Scoring with Scripts
- Pluggable Similarity Algorithms
- Changing Similarities
- Relevance Tuning Is the Last 10%
- Dealing with Human Language
- Aggregations
- Geolocation
- Modeling Your Data
- Administration, Monitoring, and Deployment