filter
- Filter queries can be used to reduce datasets to a particular date or date range, specific location, or other exact matches. Filter queries are automatically stored in the Elasticsearch cache. The next time the exact same filter query is run, the results will be pulled instantly from the cache. Unlike must
the score of the query will be ignored. Queries specified under the filter
element have no effect on scoring — scores are returned as 0
must
- Must is similar to the “and” operator used when making a Google search. Using must
tells Elasticsearch that document matches need to include all of the queries that fall under the must clause. If you have more than one query, then all of those queries need to match.must-not
- It is similar to the “not” operator used when making a Google search. It is the opposite of the must clause. Using must_not
tells Elasticsearch that document matches cannot include any of the queries that fall under the must_not clause.should
- It would be ideal for the matching documents to include all of the queries in the should clause, but they do not have to be included (not mandatory). The more should queries that a matched document has, the higher the resulting score for that document. By default the minimum number of should matches is set to 1, but here we changed the minimum value to 2.GET movies/_search
{
“query”: {
“bool”: {
“must”: {
“term”: { “text”: “comedy” }
},
“should”: [
{“term”: { “text”: “Academy Award” }},
{“term”: { “text”: “Golden Globe” }},
{“term”: { “text”: “People’s Choice” }}
],
**“minimum_should_match”: 2**
}
}
}
If there is a query that is commonly used, then writing that query with the filter clause is ideal because filtered queries are automatically stored in memory (cached
) for fast retrieval.
For some searches it makes sense to use scoring to rank the relevancy of matches. Scored searches aren’t compatible with caching though so keep that in mind. One way of handling this limitation is that you can create a filtered cache for the aspects of the query that don’t require scoring, and then run the scored portion of the query on the filtered subset of the database. With that, let’s dive into scoring.
GET customers/_search
{
“query”: {
“bool”: {
“filter”: [
{“term”: { “state”: “Texas” }},
{“range”: { “total_spend”: {“gte”: “200.00” }}}
],
}
}
}
Elasticsearch ascribes scores to matches that rank the matched documents by their relevance to the search parameters. All scores are represented by a floating number greater than zero, and the greater the score the better matched it is to the search query.
Filters
do not affect scoring.
GET /_cat/indices