Filter out metadata fields and only return source fields in elasticsearch

后端 未结 4 916
时光取名叫无心
时光取名叫无心 2020-12-05 04:44

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 wou

4条回答
  •  谎友^
    谎友^ (楼主)
    2020-12-05 05:09

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

提交回复
热议问题