Elasticsearch query string query with not equal to?
ElasticsearchElasticsearch 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
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" }
}
}
}
}
}