elasticsearch

How elastic queries work with contradicting statements


Suppose I have a query like this:

{
  "query": {
    "bool": {
      "must_not": [
        { "term": { "field": "value" } }
      ],
      "must": [
        { "term": { "field": "value" } }
      ]
    }
  }
}

Will it return results that have other values in "field" or not? Does must_not have precedence over must in this case, or it completely breaks the resultset?

I can't find anything on this topic in the docs.

Of course, you can fix this query manually by removing the "must" part but when it comes to executing such queries programmatically, you can have some sets of values that can be set to these statements, so you can have potential intersections here.


Solution

  • Will it return results that have other values in "field" or not?

    No, it will always return 0 results because it's an AND condition and searches at the exact opposite of another query.

    When building queries programmatically, it’s essential to avoid such conflicts.