我正在编写一个查询来获得匹配多个短语之一的结果,比如
{
'size': 10,
'from': 0,
'query': {
'bool': {
'should': [
{'text': {'title': { 'query': 'some words' }}},
{'text': {'title': { 'query': 'other words' }}},
{'text': {'title': { 'query': 'some other words' }}},
]
}
}
}
Run Code Online (Sandbox Code Playgroud)
它按预期工作,但我有一个问题:10个得分结果都匹配相同的短语.
我想到的解决方案是将每个should子句的结果数限制为5个元素.
问题是我没有看到如何使用弹性搜索查询来实现它,我不知道是否可能,或者它是否存在另一种方式来做我想要的.
有任何想法吗 ?
谢谢 !
DrT*_*ech 11
当您尝试实现3个查询的并集时,ElasticSearch正在寻找与您的查询匹配的"最相关"文档.
最简单(也是最快)的方法是使用多搜索运行三个查询:
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}
'
Run Code Online (Sandbox Code Playgroud)
根据您的要求,另一种选择可能是使用限制过滤器,但请注意,它限制了PER SHARD的结果数量,而不是每个索引.默认情况下,索引有5个主分片,因此如果指定限制为5,则可能会得到25个结果.
所以也许是这样的:
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"
}
}
}
}
]
}
}
}
'
Run Code Online (Sandbox Code Playgroud)
这将为您提供每个分片上每个短语的最高评分文档(包含5个分片,最多15个文档,其中(因为您未指定size=15)将减少到前10个文档).
您的里程可能会有所不同,具体取决于您的文档在整个分片中的分布情况.
| 归档时间: |
|
| 查看次数: |
9795 次 |
| 最近记录: |