首页 文章

过滤掉元数据字段,仅返回elasticsearch中的源字段

提问于
浏览
23

有没有办法告诉elasticsearch不返回任何元数据?目前我可以选择要在源中返回哪些字段 . 但我只想要源代码中的字段 . 我宁愿没有返回元数据,因为我不需要它,并会节省一些不必要的解析和传输等 .

我找到了Elasticsearch - how to return only data, not meta information?较旧的问题,有人评论说当时不可能这样做 . 想知道这个功能是否已添加或仍然缺失?

4 回答

  • 0

    如果我们知道它并不困难:)

    http://localhost:9200/***{index_name}***/***{type}***/_search?pretty&filter_path=took,hits.hits._id,hits.hits._score,**hits.hits._source**
    
  • 4

    我在查询中不知道这样的选项 . 可以在get by Id请求中执行此操作 .

    /{index}/{type}/{id}/_source
    

    http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/docs-get.html#_source

  • 6

    response_filtering

    所有REST API都接受filter_path参数,该参数可用于减少elasticsearch返回的响应 . 此参数采用逗号分隔的过滤器列表,用点表示法表示:

    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
          }
        ]
      }
    }
    

    在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
    

    如果要过滤_source字段,则应考虑将已存在的_source参数(请参阅获取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"}
        } ]
      }
    }
    
  • 20

    filter_path (响应过滤)对elasticsearch的1.5版没有任何影响 .

    除非该选项具有不同的名称或已在文档中移动,否则它首先在version 1.6中添加 .

相关问题