# Fields / Script Fields / Doc value Fields

## Fields

> 警告：
>
> stored\_fields 参数是关于显式标记为存储在映射中的字段，默认情况下关闭，通常不推荐。 使用源过滤来选择要返回的原始源文档的子集。

允许有选择地加载搜索匹配所表示的每个文档的特定存储字段。

```
GET /_search
{
    "stored_fields" : ["user", "postDate"],
    "query" : {
        "term" : { "user" : "kimchy" }
    }
}
```

可用于从文档加载所有存储的字段。

空数组只会为每个匹配返回 \_id 和 \_type，例如：

```
GET /_search
{
    "stored_fields" : [],
    "query" : {
        "term" : { "user" : "kimchy" }
    }
}
```

如果请求的字段未存储（存储映射设置为 false ），它们将被忽略。

从文档本身获取的存储字段值总是作为数组返回。 相反，诸如 \_routing 和 \_parent 字段的元数据字段从不作为数组返回。

此外，只有叶子字段（leaf field）可以通过字段选项返回。 因此，无法返回对象字段，并且此类请求将失败。

脚本字段也可以自动检测并用作字段，所以像 \_source.obj1.field1 这样的东西可以使用，虽然不推荐，因为 obj1.field1 也会工作。

### Disable stored fields entirely

要禁用存储的字段（和元数据字段），请完全使用：`_none_`：

```
GET /_search
{
    "stored_fields": "_none_",
    "query" : {
        "term" : { "user" : "kimchy" }
    }
}
```

> Note:
>
> 如果使用 \_none\_ , \_source 和 version 参数不能激活 。

## Script Fields

允许为每次匹配返回脚本评估（基于不同的字段），例如：

```
GET /_search
{
    "query" : {
        "match_all": {}
    },
    "script_fields" : {
        "test1" : {
            "script" : {
                "lang": "painless",
                "inline": "doc['my_field_name'].value * 2"
            }
        },
        "test2" : {
            "script" : {
                "lang": "painless",
                "inline": "doc['my_field_name'].value * factor",
                "params" : {
                    "factor"  : 2.0
                }
            }
        }
    }
}
```

脚本字段可以用于未存储的字段（在上述情况下为 my\_field\_name ），并允许返回要返回的自定义值（脚本的计算值）。

脚本字段还可以访问实际的\_source文档索引并提取从中返回的特定元素（可以是 “对象” 类型）。 这里是一个例子：

```
GET /_search
    {
        "query" : {
            "match_all": {}
        },
        "script_fields" : {
            "test1" : {
                "script" : "_source.obj1.obj2"
            }
        }
    }
```

注意 `_source`关键字在这里用于导航 json 样模型。

了解 doc \['my\_field'].value 和 \_source.my\_field 之间的区别很重要。 第一个，使用 doc 关键字，将导致该字段的术语被加载到内存（缓存），这将导致更快的执行，但更多的内存消耗。 此外，doc \[...] 符号只允许简单的有价值的字段（不能从它返回一个 json 对象），并且只对非分析或单个术语的字段有意义。

## Doc value Fields

允许返回每个匹配的字段的 doc value 表示形式，例如：

```
GET /_search
{
    "query" : {
        "match_all": {}
    },
    "docvalue_fields" : ["test1", "test2"]
}
```

Doc value Fields 可以用于未存储的字段。

请注意，如果 fields 参数指定没有 docvalues 的字段，它将尝试从 fielddata 缓存加载值，使得该字段的术语被加载到内存（缓存），这将导致更多的内存消耗。


---

# 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/33-apis/343sou-suo-apis-search-apis/request-body-searchwei-wan-621029/fields-script-fields-doc-value-fields.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.
