Home > Mobile >  Cannot seem to use must and must_not together in an elastic search query
Cannot seem to use must and must_not together in an elastic search query

Time:01-24

If I run the following query:

{
    "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"
                        }
                    }
                }
            ]
        }
    }
}

against an index with articles that look like:

{
    "_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": {}
    }
}

I get the following validation error from 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"
}

And I cannot figure out what the issue might be! From the elastic docs it should be OK.

I'm using ElasticSearch 7.9.3 running in a local docker container.

I'm hoping someone out there will give me a clue!

Cheers!

I would expect this to just work.

I'm trying to filter out articles that have both of the country codes gb and mx in the field allowedCountries.

I can include them easily enough in the results when I add the terms_set query to the bool.must section of the query.

CodePudding user response:

It works well, you just need to enclose your query in the query section

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

CodePudding user response:

Thank you for responding!

I was helping with a system I did not have full context on - it turns out there is a proxy in the mix with validation that was blocking the must_not query. So, with the proxy fixed, it now works.

  • Related