gpt4 book ai didi

elasticsearch - Elasticsearch复合查询

转载 作者:行者123 更新时间:2023-12-02 23:07:50 25 4
gpt4 key购买 nike

我正在使用如下所示的复合查询来查询具有300条记录的 flex 索引:

GET my_index/_search
{
"size": 10,
"query": {
"bool": {
"should": [
{
"bool": {
"should": [
{
"multi_match": {
"query": "card",
"fields": [
"title^1.0"
]
}
}
],
"must": {
"term": {
"_index": {
"value": "my_index"
}
}
}
}
}
]
}
}
}
必须使用索引是因为根据某些业务逻辑,这可能是一个多索引查询(最有可能应该是一个过滤器,我可以更改它,但这不是我的问题的一部分。我得到的结果与filter相同好)。
虽然我希望这会返回与should子句匹配的文档,但我正在检索索引中的所有文档(300)
为什么会这样?

最佳答案

解决此问题的方法是在查询中添加minimumShouldMatch字段。结果查询变为:

GET my_index/_search
{
"size": 10,
"query": {
"bool": {
"should": [
{
"bool": {
"minimum_should_match": 1,
"should": [
{
"multi_match": {
"query": "card",
"fields": [
"title^1.0"
]
}
}
],
"must": {
"term": {
"_index": {
"value": "my_index"
}
}
}
}
}
]
}
}
}
我相信其背后的原因是调整 bool(boolean) 查询以提供最大数量的匹配结果(more-matches-is-更好)。因此,如果must / filter子句匹配,则甚至不会执行。通过添加“minimum_should_match”:1,我们指示elasticsearch在返回文档之前至少匹配1个应该子句。
flex 文档摘录:

The bool query takes a more-matches-is-better approach, so the score from each matching must or should clause will be added together to provide the final _score for each document.


You can use the minimum_should_matchparameter to specify the number or percentage of should clausesreturned documents must match.

If the bool query includes at least one should clause and no must orfilter clauses, the default value is 1. Otherwise, the default valueis 0.

For other valid values, see the minimum_should_match parameter.


引用链接- https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-bool-query.html#bool-min-should-match

关于elasticsearch - Elasticsearch复合查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63568981/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com