Filter context for should in bool query - elasticsearch

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.

Related

Must returns more results than Filter

I checked this question What is the difference between must and filter in Query DSL in elasticsearch? and read answers.
As far as I understood must and filter should return same result. Am I right? But when I change filter query to must, I receive more result? What I am doing wrong?
I compared filter and must query and got different result.
Must query gives you some score that is used to add to the total score of the doc.
Filter query does not add any score. It is just used to decide whether a doc is returned or not in the result set.
By just looking at the screenshot of the query attached, when you change filter query to must query it starts adding some value to the total score of the doc.
Since you are using min_score condition, the must clause makes more docs exceed 0.2 score and hence more docs are returned in the final result set.
Rest things will be more clear when you share the complete query.

How to search result set from a result set in elasticsearch

Im tring to understand the DSL query i needed if i want to make a search from a result set i got. means i have an initial term search, then i want make another query upon the previous result.
lets say i a have 10 documents with a sharing identifier between them, each document has a description field. i want to search first all the documents containing the value 'Hello' in the description and then take their id's, and search the document containing the value 'good by'.
thanks.
No need to execute two queries, you can use filter context that will filter out the results.filter parameter will filter out documents that do not match, and will also not affect the score for matching documents.
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.
Refer this to know more about Query and Filter contexts

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.

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

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.

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.

Resources