> For the complete documentation index, see [llms.txt](https://xiaoxiami.gitbook.io/elasticsearch/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://xiaoxiami.gitbook.io/elasticsearch/ji-chu/mapping/322meta-fieldsff08-yuan-zi-duan-ff09/3223all-field-names-yuan-zi-duan.md).

# \_all, \_field\_names元字段

## Indexing meta-fields（索引的元字段）

| \_all          | 一个索引其他全部字段值的全能字段。 |
| -------------- | ----------------- |
| \_field\_names | 文档中包含非空值的所有字段。    |

### \_all field

&#x8BE5;**\_all** 字段是一个特殊的全能字段，它将所有其他字段的值连接成一个大字符串，使用空格作为分隔符，然后对其进行分词和索引，但不存储。 这意味着它可以被检索，但不能取回。

&#x8BE5;**\_all** 字段允许您在文档中搜索，并不需要知道哪个字段包含该值。 这使得它成为开始使用新数据集时的有用选项。 例如 :

```
curl -XPUT 'localhost:9200/my_index/user/1?pretty' -H 'Content-Type: application/json' -d' # 1
{
  "first_name":    "John",
  "last_name":     "Smith",
  "date_of_birth": "1970-10-24"
}
'
curl -XGET 'localhost:9200/my_index/_search?pretty' -H 'Content-Type: application/json' -d'
{
  "query": {
    "match": {
      "_all": "john smith 1970"
    }
  }
}
'
```

| 1 | 该 \_all 字段包含项: \[ "john", "smith", "1970", "10", "24" ] |
| - | ------------------------------------------------------- |

> 注意 :
>
> 所有值被视为字符串
>
> 上述示例中的**date\_of\_birth**字段被视为**date**字段，因此将索引表示**1970-10-24 00:00:00 UTC**的单个项。 然而，**\_all**字段将所有值视为字符串，因此日期值被索引成三个字符串项 : “**1970**”，“**24**”，“**10**”。\
> 注意，**\_all**字段将每个字段的原始值作为字符串组合。 它不会组合来自每个字段的项。

&#x8BE5;**\_all**字段是一个文本字段，并接受与其他字符串字段相同的参数，包括**analyzer**，**term\_vectors**，**index\_options** 和**store**。

&#x8BE5;**\_all**字段是有用的，特别是当使用简单的过滤来探索新的数据时。 但是，通过将字段值连接成一个大字符串，**\_all**字段将丢失短字段（更相关）和长字段（较不相关）之间的区别。 对于搜索相关性很重要的用例，最好专门查询各个字段。

&#x8BE5;**\_all**字段并不轻量 : 它需要额外的**CPU** 并使用更多的磁盘空间。 如果不需要，它可以完全禁用或个别字段定制。

#### 在查询中使用 \_all 字段 <a href="#idallfield-zai-cha-xun-zhong-shi-yong-all-zi-duan" id="idallfield-zai-cha-xun-zhong-shi-yong-all-zi-duan"></a>

**query\_string**和**simple\_query\_string**查询默认查&#x8BE2;**\_all**字段，除非指定了另一个字段 :

```
 curl -XGET 'localhost:9200/_search?pretty' -H 'Content-Type: application/json' -d'
{
  "query": {
    "query_string": {
      "query": "john smith 1970"
    }
  }
}
'
```

同样的在[URI搜索请求](https://www.elastic.co/guide/en/elasticsearch/reference/current/search-uri-request.html)中 ?q= parameter（内部改写为query\_string）:

```
GET _search?q=john+smith+1970
```

其他查询（如 [match](https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-match-query.html) 和 [term](https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-term-query.html) 查询）要求您按照 [第一个示例](https://www.elastic.co/guide/en/elasticsearch/reference/current/mapping-all-field.html) 显式指&#x5B9A;**\_all** 字段。

#### 禁用 \_all 字段 <a href="#idallfield-jin-yong-all-zi-duan" id="idallfield-jin-yong-all-zi-duan"></a>

通过将**enabled** 设置为**false**，可以&#x5BF9;**\_all** 字段完全禁用：

```
 curl -XPUT 'localhost:9200/my_index?pretty' -H 'Content-Type: application/json' -d'
{
  "mappings": {
    "type_1": { # 1
      "properties": {...}
    },
    "type_2": { # 2
      "_all": {
        "enabled": false
      },
      "properties": {...}
    }
  }
}
'
```

| 1 | type\_1 中的 \_all 字段已启用。   |
| - | ------------------------- |
| 2 | type\_2 中的 \_all 字段已完全禁用. |

如&#x679C;**\_all**字段被禁用，则**URI** 搜索请求和**query\_string** 和**simple\_query\_string** 查询将无法将其用于查询（请参阅在查询中使&#x7528;**\_all** 字段）。 您可以将它们配置为使用与 **index.query.default\_field** 设置不同的字段 :

```
curl -XPUT 'localhost:9200/my_index?pretty' -H 'Content-Type: application/json' -d'
{
  "mappings": {
    "my_type": {
      "_all": {
        "enabled": false # 1
      },
      "properties": {
        "content": {
          "type": "text"
        }
      }
    }
  },
  "settings": {
    "index.query.default_field": "content" # 2
  }
}
'
```

| 1 | \_all字段对于my\_type类型是禁用的。            |
| - | ----------------------------------- |
| 2 | query\_string查询将默认查询此索引中的content字段。 |

#### 排除 \_all 的字段 <a href="#idallfield-pai-chu-all-de-zi-duan" id="idallfield-pai-chu-all-de-zi-duan"></a>

可以使用 [include\_in\_all](https://www.elastic.co/guide/en/elasticsearch/reference/current/include-in-all.html) 设置&#x4ECE;**\_all**字段中包含或排除各个字段。

#### 索引提升和 \_all 字段 <a href="#idallfield-suo-yin-ti-sheng-he-all-zi-duan" id="idallfield-suo-yin-ti-sheng-he-all-zi-duan"></a>

使用 [boost](https://www.elastic.co/guide/en/elasticsearch/reference/current/mapping-boost.html) 参数，索引时可以提升个别字段。**\_all**字段考虑到这些提升 :

```
curl -XPUT 'localhost:9200/myindex?pretty' -H 'Content-Type: application/json' -d'
{
  "mappings": {
    "mytype": {
      "properties": {
        "title": { # 1
          "type": "text",
          "boost": 2
        },
        "content": { # 2
          "type": "text"
        }
      }
    }
  }
}
'
```

| 1 , 2 | 当查&#x8BE2;**\_all**字段时，源于**title**字段的字符是源于**content**字段的字符的两倍相关。 |
| ----- | ---------------------------------------------------------------- |

> 警告 :
>
> 使&#x7528;**\_all**字段的在索引时**boost**对查询性能有显着的影响。 通常更好的解决方案是单独查询字段，可在查询时**boost**。

#### 自定义 \_all 字段 <a href="#idallfield-zi-ding-yi-all-zi-duan" id="idallfield-zi-ding-yi-all-zi-duan"></a>

虽然每个索引只有一&#x4E2A;**\_all**字段，但**copy\_to** 参数允许创建多个自定&#x4E49;**\_all** 字段。 例如，**first\_name** 和**last\_name** 字段可以组合到**full\_name** 字段中 :

```
curl -XPUT 'localhost:9200/myindex?pretty' -H 'Content-Type: application/json' -d'
{
  "mappings": {
    "mytype": {
      "properties": {
        "first_name": {
          "type":    "text",
          "copy_to": "full_name" # 1
        },
        "last_name": {
          "type":    "text",
          "copy_to": "full_name" # 2
        },
        "full_name": {
          "type":    "text"
        }
      }
    }
  }
}
'
curl -XPUT 'localhost:9200/myindex/mytype/1?pretty' -H 'Content-Type: application/json' -d'
{
  "first_name": "John",
  "last_name": "Smith"
}
'
curl -XGET 'localhost:9200/myindex/_search?pretty' -H 'Content-Type: application/json' -d'
{
  "query": {
    "match": {
      "full_name": "John Smith"
    }
  }
}
'
```

| 1 ， 2 | **first\_name**和**last\_name**值将复制到**full\_name**字段。 |
| ----- | ---------------------------------------------------- |

#### 高亮显示和 \_all 字段 <a href="#idallfield-gao-liang-xian-shi-he-all-zi-duan" id="idallfield-gao-liang-xian-shi-he-all-zi-duan"></a>

如果原始字符串值可用，则字段只能用于 [高亮](https://www.elastic.co/guide/en/elasticsearch/reference/current/search-request-highlighting.html) 显示，从 [\_source](https://www.elastic.co/guide/en/elasticsearch/reference/current/mapping-source-field.html) 字段或作为存储字段。

**\_all**字段不&#x5728;**\_source** 字段中，默认情况下不存储，因此无法高亮显示。 有两个选项。存储[ \_all ](https://www.elastic.co/guide/en/elasticsearch/reference/current/mapping-all-field.html#all-field-store)字段或高亮显示 [原始字段 ](https://www.elastic.co/guide/en/elasticsearch/reference/current/mapping-all-field.html#all-highlight-fields)。

#### 存储 \_all 字段 <a href="#idallfield-cun-chu-all-zi-duan" id="idallfield-cun-chu-all-zi-duan"></a>

如果存储设置为**true**，则可以检索原始字段值，并且可以高亮显示 :

```
curl -XPUT 'localhost:9200/myindex?pretty' -H 'Content-Type: application/json' -d'
{
  "mappings": {
    "mytype": {
      "_all": {
        "store": true
      }
    }
  }
}
'
curl -XPUT 'localhost:9200/myindex/mytype/1?pretty' -H 'Content-Type: application/json' -d'
{
  "first_name": "John",
  "last_name": "Smith"
}
'
curl -XGET 'localhost:9200/_search?pretty' -H 'Content-Type: application/json' -d'
{
  "query": {
    "match": {
      "_all": "John Smith"
    }
  },
  "highlight": {
    "fields": {
      "_all": {}
    }
  }
}
'
```

当然，存&#x50A8;**\_all** 字段将使用更多的磁盘空间，因为由于它是其他字段的组合，所以可能会导致奇怪的高亮显示结果。

**\_all**字段也接受**term\_vector** 和**index\_options** 参数，允许使用快速的矢量荧光笔和贴片荧光笔。

#### 高亮显示原始字段 <a href="#idallfield-gao-liang-xian-shi-yuan-shi-zi-duan" id="idallfield-gao-liang-xian-shi-yuan-shi-zi-duan"></a>

您可以查&#x8BE2;**\_all**字段，但使用原始字段进行高亮显示，如下所示 :

```
curl -XPUT 'localhost:9200/myindex?pretty' -H 'Content-Type: application/json' -d'
{
  "mappings": {
    "mytype": {
      "_all": {}
    }
  }
}
'
curl -XPUT 'localhost:9200/myindex/mytype/1?pretty' -H 'Content-Type: application/json' -d'
{
  "first_name": "John",
  "last_name": "Smith"
}
'
curl -XGET 'localhost:9200/_search?pretty' -H 'Content-Type: application/json' -d'
{
  "query": {
    "match": {
      "_all": "John Smith" # 1
    }
  },
  "highlight": {
    "fields": {
      "*_name": { # 2
        "require_field_match": false # 3
      }
    }
  }
}
'
```

| 1 | 查询检&#x67E5;**\_all**字段以查找匹配的文档。                              |
| - | ------------------------------------------------------------ |
| 2 | &#x5728;**\_source**可以使用的两个名称字段上执行高亮显示。                      |
| 3 | 该查询未针对**name**字段运行，因此将**require\_field\_match**设置为**false**。 |

### \_field\_names field

&#x8BE5;**\_field\_names** 字段索引包含除 **null** 之外的任何值的文档中每个字段的名称。 该字段通过 [**exists**](https://www.elastic.co/guide/en/elasticsearch/reference/5.3/query-dsl-exists-query.html)查询以查找特定字段具有或不具有任何非空值的文档。

&#x8BE5;**\_field\_names** 字段的值可以在查询中被访问：

```
# Example documents
PUT my_index/my_type/1
{
  "title": "This is a document"
}

PUT my_index/my_type/2
{
  "title": "This is another document",
  "body": "This document has a body"
}
PUT my_index/my_type/3
{
"title": ""
}

PUT my_index/my_type/4?refresh=true
{
"title": null
}

GET my_index/_search
{
  "query": {
    "terms": {
      "_field_names": [ "title" ] # 1
    }
  }
}
```

| 1 | &#x5728;**\_field\_names**字段上查询（参考[**exists**](https://www.elastic.co/guide/en/elasticsearch/reference/5.3/query-dsl-exists-query.html)查询） |
| - | ------------------------------------------------------------------------------------------------------------------------------------------ |

通过以下返回结果,简单理解就是指定的字段,值不为null的所有文档.

返回结果:

```
{
  "took": 4,
  "timed_out": false,
  "_shards": {
    "total": 5,
    "successful": 5,
    "failed": 0
  },
  "hits": {
    "total": 3,
    "max_score": 0.6931472,
    "hits": [
      {
        "_index": "my_index",
        "_type": "my_type",
        "_id": "2",
        "_score": 0.6931472,
        "_source": {
          "title": "This is another document",
          "body": "This document has a body"
        }
      },
      {
        "_index": "my_index",
        "_type": "my_type",
        "_id": "1",
        "_score": 0.2876821,
        "_source": {
          "title": "This is a document"
        }
      },
      {
        "_index": "my_index",
        "_type": "my_type",
        "_id": "3",
        "_score": 0.2876821,
        "_source": {
          "title": ""
        }
      }
    ]
  }
}
```
