创buildElasticsearchcurl查询为非空且不为空(“”)

我如何创buildElasticsearchcurl查询来获得非空和非空的字段值(“”),

这里是mysql的查询:

select field1 from mytable where field1!=null and field1!=""; 

空值和空string都会导致没有值被索引,在这种情况下,您可以使用existsfilter

 curl -XGET 'http://127.0.0.1:9200/test/test/_search?pretty=1' -d ' { "query" : { "constant_score" : { "filter" : { "exists" : { "field" : "myfield" } } } } } ' 

或者在title字段中结合(例如)全文search:

 curl -XGET 'http://127.0.0.1:9200/test/test/_search?pretty=1' -d ' { "query" : { "filtered" : { "filter" : { "exists" : { "field" : "myfield" } }, "query" : { "match" : { "title" : "search keywords" } } } } } ' 

在Bool Filter的Must-Not部分封装 Missing Filter 。 它只会返回字段存在的文档,并且如果将“null_value”属性设置为true,则显式值不为null。

 { "query":{ "filtered":{ "query":{ "match_all":{} }, "filter":{ "bool":{ "must":{}, "should":{}, "must_not":{ "missing":{ "field":"field1", "existence":true, "null_value":true } } } } } } } 

正如@luqmaan在注释中指出的那样, 文档说filterexists 不会过滤出空string,因为它们被认为是非空值

所以添加到@ DrTech的答案,有效地过滤空和空string值,你应该使用这样的东西:

 { "query" : { "constant_score" : { "filter" : { "bool": { "must": {"exists": {"field": "<your_field_name_here>"}}, "must_not": {"term": {"<your_field_name_here>": ""}} } } } } } 

您可以使用不在filter之上的filter。

 "query": { "filtered": { "query": { "match_all": {} }, "filter": { "not": { "filter": { "missing": { "field": "searchField" } } } } } } 

我们正在使用Elasticsearch版本1.6,并且我使用来自同事的这个查询来覆盖不为空而不是空的字段:

 { "query": { "filtered": { "query": { "match_all": {} }, "filter": { "bool": { "must": [ { "exists": { "field": "myfieldName" } }, { "not": { "filter": { "term": { "myfieldName": "" } } } } ] } } } } }