Elasticsearch query string query with not equal to?

Elasticsearch

Elasticsearch Problem Overview


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.

Elasticsearch Solutions


Solution 1 - Elasticsearch

You need to use the NOT operator, like this:

!(name:"Fred")

or

NOT (name:"Fred")

Solution 2 - Elasticsearch

You should use bool query with must_not statement

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

Solution 3 - Elasticsearch

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"
                    }
                }
            ]
        }
    }
}

Solution 4 - Elasticsearch

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" }
                }
            }
        }
    }
}

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionRolandoView Question on Stackoverflow
Solution 1 - ElasticsearchValView Answer on Stackoverflow
Solution 2 - ElasticsearchVova BilyachatView Answer on Stackoverflow
Solution 3 - ElasticsearchAjit SurendranView Answer on Stackoverflow
Solution 4 - ElasticsearchOm PrakashView Answer on Stackoverflow