简体   繁体   English

如何在Elasticsearch索引中搜索值为字段为空数组的文档?

[英]How to search for documents in an elasticsearch index having value as empty array for a field?

I need to get documents in an elasticseach index which have empty array for a field (which can be nested)? 我需要在elasticseach索引中获取文档,该文档的字段为空数组(可以嵌套)吗?

Doing it the below way is not working 按以下方式进行操作不起作用

{
  "query": {
    "filter": {
      "script": {
        "script": "doc['arrayField'].length > 0"
      }
    }
  } 
}

or 要么

{
  "query": {
    "bool": { 
      "must": { "match_all": {} },
      "filter": { "term": { "arrayField": [] }}
    }
  } 
}

It seems like I have to use the filter feature, alongwith some script but can't figure out how. 似乎我必须使用过滤器功能以及一些脚本,但无法弄清楚该怎么做。

To get documents with the empty array we find the length of the field and see if it's length is less than 1. Please, the working query below. 要获取具有空数组的文档,我们需要找到字段的长度,并查看其长度是否小于1。请使用下面的有效查询。

"query": {
        "bool" : {
            "must" : {
                "script" : {
                    "script" : {
                        "inline": "doc['arrayField'].length < 1",
                        "lang": "painless"
                     }
                }
            }
        }
    }

Let me know if you find any issues. 如果您发现任何问题,请告诉我。
Thanks 谢谢

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Elasticsearch:根据文本字段中搜索字符串的索引值对文档进行排序 - Elasticsearch: Sort the Documents on the index value of the search string in a text field 如何在elasticsearch中搜索具有冒号(:)值的字段 - How to search a field having a colon(:) value in elasticsearch 在ElasticSearch中按索引搜索数组值 - search array value by index in elasticsearch 在 ElasticSearch 上搜索具有空数组字段的文档 - Search document with empty array field, on ElasticSearch 如何在Elasticsearch中按数组字段搜索? - How to search by array field in Elasticsearch? 在索引文档 elasticsearch 中搜索文本 - search a text in index documents elasticsearch 在Elasticsearch中排除空数组字段-但包括缺少该字段的文档 - Exclude empty array fields - but include documents missing the field - in elasticsearch PHP Elasticsearch从索引中的所有文档中获取字段的值 - PHP Elasticsearch GET value of a field from all documents in index 如何从其他文档(ElasticSearch)中按字段值获取嵌套文档? - How to get nested documents by field value from other documents (ElasticSearch)? 如何从索引中所有可用文档的弹性搜索数组中删除空字符串? - How to remove empty strings from elastic search array in all available documents in an index?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM