Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 740
Rolando Avatar asked Nov 14 '16 22:11

Rolando


People also ask

How do I use not equal in Elasticsearch?

Similarly, to find documents whose field value is NOT equal to a given query string, you can do so using the NOT operator. If you want to find documents whose field value does not match multiple values, you need to use a space separated list. NOTE: you can also get the same results using a must_not boolean query.

How do I search multiple fields in Elasticsearch?

One of the most common queries in elasticsearch is the match query, which works on a single field. And there's another query with the very same options that works also on multiple fields, called multi_match. These queries support text analysis and work really well.


Video Answer


2 Answers

You need to use the NOT operator, like this:

!(name:"Fred") 

or

NOT (name:"Fred") 
like image 86
Val Avatar answered Sep 21 '22 19:09

Val


You should use bool query with must_not statement

{   "query": {     "bool" : {       "must_not" : {         "term" : {           "name" : "Fred"         }       }     }   } } 
like image 37
Vova Bilyachat Avatar answered Sep 20 '22 19:09

Vova Bilyachat