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
Related
I have a situation where I need to search from multiple indexes (products and users). Below is a sample query I am using to do that search
http://localhost:9200/_all/_search?q=*wood*
http://localhost:9200/users,products/_search?q=*wood*
With the above API request, it only returns search results for the product index. But if I search using the below API it returns search results for users index
http://localhost:9200/users/_search?q=*wood*
As you can see I am passing same value for "q" parameter. I need to search for both product and users index and check if there is the word "wood" in any attribute in both indexes. How can I achieve this
You can pass multiple index names instead of _all as it will search in other indices that you don't intent to by using the comma seprated index name like
http://localhost:9200/users,products/_search?q=*wood*
Although, _all should also fetch the result from users index which you get when you specify its name, you need to debug why its happening, maybe increase the size param to 1000 as by default Elasticsearch returns only 10 results and it seems in case of _all all the top results coming from products index only.
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.
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.
How can I get the list of terms that elasticsearch matched in each result? I know the highlight contains this but I want to get a list of the terms that were found without manually performing postprocessing on the highlight for each result.
You could use named queries with unique query for each term.
Search result will contain matched queries for each document in result.
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.