Rolando
Rolando

Reputation: 62596

Elasticsearch query string query with not equal to?

Usually with a query_string query in elasticsearch, I can do:

name:"Fred"

I want to find all documents where name is not equal to Fred. What is the proper syntax for that? I tried:

name!="Fred"

Though it returns 0 documents.

Upvotes: 105

Views: 236112

Answers (4)

Om Prakash
Om Prakash

Reputation: 853

and, or and not filters


For "and" query:

{
    "filtered" : {
        "query" : {
            "term" : { "name.first" : "shay" }
        },
        "filter" : {
            "and" : {
                "filters" : [
                    {
                        "term" : { "name.first" : "something" }
                    },
                    {
                        "term" : { "name.first" : "other" }
                    }
                ]
            }
        }
    }
}

For "or" query:

{
    "filtered" : {
        "query" : {
            "term" : { "name.first" : "shay" }
        },
        "filter" : {
            "or" : {
                "filters" : [
                    {
                        "term" : { "name.first" : "something" }
                    },
                    {
                        "term" : { "name.first" : "other" }
                    }
                ]
            }
        }
    }
}

For "not" query:

{
    "filtered" : {
        "query" : {
            "term" : { "name.first" : "shay" }
        },
        "filter" : {
            "not" : {
                "filter" :  {
                    "term" : { "name.first" : "someting" }
                }
            }
        }
    }
}

Upvotes: 5

Ajit Surendran
Ajit Surendran

Reputation: 777

You can also use a combination of must and must_not. Like pull docs where name is Ajit and status is not queued.

{
    "query": {
        "bool": {
            "must": [
                {
                    "match": {
                        "name": "Ajit"
                    }
                }
            ],
            "must_not": [
                {
                    "match": {
                        "status": "queued"
                    }
                }
            ]
        }
    }
}

Upvotes: 32

Vova Bilyachat
Vova Bilyachat

Reputation: 19474

You should use bool query with must_not statement

{
  "query": {
    "bool" : {
      "must_not" : {
        "term" : {
          "name" : "Fred"
        }
      }
    }
  }
}

Upvotes: 92

Val
Val

Reputation: 217254

You need to use the NOT operator, like this:

!(name:"Fred")

or

NOT (name:"Fred")

Upvotes: 169

Related Questions