Home > Back-end >  How can I put `must_not` under `filter` in Elasticsearch?
How can I put `must_not` under `filter` in Elasticsearch?

Time:07-05

I'd like to use not equal in my filter but it doesn't work [13:24] [bool] failed to parse field [filter]:

"query": {
    "bool": {
      
      "filter": [
        {
          "must_not" : {
            "term" : {
               "status" : "DECLINED"
            }
           }
        },
        {
           "term": { "type": "ORDER"}
         }
      ]
    }
  }

it works if I put the must_not under query like below. How can I put not equal in filter?

"query": {
    "bool": {
      "must_not": {
        "term": {
          "status": "DECLINED"
        }
      },
      "filter": ...

CodePudding user response:

May be one more bool needed inside the filter ?

/_search
{
  "query": {
    "bool": {
      "filter": {
        "bool": {
          "must": [
            {
                "term": { "type": "ORDER"}
            }
          ],
          "must_not": [
            {
              "term": {
                "status": "DECLINED"
              }
            }
          ]
        }
      }
    }
  }
}
  • Related