elasticsearchelastic-stackelasticsearch-dsl

How to fetch documents with filter context with not clause in elastic search 8.9 v


{
    "size": 0,
    "query": {
        "bool": {
            "must": [
             {
                 "match": {
                     "cid": {
                         "query": "AFM"
                     }
                 }
             },
                {
                    "match": {
                        "web_code": "P" 
                    }
                }
            ],
            "filter": [
                {
                     
                    "not": {  // "type": "parsing_exception","reason": "unknown query [not]"
                        "term": {
                            "web_code": "PS"
                        }
                    }
                    
                }
            ]
        }
    }
 }

Solution

  • You simply need to use the bool/must_not query:

    {
      "size": 0,
      "query": {
        "bool": {
          "must": [
            {
              "match": {
                "cid": {
                  "query": "AFM"
                }
              }
            },
            {
              "match": {
                "web_code.keyword": "P"
              }
            }
          ],
          "must_not": [
            {
              "term": {
                "web_code.keyword": "PS"
              }
            }
          ]
        }
      }
    }