事实上,_source 字段就是1个贮存字段。
在 Elasticsearch 中,单独设置贮存字段不是1个好做法。完全的文档已被保存在 _source 字段中。
通常最好的办法会是使用 _source 参数来过滤你需要的字段。
必须设置所有字段:store:true
如果mapping中,对某个type,设置了 “_source”: {“enabled”: false},那末查询结果中就没有_source字段只能查到文档id。
如果mapping中,field设置了store为true,例如 “age”:{“type”:”integer”,”store”:true},那末就能够在查询时通过指定fields获得到指定的field的数据。
store默许false。
fielddata_fields 和 fields区分?
fielddata_fields同时还返回_source, fields不返回_source。
https://www.elastic.co/guide/en/elasticsearch/reference/2.1/search-request-fielddata-fields.html
https://www.elastic.co/guide/en/elasticsearch/reference/2.1/search-request-fields.html
REST:
https://www.elastic.co/guide/en/elasticsearch/reference/2.1/search-request-fields.html
{
"fields" : ["user", "postDate"],
"query" : {
"term" : { "user" : "kimchy" }
}
}
Java API
SearchRequestBuilder类
方法:addFields(String... fields)
或:addField(String field)
默许存储所有字段
PUT logs
{
"mappings": {
"event": {
"_source": {
"includes": [
"*.count",
"meta.*"
],
"excludes": [
"meta.description",
"meta.other.*"
]
}
}
}
}
GET /_search
{
"query": { "match_all": {}},
"_source": [ "title", "created" ]
}
//支持通配符
{
"_source": "obj.*",
"query" : {
"term" : { "user" : "kimchy" }
}
}
{
"_source": [ "obj1.*", "obj2.*" ],
"query" : {
"term" : { "user" : "kimchy" }
}
}
{
"_source": {
"include": [ "obj1.*", "obj2.*" ],
"exclude": [ "*.description" ]
},
"query" : {
"term" : { "user" : "kimchy" }
}
}