限制弹性搜索中应用条款的结果数量 [英] Limiting the number of results of should clauses in Elastic Search
问题描述
我正在撰写一个查询,以获得匹配多个短语之一的结果,如
{
'size ':10,
'from':0,
'query':{
'bool':{
'should':[
{ 'text':{'title':{'query':'some words'}}},
{'text':{'title':{'query':'other words'}}},
{'text':{'title':{'query':'some other words'}}},
]
}
}
}
它按预期工作,但我有一个问题:10个得分结果都匹配相同的短语。 p>
我想到的解决方案是将每个应该
子句的结果数限制为5个元素。 p>
问题是我没有看到如何使用弹性搜索查询来实现这一点,而且我不知道是否可能,或者是否存在另一种方式来做我想要。
任何ide作为?
谢谢!
ElasticSearch正在寻找最相关的文档与您的查询匹配,而您正在尝试实现3个查询的并集。
最简单(最快)的方法是运行三个查询,使用多重搜索:
curl -XGET'http://127.0.0.1:9200/my_index/_msearch?pretty=1'-d'
{}
{query:{text:{title:some words}},size:5}
{}
{query:{text {title:other other words}},size:5}
{}
{query:{text:{title:other words}} ,size:5}
'
另一种选择,根据您的要求可能使用限制过滤器,但请注意,它限制Ť他的结果数量PER SHARD,而不是每个索引。默认情况下,一个索引有5个主分片,所以如果你指定一个限制为5,你可能会得到25个结果。
所以也许这样的事情: p>
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
}
},
查询:{
文本:{
标题:其他单词
}
}
}
}
]
}
}
}
'
这将为您提供每个分片上每个短语的最高评分文档(最多5个分片,最多15个文档,因为您没有指定 size = 15
)将减少到前10名文档。
您的里程可能会有所不同,取决于您的文档如何分布在您的分片上。
I'm writing a query to get results matching one of multiple phrases, like
{
'size': 10,
'from': 0,
'query': {
'bool': {
'should': [
{'text': {'title': { 'query': 'some words' }}},
{'text': {'title': { 'query': 'other words' }}},
{'text': {'title': { 'query': 'some other words' }}},
]
}
}
}
It works as expected, but I have a problem : the 10 scored results are all matching the same phrase.
The solution I thought of was to limit the number of results from each should
clause to 5 elements for example.
The problem is that I don't see how to implement this using Elastic Search queries, and I don't know if it possible, or if it exists another way to do what I want.
Any ideas ?
Thanks !
ElasticSearch is looking for the "most relevant" docs matching your query, while you are trying to achieve a union of 3 queries.
The simplest (and fastest) way to do this would be to run three queries, using multi 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 the limit filter, but note that it limits the number of results PER SHARD, not per index. By default, an index has 5 primary shards, so if you specify a limit of 5, you may well get 25 results back.
So perhaps 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 would give you the top scoring doc for each phrase on each shard (with 5 shards, a maximum of 15 docs, which (because you haven't specified size=15
) would be reduced to the top 10 docs).
Your mileage may vary, depending on how your docs are distributed across your shards.
这篇关于限制弹性搜索中应用条款的结果数量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!