gpt4 book ai didi

elasticsearch - 当存在 "should"、 "should"和 "must"子句时,Elastic Search "must_not"子句无法按预期工作

转载 作者:行者123 更新时间:2023-12-02 22:19:20 25 4
gpt4 key购买 nike

我已经加载了提供的数据 here进入 Elasticsearch 。

现在我正在尝试查询帐户。

  1. 年龄必须为 38 岁。
  2. State 不应为“ID”。
  3. Address Should Contain Either:(这不起作用)
    • 文本“车道”或
    • 全文“磨坊大道”

请求:POST - http://localhost:9200/bank/account/_search?pretty

{
"query": {
"bool": {
"should": [
{ "match": { "address": "lane" } },
{ "match_phrase": { "address": "mill avenue" } }
],
"must": [
{ "match": { "age": "38" } }
],
"must_not": [
{ "match": { "state": "ID" } }
]
}
},
"from": 0,
"size": 1,
"_source": ["account_number", "balance", "firstname", "lastname", "age", "email", "address", "state"],
"sort": [
{ "account_number": "asc" },
{ "balance": "desc"}
]
}

响应:

{
"took": 8,
"timed_out": false,
"_shards": {
"total": 5,
"successful": 5,
"failed": 0
},
"hits": {
"total": 36,
"max_score": null,
"hits": [
{
"_index": "bank",
"_type": "account",
"_id": "21",
"_score": null,
"_source": {
"account_number": 21,
"firstname": "Estella",
"address": "859 Portal Street",
"balance": 7004,
"state": "WV",
"age": 38,
"email": "estellapaul@zillatide.com",
"lastname": "Paul"
},
"sort": [
21,
7004
]
}
]
}
}

您可以检查响应。已收到地址为 “859 Portal Street” 的记录,它不包含“lane”或“mill avenue”

Elasticsearch 版本:5.1.1

----编辑----解决方案(感谢发布的@Lax 和@mattweber here ):如果存在 must/must_not,则需要
minimum_should_match

{
"query": {
"bool": {
"minimum_should_match": 1,
"should": [
{ "match": { "address": "avenue" } },
{ "match_phrase": { "address": "Williams Place" } }
],
"must": [
{ "match": { "age": "38" } }
],
"must_not": [
{ "match": { "state": "ID" } }
],
"filter": {
"range": {
"balance": {
"gte": 20000,
"lte": 30000
}
}
}
}
},
"from": 0,
"size": 1000,
"_source": ["account_number", "balance", "firstname", "lastname", "age", "email", "address", "state"],
"sort": [
{ "account_number": "asc" },
{ "balance": "desc"}
],
"aggs": {
"group_by_state": {
"terms": {
"field": "state.keyword"
},
"aggs": {
"average_balance": {
"avg": {
"field": "balance"
}
}
}
}
}
}

最佳答案

当 must/must_not present 你需要添加:

minimum_should_match" : 1

在应该数组之后

关于elasticsearch - 当存在 "should"、 "should"和 "must"子句时,Elastic Search "must_not"子句无法按预期工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41385855/

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