# Exists 查询(非空值查询)

返回在原始字段中至少有一个非空值的文档：

```
GET /_search
{
    "query": {
        "exists" : { "field" : "user" }
    }
}
```

例如，这些文档都将匹配上面的查询：

```
{ "user": "jane" }
{ "user": "" } ①
{ "user": "-" } ②
{ "user": ["jane"] }
{ "user": ["jane", null ] } ③
```

①空字符串是**non-null**（非空值）。

②即使通过**standard analyzer**标准分析器也不会发出警告，原始字段也是非空的。

③至少需要一个**non-null**非空值。

这些文档将不会被上面的查询匹配到：

```
{ "user": null }
{ "user": [] } ①
{ "user": [null] } ②
{ "foo":  "bar" } ③
```

①这个字段没有任何值。

②至少需要一个 **non-null** 非空值。

③ user 字段完全丢失。

## null\_value mapping （非空值映射）

如果字段映射包括[**null\_value**](https://www.elastic.co/guide/en/elasticsearch/reference/current/null-value.html)（空值）设置，那么明确的**null**（空对象）将替换为指定的**null\_value**（空值）。

例如：user 字段映射如下：

```
"user": {
  "type": "text",
  "null_value": "_null_"
}
```

那么明确的**null**（空对象）将被索引为字符串 “\_null\_” ，并且以下文档将被**exists** （非空值）筛选器匹配：

```
{ "user": null }
{ "user": [null] }
```

但是，这些文档没有明确的**null**（空对象）值，在 user 字段中也没有值，并且将不能被 **exists** （非空值）筛选器匹配：

```
{ "user": [] }
{ "foo": "bar" }
```

## missing query （缺失查询）

&#x20;**missing query**（缺失查询）已被废弃，因为它可以方便的由**must\_not**子句中的**exists**查询替换，如下所示：

```
GET /_search
{
    "query": {
        "bool": {
            "must_not": {
                "exists": {
                    "field": "user"
                }
            }
        }
    }
}
```

此查询返回在 user 字段中没有值的文档。


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://xiaoxiami.gitbook.io/elasticsearch/ji-chu/35query-dsldslfang-shi-cha-8be229/354zhu-yu-ji-bie-cha-8be228-term-level-queries/exists-querywei-wan-621029.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
