ElasticSearch searches for the “most relevant” documents that match your query, while you try to combine the three queries.
The easiest (and fastest) way to do this is to run three queries using multi-user search :
curl -XGET 'http://127.0.0.1:9200/my_index/_msearch?pretty=1' -d ' {} {"query" : {"text" : {"title" : "some words"}}, "size" : 5} {} {"query" : {"text" : {"title" : "some other words"}}, "size" : 5} {} {"query" : {"text" : {"title" : "other words"}}, "size" : 5} '
An alternative, depending on your requirements, may be to use a filter, but note that it limits the number of PER SHARD results, not per index. By default, the index has 5 primary fragments, so if you specify a limit of 5, you can get 25 results.
So maybe something like this:
curl -XGET 'http://127.0.0.1:9200/_all/_search?pretty=1' -d ' { "query" : { "bool" : { "should" : [ { "filtered" : { "filter" : { "limit" : { "value" : 1 } }, "query" : { "text" : { "title" : "some words" } } } }, { "filtered" : { "filter" : { "limit" : { "value" : 1 } }, "query" : { "text" : { "title" : "other words" } } } }, { "filtered" : { "filter" : { "limit" : { "value" : 1 } }, "query" : { "text" : { "title" : "some other words" } } } } ] } } } '
This will give you the top scoring document for each phrase on each shard (with 5 shards, a maximum of 15 documents, which (since you did not specify size=15 ) will be reduced to the top 10 documents).
Your mileage may vary depending on how your documents are distributed across your shards.
Drtech
source share