似乎无法在ElasticSearch查询中同时使用must和must_not

0g0grzrc  于 2023-01-20  发布在  ElasticSearch
关注(0)|答案(1)|浏览(193)

如果运行以下查询:

{
    "query": {
        "bool": {
            "must": [
                {
                    "multi_match": {
                        "query": "boxing",
                        "fuzziness": 2,
                        "minimum_should_match": 2
                    }
                }
            ],
            "must_not": [
                {
                    "terms_set": {
                        "allowedCountries": {
                            "terms": ["gb", "mx"],
                            "minimum_should_match_script": {
                                "source": "2"
                            }
                        }
                    }
                }
            ],
            "filter": [
                {
                    "range": {
                        "expireTime": {
                            "gt": 1674061907954
                        }
                    }
                },
                {
                    "term": {
                        "region": {
                            "value": "row"
                        }
                    }
                },
                {
                    "term": {
                        "sourceType": {
                            "value": "article"
                        }
                    }
                }
            ]
        }
    }
}

对照包含如下文章的索引:

{
    "_index": "content-items-v10",
    "_type": "_doc",
    "_id": "e7hm75ui4dma1mm4j8q5v7914",
    "_score": 4.3724976,
    "_source": {
        "allowedCountries": ["gb", "ie"],
        "body": "Both Joshua Buatsi and Craig Richards join The DAZN Boxing Show ahead of their clash at London's O2 Arena. Matchroom's Eddie Hearn also gives his take on the night, as well as Chantelle Cameron previewing her contest with Victoria Noelia Bustos.",
        "competitions": [
            {
                "id": "8lo6205qyio0fksjx9glqbdhj",
                "name": "Buatsi v Richards"
            }
        ],
        "contestants": [
            {
                "id": "7rq59j3eiamxlm12vhxcsgujj",
                "name": "Joshua Buatsi"
            },
            {
                "id": "boby9oqe23g6qyuwphrxh8su5",
                "name": "Craig Richards"
            }
        ],
        "countries": [
            {
                "id": "7yasa43laq1nb2e6f8bfuvxed",
                "name": "World"
            },
            {
                "id": "258l9t5sm55592i08mdpqzr3t",
                "name": "United Kingdom"
            }
        ],
        "dotsLastUpdateTime": 1673979749396,
        "expireTime": 4800000000000,
        "fixtureDate": {},
        "headline": "Buatsi vs. Richards: Preview",
        "id": "e7hm75ui4dma1mm4j8q5v7914",
        "importance": 0,
        "languageKeys": ["en"],
        "languages": ["en"],
        "lastUpdateTime": {
            "ts": 1653088281000,
            "iso8601": "2022-05-20T23:11:21.000Z"
        },
        "promoImageUrl": null,
        "publication": {
            "typeId": "1plcw0iyhx9vn1fcanbm2ja3rf",
            "typeName": "Shoulder"
        },
        "publishedTime": {
            "ts": 1653088281000,
            "iso8601": "2022-05-20T23:11:21.000Z"
        },
        "region": "row",
        "shortHeadline": null,
        "sourceType": "article",
        "sports": [
            {
                "id": "2x2oqzx60orpoeugkd754ga17",
                "name": "Boxing"
            }
        ],
        "teaser": "",
        "thumbnailImageUrl": "https://images.daznservices.com/di/library/babcock_canada/45/3e/the-dazn-boxing-show-20052022_xc4jbfqi022l1shq9lu641h9e.png?t=-477976832",
        "translations": {}
    }
}

我从elasticsearch得到了以下验证错误:

{
    "ok": false,
    "errors": {
        "validation": [
            {
                "message": "\"query.bool.must_not\" is not allowed",
                "path": [
                    "query",
                    "bool",
                    "must_not"
                ],
                "type": "object.unknown",
                "context": {
                    "child": "must_not",
                    "label": "query.bool.must_not",
                    "value": [
                        {
                            "terms_set": {
                                "allowedCountries": {
                                    "terms": [
                                        "gb",
                                        "mx"
                                    ],
                                    "minimum_should_match_script": {
                                        "source": "2"
                                    }
                                }
                            }
                        }
                    ],
                    "key": "must_not"
                }
            }
        ]
    },
    "correlationId": "d29e9275-9ab3-4ff8-944d-852b98d4b503"
}

我不知道是什么问题!从弹性文件来看应该没问题。
我使用的是在本地Docker容器中运行的ElasticSearch 7.9.3。
我希望外面有人能给我点提示!
干杯!
我希望这能成功。
我正在尝试过滤掉在allowedCountries字段中同时具有国家代码gbmx的文章。
当我将terms_set查询添加到查询的bool.must部分时,我可以很容易地将它们包含在结果中。

c8ib6hqw

c8ib6hqw1#

它运行良好,您只需将查询包含在query部分中

{
    "query": {        <--- add this 
       "bool": {      <--- your query starts here
          "must": [
      ...

相关问题