如何创建Elasticsearch curl查询以获取非空且非空("")的字段值,
这是mysql查询:
select field1 from mytable where field1!=null and field1!="";
Run Code Online (Sandbox Code Playgroud)
DrT*_*ech 65
空值和空字符串都不会导致索引值,在这种情况下,您可以使用exists过滤器
curl -XGET 'http://127.0.0.1:9200/test/test/_search?pretty=1' -d '
{
"query" : {
"constant_score" : {
"filter" : {
"exists" : {
"field" : "myfield"
}
}
}
}
}
'
Run Code Online (Sandbox Code Playgroud)
或者与(例如)对该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"
}
}
}
}
}
'
Run Code Online (Sandbox Code Playgroud)
Zac*_*ach 24
在Bool过滤器的Must-Not部分中包装缺少的过滤器.它只返回字段存在的文档,如果将"null_value"属性设置为true,则显式为非null的值.
{
"query":{
"filtered":{
"query":{
"match_all":{}
},
"filter":{
"bool":{
"must":{},
"should":{},
"must_not":{
"missing":{
"field":"field1",
"existence":true,
"null_value":true
}
}
}
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
cav*_*llo 20
正如@luqmaan在评论中指出的那样,文档说过滤器exists 不会过滤掉空字符串,因为它们被认为是非空值.
所以添加@ DrTech的答案,为了有效地过滤掉null和空字符串值,你应该使用这样的东西:
{
"query" : {
"constant_score" : {
"filter" : {
"bool": {
"must": {"exists": {"field": "<your_field_name_here>"}},
"must_not": {"term": {"<your_field_name_here>": ""}}
}
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
小智 18
在elasticsearch 5.6上,我必须使用下面的命令来过滤掉空字符串:
GET /_search
{
"query" : {
"regexp":{
"<your_field_name_here>": ".+"
}
}
}
Run Code Online (Sandbox Code Playgroud)
在 5.6.5 中对我有用的唯一解决方案是 bigstone1998 的正则表达式答案。出于性能原因,我不希望使用正则表达式搜索。我相信其他解决方案不起作用的原因是因为将分析标准字段,因此没有空字符串标记可以否定。存在查询本身也无济于事,因为空字符串被认为是非空的。
如果您不能更改索引,正则表达式方法可能是您唯一的选择,但如果您可以更改索引,那么添加关键字子字段将解决问题。
在索引的映射中:
"myfield": {
"type": "text",
"fields": {
"keyword": {
"ignore_above": 256,
"type": "keyword"
}
}
}
Run Code Online (Sandbox Code Playgroud)
然后你可以简单地使用查询:
{
"query": {
"bool": {
"must": {
"exists": {
"field": "myfield"
}
},
"must_not": {
"term": {
"myfield.keyword": ""
}
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
请注意.keywordmust_not 组件中的 。
您可以通过布尔查询以及must和must_not的组合来做到这一点,如下所示:
GET index/_search
{
"query": {
"bool": {
"must": [
{"exists": {"field": "field1"}}
],
"must_not": [
{"term": {"field1": ""}}
]
}
}
}
Run Code Online (Sandbox Code Playgroud)
我在Kibana中使用Elasticsearch 5.6.5进行了测试。
小智 6
您可以使用不过滤丢失.
"query": {
"filtered": {
"query": {
"match_all": {}
},
"filter": {
"not": {
"filter": {
"missing": {
"field": "searchField"
}
}
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
以下是检查多个字段是否存在的查询示例:
{
"query": {
"bool": {
"filter": [
{
"exists": {
"field": "field_1"
}
},
{
"exists": {
"field": "field_2"
}
},
{
"exists": {
"field": "field_n"
}
}
]
}
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
76224 次 |
| 最近记录: |