Is there performance difference between constant_score and bool query using filter? - elasticsearch

When it comes to the "performance difference", I read nothing reliable till now.
Based on its official docs, as to filter used in the bool query
The clause (query) must appear in matching documents. However unlike must the score of the query will be ignored. Filter clauses are executed in filter context, meaning that scoring is ignored and clauses are considered for caching.
As for constant score query
Filter queries do not calculate relevance scores. To speed up performance, Elasticsearch automatically caches frequently used filter queries.
Just a guess
Constant query will not calculate (TF-IDF or more advanced algs), while the bool query will do the calculation but return 0 (ignoring it); so the constant query is more performant.
Besides when it comes to a specified score, you have to use constant score query instead of bool query which only will return 0.
QAs I just read: Elasticsearch : constant_score query vs bool.filter query

NO, there is no performance difference since they are the same.
Based on again its offical doc discussing about filter context:
In a filter context, a query clause answers the question “Does this document match this query clause?” The answer is a simple Yes or No — no scores are calculated.
And
Filter context is in effect whenever a query clause is passed to a filter parameter, such as the filter or must_not parameters in the bool query, the filter parameter in the constant_score query, or the filter aggregation.

Related

ES bool and match when should be used?

I got that bool has different options which influences scoring. In the other hand, match has other options like lenient, minimum_should_match, operator.
What is the real use of these two? Can we have one to satisfy all of these things?
Is my understanding wrong? When should I use bool and match?
Bool is used to combine different queries. It provides below options
1. Must :
The clause (query) must appear in matching documents and will contribute to the score.
It works like "AND" for different queries
Filter:
The clause (query) must appear in matching documents. However unlike must the score of the query will be ignored
It works like "AND" but doesn't calculate score
Should :
The clause (query) should appear in the matching document.
It works like "OR" for multiple queries
Must_not:
The clause (query) must not appear in the matching documents. Clauses are executed in filter context meaning that scoring is ignored
Match Query
It is used for performing full text search
If you need a query to do full text search on a single field , you can use match query. If you need more than one query say a exact match on field(term query) and partial match on another field (match query). You will have to combine them using Bool clause.

Filter context for should in bool query

Elasticsearch documentation says
If the bool query is a filter context or has neither must or filter then at least one of the should queries must match a document for it to match the bool query.
So I can have a bool query with a should clause along with must or filter within a filter context and then still get an exact match. How to do this combination?
I would also be interested in knowing how to do this with Jest client?
In that case you need to add minimum_should_match: 1 if you want that at least one of your should clauses match.

Filter vs query behaviour in constant_score

I am confused about the difference between behaviour of filter and query when wrapped in constant_score compound query. Both gives me score of 1 without any boost for all documents. But the docs say that filter context is activated when we use filter clause inside constant_score. If I am getting constant score for all documents with query parameter under constant_score then that means that the query is running in filter context only. So why the doc specifically mentions filter parameter inside constant_score ? What am I missing ?
You should read this part of documentation:
https://www.elastic.co/guide/en/elasticsearch/reference/5.5/query-filter-context.html
https://www.elastic.co/guide/en/elasticsearch/reference/5.5/query-dsl-constant-score-query.html
Elasticsearch provides good explanation of your.
However, filters out constant_score will return all values that match them. Filters in constant_point, returns the result of the filter, limited by constant_score.
Regards.

Elastic Search: Use Query or Filtered Query for term, prefix or range queries?

I wonder if I should translate my Term Queries into a filtered Query to cache the results and to keep the score?
From the documentation of filtered query:
Filters are usually faster than queries because:
they don’t have to calculate the relevance _score for each document — 
the answer is just a boolean “Yes, the document matches the filter” or
“No, the document does not match the filter”.
the results from most
filters can be cached in memory, making subsequent executions faster
Also, from the filters:
Some filters already produce a result that is easily cacheable, and
the difference between caching and not caching them is the act of
placing the result in the cache or not. These filters, which include
the term, terms, prefix, and range filters, are by default cached and
are recommended to use (compared to the equivalent query version) when
the same filter (same parameters) will be used across multiple
different queries (for example, a range filter with age higher than
10).
My solution is that I would then put the term query inside a filtered query, where the "query" and "filter" predicate are the same.
I guess that I then have the best of both worlds, scoring and caching. Does this makes sense?
Sample Query:
{
"_source":true,
"query":{"term":{"displayName":"example name"}}
}
Optimized Filtered Query:
{
"_source":true,
"query":
{"filtered":
{"query":{"term":{"displayName":"example name"}},
"filter":{"term":{"displayName":"example name"}}
}
}
}
I have tested this, but didn't noticed some performance gains. As I have to write a lot of search terms beforehand, I would like to know what is the best solution and why.

Difference between boolean query and term query

Difference between boolean query and term query.
As far as my understanding boolean query is combination of one or more query and term query is single query to match particular field.Am in right?
Thanks in Advance..!
A bool query literally combines multiple queries of any type together with clauses such as must, must_not, and should.
A term query specifies a single field and a single term to determine if the field matches. Note that term queries are specifically for non-analyzed fields.

Resources