过滤掉元数据字段,只返回弹性搜索中的源字段 [英] Filter out metadata fields and only return source fields in elasticsearch

查看:147
本文介绍了过滤掉元数据字段,只返回弹性搜索中的源字段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有办法告诉弹性搜索不返回任何元数据?目前我可以选择要在源中返回哪些字段。但我只想要来源的字段。我不喜欢没有返回的元数据,因为我不需要它,并将节省一些不必要的解析和传输等。



我发现 Elasticsearch - 如何只返回数据,而不是元信息?老问题,有人评论说不可能做到这一点。想知道这个功能是否被添加或仍然缺少?

解决方案

response_filtering


所有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?

解决方案

response_filtering

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆