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.
Post Filteredit
So far, we have a way to filter both the search results and aggregations (a
non-scoring filter
query), as well as filtering individual portions of the aggregation
(filter
bucket).
You may be thinking to yourself, "hmm…is there a way to filter just the search
results but not the aggregation?" The answer is to use a post_filter
.
This is a top-level search-request element that accepts a filter. The filter is
applied after the query has executed (hence the post
moniker: it runs
post query execution). Because it operates after the query has executed,
it does not affect the query scope—and thus does not affect the aggregations
either.
We can use this behavior to apply additional filters to our search criteria that don’t affect things like categorical facets in your UI. Let’s design another search page for our car dealer. This page will allow the user to search for a car and filter by color. Color choices are populated via an aggregation:
GET /cars/transactions/_search { "size" : 0, "query": { "match": { "make": "ford" } }, "post_filter": { "term" : { "color" : "green" } }, "aggs" : { "all_colors": { "terms" : { "field" : "color" } } } }
The query
portion is finding all ford
cars. We are then building a list of
colors with a terms
aggregation. Because aggregations operate in the query
scope, the list of colors will correspond with the colors that Ford cars are
painted.
Finally, the post_filter
will filter the search results to show only green
ford
cars. This happens after the query is executed, so the aggregations
are unaffected.
This is often important for coherent UIs. Imagine that a user clicks a category in
your UI (for example, green). The expectation is that the search results are filtered,
but not the UI options. If you applied a Boolean filter
query, the UI would
instantly transform to show only green
as an option—not what the user wants!
Performance consideration
Use a post_filter
only if you need to differentially filter search results
and aggregations. Sometimes people will use post_filter
for regular searches.
Don’t do this! The nature of the post_filter
means it runs after the query,
so any performance benefit of filtering (such as caches) is lost completely.
The post_filter
should be used only in combination with aggregations, and only
when you need differential filtering.
- 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