Insert a must_not match inside nested filtered query

Here's my current query, which is returning 100 results, instead of 99.

{
  "query": {
    "bool": {
      "filter": [
        {
          "nested": {
            "path": "properties",
            "query": {
              "bool": {
                "filter": [
                  {
                    "match": {
                      "properties.key": "keyToInclude"
                    }
                  },
                  {
                    "match": {
                      "properties.value": "valueToInclude"
                    }
                  }
                ],
                "must_not": [
                  {
                    "match": {
                      "properties.key": "keyToExclude"
                    }
                  }
                ]
              }
            }
          }
        }
      ]
    }
  }
}

There's one document which has the property key "keysToExclude", which is a nested object inside the properties array.
Why is it not excluding that one document? Is my structure wrong or should I not use match inside must_not or something else?

Thanks!

And of course right after I've asked, I've found the answer or at least a possible solution:

{
  "query": {
    "bool": {
      "filter": [
        {
          "nested": {
            "path": "properties",
            "query": {
              "bool": {
                "filter": [
                  {
                    "match": {
                      "properties.key": "keyToInclude"
                    }
                  },
                  {
                    "match": {
                      "properties.value": "valueToInclude"
                    }
                  }
                ]
              }
            }
          }
        }
      ],
      "must_not": [
        {
          "nested": {
            "path": "properties",
            "query": {
              "bool": {
                "filter": [
                  {
                    "match": {
                      "properties.key": "keyToExclude"
                    }
                  }
                ]
              }
            }
          }
        }
      ]
    }
  }
}

Please let me know if there's a better and easier way to do it.

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.