sim
sim

Reputation: 488

How to filter not in elasticsearch DSL query

DSL query is below

 {'from': 0, 'size': 10, 'aggs': 
     {'products': {'terms': {'field': 'softwares.name.keyword', 'order': {'_key': 'desc'}}}, 
      'color': {'terms': {'field': 'white.name.keyword', 'order': {'_key': 'desc'}}},
      'types': {'terms': {'field': 'mercedez.name.keyword', 'order': {'_key': 'desc'}}}},
     'query': {'bool': {'must': {'match_all': {}}, 
     'filter': [{'term': {'name.keyword': 'Germany'}}]}}}

My Ask?

Upvotes: 0

Views: 1989

Answers (1)

Sagar Patel
Sagar Patel

Reputation: 5486

You can use must_not query with bool query.

Option 1: using filter

{
  "query": {
    "bool": {
      "must": [
        {
          "match_all": {}
        }
      ],
      "filter": [
        {
          "bool": {
            "must_not": [
              {
                "term": {
                  "name.keyword": {
                    "value": "Germany"
                  }
                }
              }
            ]
          }
        }
      ]
    }
  }
}

Option 2: without filter (but here Germany query will be also consider for score calculation)

{
  "query": {
    "bool": {
      "must": [
        {
          "match_all": {}
        }
      ],
      "must_not": [
        {
          "term": {
            "name.keyword": {
              "value": "Germany"
            }
          }
        }
      ]
    }
  }
}

Upvotes: 1

Related Questions