安裝:pip install elasticsearchpython
Python操做ES查詢時,如search操做,源碼:search(self, index=None, doc_type=None, body=None, params=None)json
其中index,doc_type,body都好理解,與平時查詢時的同樣,body爲DSL查詢語句,curl
params即在URL後添加的參數,使用 curl 查詢時URL參數如:?size=0 但在python中使用時賦值方式須要將其設爲字典,如:params={「size":0},不然查詢時會沒法解析elasticsearch
params參數源碼註釋以下
:arg params: dictionary of query parameters, will be handed over to the
underlying :class:`~elasticsearch.Connection` class for serialization
dsl = { 'query': { 'match': { 'title': 'xxxtitle' } } } es = Elasticsearch() result = es.search(index='news', doc_type='politics', body=dsl,params={"size":0}) print(json.dumps(result, indent=2, ensure_ascii=False))