如何创建Elasticsearch curl查询来获取非空非空(“”)的字段值,
下面是mysql查询:
select field1 from mytable where field1!=null and field1!="";发布于 2013-02-07 18:23:48
空值和空字符串都不会对任何值进行索引,在这种情况下,您可以使用exists过滤器
curl -XGET 'http://127.0.0.1:9200/test/test/_search?pretty=1' -d '
{
"query" : {
"constant_score" : {
"filter" : {
"exists" : {
"field" : "myfield"
}
}
}
}
}
'或者结合(例如) title字段上的全文搜索:
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"
}
}
}
}
}
'发布于 2015-07-31 02:55:08
正如@luqmaan在评论中指出的那样,documentation表示filter exists 不会过滤掉空字符串,因为它们被认为是非空值。
因此,除了@DrTech的答案之外,为了有效地过滤掉null和空字符串的值,您应该使用如下内容:
{
"query" : {
"constant_score" : {
"filter" : {
"bool": {
"must": {"exists": {"field": "<your_field_name_here>"}},
"must_not": {"term": {"<your_field_name_here>": ""}}
}
}
}
}
}发布于 2018-01-22 23:34:31
在elasticsearch 5.6上,我必须使用下面的命令来过滤掉空字符串:
GET /_search
{
"query" : {
"regexp":{
"<your_field_name_here>": ".+"
}
}
} https://stackoverflow.com/questions/14745210
复制相似问题