官网地址
一、source
文档_source在 Lucene 中存储为单个字段。这种结构意味着_source即使您只请求其中的一部分,也必须加载和解析整个对象
二、fields
POST my-index-000001/_search
{
"query": {
"match": {
"user.id": "kimchy"
}
},
"fields": [
"user.id",
"http.response.*",
{
"field": "@timestamp",
"format": "epoch_millis"
}
],
"_source": false
}
fields响应始终为每个字段返回一个值数组,即使_source中只有一个值。这是因为Elasticsearch没有专用的数组类型,任何字段都可能包含多个值。fields参数也不能保证数组值按特定顺序返回。
{
"hits" : {
"total" : {
"value" : 1,
"relation" : "eq"
},
"max_score" : 1.0,
"hits" : [
{
"_index" : "my-index-000001",
"_id" : "0",
"_score" : 1.0,
"_type" : "_doc",
"fields" : {
"user.id" : [
"kimchy"
],
"@timestamp" : [
"4098435132000"
],
"http.response.bytes": [
1070000
],
"http.response.status_code": [
200
]
}
}
]
}
}
三、Doc value fields
四、Script fields
五、总结
要检索搜索响应中的特定字段,请使用fields参数。因为它参考索引映射,所以fields参数提供了几个优于直接引用_source的优点。字段参数:
- 以与映射类型匹配的标准化方式返回每个值
- 接受多个字段和字段别名
- 格式化日期和空间数据类型 注意:(格式化的日期会显示的是UTC时间,没有找到时区设置的参数)
- 检索运行时字段值
- 返回脚本在索引时计算的字段