Link Search Menu Expand Document Documentation Menu

Exists 查询

使用 exists 查询来搜索包含特定字段的文档。

在以下任何情况下,文档字段将不存在索引值:

  • 字段在映射中指定了 "index" : false
  • 源 JSON 中的字段为 null[]
  • 字段值的长度超过了映射中的 ignore_above 设置。
  • 字段值格式不正确,并且映射中定义了 ignore_malformed

在以下任何情况下,文档字段将存在索引值:

  • 该值是一个数组,包含一个或多个 null 元素以及一个或多个非 null 元素(例如,["one", null])。
  • 该值为空字符串("""-")。
  • 该值是自定义的 null_value,如字段映射中定义的。

示例

例如,考虑一个包含以下两个文档的索引:

PUT testindex/_doc/1
{
  "title": "The wind rises"
}

PUT testindex/_doc/2
{
  "title": "Gone with the wind",
  "description": "A 1939 American epic historical film"
}

以下查询搜索包含 description 字段的文档:

GET testindex/_search
{
  "query": {
    "exists": {
      "field": "description"
    }
  }
}

响应包含匹配的文档

{
  "took": 3,
  "timed_out": false,
  "_shards": {
    "total": 1,
    "successful": 1,
    "skipped": 0,
    "failed": 0
  },
  "hits": {
    "total": {
      "value": 1,
      "relation": "eq"
    },
    "max_score": 1,
    "hits": [
      {
        "_index": "testindex",
        "_id": "2",
        "_score": 1,
        "_source": {
          "title": "Gone with the wind",
          "description": "A 1939 American epic historical film"
        }
      }
    ]
  }
}

查找缺少索引值的文档

要查找缺少索引值的文档,您可以使用 must_not 布尔查询 和内部的 exists 查询。例如,以下请求搜索缺少 description 字段的文档:

GET testindex/_search
{
  "query": {
    "bool": {
      "must_not": {
        "exists": {
          "field": "description"
        }
      }
    }
  }
}

响应包含匹配的文档

{
  "took": 19,
  "timed_out": false,
  "_shards": {
    "total": 1,
    "successful": 1,
    "skipped": 0,
    "failed": 0
  },
  "hits": {
    "total": {
      "value": 1,
      "relation": "eq"
    },
    "max_score": 0,
    "hits": [
      {
        "_index": "testindex",
        "_id": "1",
        "_score": 0,
        "_source": {
          "title": "The wind rises"
        }
      }
    ]
  }
}

参数

该查询接受字段名称 (<field>) 作为顶层参数。

参数 数据类型 描述
提升 浮点数 一个浮点值,指定此字段对相关性得分的权重。高于 1.0 的值会增加字段的相关性。介于 0.0 和 1.0 之间的值会降低字段的相关性。默认值为 1.0。
剩余 350 字符

有问题?

想做贡献?