有時候我們可能并不想返回所有的字段岳守,甚至連元數(shù)據(jù)字段需要的都很少婆廊,所以我們想精簡返回的結(jié)果帜讲。
方法所有的API都接受一個filter_path參數(shù)其弊,這個參數(shù)支持逗號分隔癞己,可以同時填寫多個值。
例如梭伐,如果只想要返回查詢的時間痹雅、事件的id和分值,可以像下面這樣:
curl -XGET 'localhost:9200/_search?pretty&filter_path=took,hits.hits._id,hits.hits._score'
{
"took" : 3,
"hits" : {
"hits" : [
{
"_id" : "3640",
"_score" : 1.0
},
{
"_id" : "3642",
"_score" : 1.0
}
]
}
}
也支持通配符*來忽略對某個字段的過濾:
curl -XGET 'localhost:9200/_nodes/stats?filter_path=nodes.*.ho*'
{
"nodes" : {
"lvJHed8uQQu4brS-SXKsNA" : {
"host" : "portable"
}
}
}
使用**則會忽略最大長度的路徑糊识,與Spring MVC的Url匹配差不多绩社。
curl 'localhost:9200/_segments?pretty&filter_path=indices.**.version'
{
"indices" : {
"movies" : {
"shards" : {
"0" : [ {
"segments" : {
"_0" : {
"version" : "5.2.0"
}
}
} ],
"2" : [ {
"segments" : {
"_0" : {
"version" : "5.2.0"
}
}
} ]
}
},
"books" : {
"shards" : {
"0" : [ {
"segments" : {
"_0" : {
"version" : "5.2.0"
}
}
} ]
}
}
}
}
注意,elasticsearch一般會直接返回一條數(shù)據(jù)的原始信息赂苗,即_source字段愉耙。如果要對_source進行過濾,可以參考下面的用法:
curl -XGET 'localhost:9200/_search?pretty&filter_path=hits.hits._source&_source=title'
{
"hits" : {
"hits" : [ {
"_source":{"title":"Book #2"}
}, {
"_source":{"title":"Book #1"}
}, {
"_source":{"title":"Book #3"}
} ]
}
}