es python search 返回_elasticsearch返回指定字段

1. postman 请求elasticsearch 返回指定字段

1.直接在请求体当中,json 数据,对应的是一个列表

{

"_source":['title','id','desc'],

"from":10,

"size":100,

}

至于from和size是浅分页

2. 或者这样

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

}

}

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

3.直接在请求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}

]

}

}

对_source的字段进行过滤

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

} ]

}

}

这样也可以

_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字段

pprint(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}

3.不懂直接查询api接口

4.返回文档版本字段

GET /_search

{"version": true,"query": {"term" : { "user" : "kimchy"}

}

}

5.Script Field 用脚本来对命中的每个文档的字段进行运算后返回

GET /bank/_search

{"query": {"match_all": {}

},"script_fields": {"test1": {"script": {"lang": "painless","source": "doc['balance'].value * 2"}

},"test2": {"script": {"lang": "painless",

"source": "doc['age'].value * params.factor","params": {"factor": 2}

}

} }}

搜索结果:

{"took": 3,"timed_out": false,"_shards": {"total": 5,"successful": 5,"skipped": 0,"failed": 0

},"hits": {"total": 1000,"max_score": 1,"hits": [

{"_index": "bank","_type": "_doc","_id": "25","_score": 1,"fields": {"test1": [

],"test2": [

]

}

},

{"_index": "bank","_type": "_doc","_id": "44","_score": 1,"fields": {"test1": [

],"test2": [

]

}

}}

]

}

}

示例2

GET/bank/_search

{"query": {"match_all": {}

},"script_fields": {"ffx": {"script": {"lang": "painless","source": "doc['age'].value * doc['balance'].value"}

},"balance*2": {"script": {"lang": "painless","source": "params['_source'].balance*2"}

}

}

}

搜索结果:

{"took": 26,"timed_out": false,"_shards": {"total": 5,"successful": 5,"skipped": 0,"failed": 0

},"hits": {"total": 1000,"max_score": 1,"hits": [

{"_index": "bank","_type": "_doc","_id": "25","_score": 1,"fields": {"balance*2": [

],"ffx": [

]

}

},

{"_index": "bank","_type": "_doc","_id": "44","_score": 1,"fields": {"balance*2": [

],"ffx": [

]

}

},}

]

}

}

说明: params  _source 取 _source字段值

官方推荐使用doc,理由是用doc效率比取_source 高

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值