1. 请求elasticsearch 返回指定字段

  a._source指定需要返回的字段

{

  "_source":['title','id','desc'],
  "from":10,
  "size":100,

}
{
    "_source":{
        "includes":["title","url","id"],
        "excludes":["desc"]
    }
}

 

  b.其中includes代表需要返回的字段,excludes代表不要返回的字段

  c.直接在请求url带上需要查询参数

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

4.对_source的字段进行过滤

{
  "hits" : {
    "hits" : [ {
      "_source":{"title":"Book #2"}
    }, {
      "_source":{"title":"Book #1"}
    }, {
      "_source":{"title":"Book #3"}
    } ]
  }
}


_search?_source=goodsId,uri

_search?fields=goodsId,uri

2.python 对接elassearch ,指定返回字段

  1.实例化的es客户端,然后调用search方法,传入参数,params


from elasticsearch import Elasticsearch
es=Elasticseach(xxxx) es.search(params={"_source":"title,id,desc,url"})

   注:这边调用的是包中的search方法,和postman不一样的是,_source的值是一个z字符串,不同字段用逗号隔开,而post满是一个列表

   2.也是调用Elasticsearch的search方法,传入参数不是param,而直接是_source字段

   2.也是调用Elasticsearch的search方法,传入参数不是param,而直接是_source字段

print(es.search(index='person', body={"query": {"match": {"age": "19"}}}, _source=['name']))

  结果:

复制代码
{'_shards': {'failed': 0, 'skipped': 0, 'successful': 1, 'total': 1},
 'hits': {'hits': [{'_id': 'xFznIXIBMTX0DMkCyicV',
                    '_index': 'person',
                    '_score': 1.0,
                    '_source': {'name': 'lisi'},
                    '_type': 'male'}],
          'max_score': 1.0,
          'total': {'relation': 'eq', 'value': 1}},
 'timed_out': False,
 'took': 1}
复制代码