# \_source,\_size 元字段

## Document source meta-fields（文档源的元字段）

| \_source | 原始的JSON 字符串表示文档的正文。                                                                                                      |
| -------- | ------------------------------------------------------------------------------------------------------------------------ |
| \_size   | \_source 字段的大小（以字节为单位），由 [mapper-size](https://www.elastic.co/guide/en/elasticsearch/plugins/5.3/mapper-size.html) 插件提供。 |

### \_source field

该 \_source 字段包含在索引时传递的原始 JSON 文档正文。该 \_source 字段本身不被索引（因此是不可搜索的），但它被存储，以便在执行撷取请求时可以返回，例如 get 或 search 。

### 禁用\_source字段 <a href="#idsourcefield-jin-yong-source-zi-duan" id="idsourcefield-jin-yong-source-zi-duan"></a>

虽然很方便，但是 **\_source** 字段确实在索引中有不小的存储开销。 因此，可以使用如下方式禁用 :

```
curl -XPUT 'localhost:9200/tweets?pretty' -H 'Content-Type: application/json' -d'
{
  "mappings": {
    "tweet": {
      "_source": {
        "enabled": false
      }
    }
  }
}
'
```

> 警告 :
>
> #### 在禁用\_source 字段之前请考虑
>
> 用户经常禁&#x7528;**\_source**字段而不考虑后果，然后为此后悔。 如&#x679C;**\_source**字段不可用，则不支持许多功能 ：
>
> * [**update**](https://www.elastic.co/guide/en/elasticsearch/reference/5.3/docs-update.html)，[**update\_by\_query**](https://www.elastic.co/guide/en/elasticsearch/reference/5.3/docs-update-by-query.html)，[**reindex**](https://www.elastic.co/guide/en/elasticsearch/reference/5.3/docs-reindex.html) **APIs**.
> * 高亮
> * 将索引从一 个 **lasticsearch** 索引 **reindex**（重索引） 到另一个索引的能力，以更改映射或分析，或将索引升级到新的主要版本。
> * 通过查看索引时使用的原始文档来调试查询或聚合的能力。
> * 潜在的未来可能会自动修复索引损坏的能力。
>
> 提示:
>
> 如果磁盘空间是瓶颈，应是增加压缩级别而不是禁&#x7528;**\_source**。

### The metrics use case(指标用例) <a href="#id-_sourcefield-themetricsusecase" id="id-_sourcefield-themetricsusecase"></a>

指标用例与其他基于时间或日志记录的用例不同，因为有许多小型文档只包含数字，日期或关键字。

没有更新，没有高亮的请求，并且数据快速老化，因此不需要**reindex**。

搜索请求通常使用简单查询来按日期或标签过滤数据集，结果作为聚合返回。

在这种情况下，禁&#x7528;**\_source**字段将节省空间并减少**I/O**。

建议在**metrics case**下禁用[**\_all**](https://www.elastic.co/guide/en/elasticsearch/reference/5.3/mapping-all-field.html)字段。

### 包含 / 排除 \_source 中的字段 <a href="#idsourcefield-bao-han-pai-chu-source-zhong-de-zi-duan" id="idsourcefield-bao-han-pai-chu-source-zhong-de-zi-duan"></a>

专家级功能是在文档索引后但在 **\_source** 字段存储之前修剪 **\_source** 字段的内容。

> 警告:
>
> &#x4ECE;**\_source**中删除字段与禁&#x7528;**\_source**有类似的缺点，尤其是您不能将文档从一个**Elasticsearch**索引重新索引到另一个。
>
> 考虑使用[**source filtering**](https://www.elastic.co/guide/en/elasticsearch/reference/5.3/search-request-source-filtering.html)（源代码）过滤。

可以使用**includes**/**excludes** 参数（也可以接受通配符），如下所示 :

```
curl -XPUT 'localhost:9200/logs?pretty' -H 'Content-Type: application/json' -d'
{
  "mappings": {
    "event": {
      "_source": {
        "includes": [
          "*.count",
          "meta.*"
        ],
        "excludes": [
          "meta.description",
          "meta.other.*"
        ]
      }
    }
  }
}
'
curl -XPUT 'localhost:9200/logs/event/1?pretty' -H 'Content-Type: application/json' -d'
{
  "requests": {
    "count": 10,
    "foo": "bar" # 1
  },
  "meta": {
    "name": "Some metric",
    "description": "Some metric description", # 2
    "other": {
      "foo": "one", # 3
      "baz": "two" # 4
    }
  }
}
'
curl -XGET 'localhost:9200/logs/event/_search?pretty' -H 'Content-Type: application/json' -d'
{
  "query": {
    "match": {
      "meta.other.foo": "one" # 5
    }
  }
}
'
```

| 1 | 这些字段将从存储&#x7684;**\_source**字段中删除。        |
| - | ----------------------------------------- |
| 5 | 我们仍然可以搜索这个字段，即使它不在存储&#x7684;**\_source**。 |


---

# 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/mapping/322meta-fieldsff08-yuan-zi-duan-ff09/3222source-size-yuan-zi-duan.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.
