89

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.

golopot
  • 10,726
  • 6
  • 37
  • 51
Rolando
  • 58,640
  • 98
  • 266
  • 407

4 Answers4

143

You need to use the NOT operator, like this:

!(name:"Fred")

or

NOT (name:"Fred")
Val
  • 207,596
  • 13
  • 358
  • 360
86

You should use bool query with must_not statement

{
  "query": {
    "bool" : {
      "must_not" : {
        "term" : {
          "name" : "Fred"
        }
      }
    }
  }
}
Vova Bilyachat
  • 18,765
  • 4
  • 55
  • 80
  • Why? Which is the better option, to use Query String query or your option? – RodriKing Jun 27 '18 at 12:06
  • @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. – Vova Bilyachat Jun 27 '18 at 12:33
  • 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 Jun 27 '18 at 12:41
  • @RodriKing create separate question with example pls. – Vova Bilyachat Jun 27 '18 at 12:42
29

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"
                    }
                }
            ]
        }
    }
}
Jason Pan
  • 702
  • 7
  • 21
Ajit Surendran
  • 709
  • 7
  • 4
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" }
                }
            }
        }
    }
}
Om Prakash
  • 793
  • 7
  • 14