ElasticSearch Filter vs Query

ElasticSearch Filter vs Query Feature Image

ElasticSearch has two ways to limit the number of documents to return, depending on the context. This tutorial gives you overview of these ways which we call Query and Filter.

Related Posts:
Elasticsearch Overview
ElasticSearch – Structure of a Search Request/Response
ElasticSearch Full Text Queries – Basic

1. Query Context

In this context, the query clause answers the question:
“How well does this document match this query clause?”

>> We have 2 main requirements:
– whether or not the document matches
– how well the document matches, relative to other documents (that _score represents)

For example, this query:

will have the response like this:

Look at the _score for responsed items. We can see that the _score decreases in order.
Change the query to:

The response will be:

We recognise that the order is changed with _score.

2. Filter Context

In Filter context, a query clause answers the question:
“Does this document match this query clause?”

>> The response is just a simple Yes or No (without _score).

Frequently used filters will be cached automatically by Elasticsearch, to speed up performance. This context is mostly used for filtering structured data.

*Note: With Elasticsearch 5.6, the filtered query is replaced by the bool query.

For example:
– Is post_date from “2017-10-25”?
– Does tags contain “firebase”?

So with this query:

We have the response:

Notice that _score is constant.

3. Query & Filter Context

Now we mix 2 types of context in a Query Request:

– The must and two match clauses are used in Query context, which means that they are used to calculate _score for how well each document matches.
– The filter indicates Filter context in which term and range are used. They will filter out documents which do not match, but NOT affect the _score.

So, we can look at the response:

We can see that _score is calculated.
Now we change range of filter:

The response is:

The _score for “Angular 4 Firebase – CRUD Operations example” still does not change the value.

By grokonez | October 25, 2017.

Last updated on April 25, 2019.

Related Posts

Got Something To Say:

Your email address will not be published. Required fields are marked *