109

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.

4 Answers 4

174

You need to use the NOT operator, like this:

!(name:"Fred")

or

NOT (name:"Fred")
Sign up to request clarification or add additional context in comments.

2 Comments

For those wondering how to concat multiple values NOT(key:value1 value2 value3)
@val can you help here please stackoverflow.com/questions/66331236/…
94

You should use bool query with must_not statement

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

4 Comments

Why? Which is the better option, to use Query String query or your option?
@RodriKing there is not better option it depends what you are going to do. In current question question was how to filer on specific term this is why I would use term, query_string on other hand will split your search text to multiple terms. So basically it depends on what you want to achieve.
Ok thank you, another little question, please. I want to make a wildcard query, but wildcard queries do not apply to analized/filters (match query does). I want to apply the analysis of ignoring the accents of the words. As with wildcard queries I can't, I'm using query string queries. Would that be correct?
@RodriKing create separate question with example pls.
32

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

Comments

5

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.