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

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

问题描述

可能是一个非常愚蠢的问题,检查弹性搜索中的文档的一个字段是否存在是最好的方法?我在文档中找不到任何东西。

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

例如,如果此文档没有字段/键价格,我不想返回

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


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

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

我可以做什么?

谢谢

推荐答案

你可以使用 存在过滤器 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
          ]
        }
      }
    }
  }
}

您还可以使用 缺少过滤器结合 bool / must_not 过滤器

You can also use the missing filter combined with a bool/must_not filter:

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

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

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