Saptarshi Guha
Saptarshi Guha

Reputation: 51

elasticsearch-dsl: Negate a Filter

I have a query like this

   s = Search(using=es, index=indices)
   s = s.filter('range', 
                utctimestamp={'gte': begindateUTC, 'lte': enddateUTC})
         .filter("term",tags="foo").
         .sort("-utctimestamp")

My question is, how can negate the filter? i.e. not foo. The above is a "must" using filter e.g.

  "filter": {
    "bool": {
      "must": [
        {
          "range": {
            "utctimestamp": {
              "from": 1460407864660,
              "to": 1460409664660
            }
          }
        },
        {
          "fquery": {
            "query": {
              "query_string": {
                "query": "_type:(\"foo\")"
              }
            },
            "_cache": true
          }
        }
      ]

how do i make it 'must not' for the 'foo' bit.

thanks in advance

Upvotes: 3

Views: 2627

Answers (1)

mbudnik
mbudnik

Reputation: 2107

You should use you filter in must_not clause.

The must_not clauses do not affect the score; their only purpose is to exclude documents that might otherwise have been included.

Upvotes: 6

Related Questions