检查 Elasticsearch 文档中是否存在字段的最佳方法 [英] Best way to check if a field exist in an Elasticsearch document

查看:62
本文介绍了检查 Elasticsearch 文档中是否存在字段的最佳方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在elasticsearch中检查文档字段是否存在的最佳方法是什么?我在文档中找不到任何内容.

What is the best way to check if a field of a document in elasticsearch exists? I can't find anything in the documentation.

例如,如果此文档没有字段/键price"我不想在结果中返回.

For example if this document doesn't have the field/key "price" I don't want to return in the result.

{
    "updated": "2015/09/17 11:27:27",
     "name": "Eye Shadow",
     "format": "1.5 g / 0.05 oz",
}

我能做什么?

推荐答案

您可以使用 exists 过滤器 结合了 bool/must filter 像这样:

You can use the exists filter combined with a bool/must filter like this:

{
  "query": {
    "filtered": {
      "filter": {
        "bool": {
          "must": [
            {
              "exists": {
                "field": "price"
              }
            },
            ...     <-- your other constraints, if any
          ]
        }
      }
    }
  }
}

已弃用(自 ES5 起)您还可以使用 missing 过滤器 结合 bool/must_not 过滤器:

DEPRECATED (since ES5) You can also use the missing filter combined with a bool/must_not filter:

{
  "query": {
    "filtered": {
      "filter": {
        "bool": {
          "must_not": [
            {
              "missing": {
                "field": "price"
              }
            }
          ]
        }
      }
    }
  }
}

这篇关于检查 Elasticsearch 文档中是否存在字段的最佳方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆