过滤掉元数据字段,只返回弹性搜索中的源字段 [英] Filter out metadata fields and only return source fields in elasticsearch
问题描述
我发现 Elasticsearch - 如何只返回数据,而不是元信息?老问题,有人评论说不可能做到这一点。想知道这个功能是否被添加或仍然缺少?
所有REST API接受可以用于
的 filter_path 参数,以减少elasticsearch返回的响应。此参数使用
逗号分隔的以点表示法表示的过滤器列表:
curl -XGET'localhost:9200 / _search?pretty& filter_path = taken,hits.hits._id,hits.hits._score'
{
取:3,
命中:{
hits:[
{
_id:3640,
_score:1.0
},
{
_id:3642,
_score:1.0
}
]
}
}
在python
def get_all(connection, index_name,type_name)
query = {
match_all:{}
}
result = connection.search(index_name,type_name,
{query:query},
filter_path = [took,hits.hits._id,hits.hits.score])
返回结果
如果要过滤_source字段,您应该考虑结合
已经存在的
_source 参数(有关详细信息,请参阅Get API)
和filter_path参数如下:
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}
}]
}
}
Is there a way to tell elasticsearch to not return any metadata? Currently I can select which fields I want to be returned in source. But I only want fields in source. I would prefer to not have the metadata returned as I dont need it and would save some unnecessary parsing and transport etc.
I found Elasticsearch - how to return only data, not meta information? older question where somebody commented that it wasnt possible to do it then. Wondering if this functionality has been added or is still missing?
All REST APIs accept a filter_path parameter that can be used to reduce the response returned by elasticsearch. This parameter takes a comma separated list of filters expressed with the dot notation:
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
}
]
}
}
In python
def get_all( connection, index_name, type_name ):
query = {
"match_all":{}
}
result = connection.search( index_name, type_name,
{"query": query},
filter_path= ["took", "hits.hits._id", "hits.hits.score"])
return result
If you want to filter _source fields, you should consider combining the already existing _source parameter (see Get API for more details) with the filter_path parameter like this:
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"}
} ]
}
}
这篇关于过滤掉元数据字段,只返回弹性搜索中的源字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!