Christof Buechi
Christof Buechi

Reputation: 185

How to create an ElasticSearch Query which should not match any of multiple categories

I'm looking for documents inside elasticSearch which do not match one or the other brand, but fullFill a fix requirement. I'm looking for any entries which are not from Toyota, BMW or Audi. But the entry must be a superEntry (exact match)

The following Query is what I'm working on:

    "query": {
        "bool": {
            "filter": {
                "term": {
                    "superEntry": true
                }
            },
            "must": {
                "bool": {
                    "must_not": [
                        {
                            "term": {
                                "brand": "Toyota"
                            }
                        },
                        {
                            "term": {
                                "brand": "BMW"
                            }
                        },
                        {
                            "term": {
                                "brand": "Audi"
                            }
                        }
                    ]
                }
            }
        }
    }
}

Expected: I find any super-entries from any other brand, but not from those 3. The query above still lists me cars from BMW as an example..

Upvotes: 0

Views: 247

Answers (1)

A l w a y s S u n n y
A l w a y s S u n n y

Reputation: 38502

Not tested but something like this will help-

{
  "size": 10,
  "query": {
    "bool": {
      "must": [
        {
          "match": {
            "superEntry": true
          }
        }
      ],
      "must_not": [
        {
          "terms": {
            "brand": [
              "Toyota",
              "BMW",
              "Audi"
            ]
          }
        }
      ]
    }
  }
}

Upvotes: 2

Related Questions