安裝:pip install elasticsearch
Python操作ES查詢時,如search操作,源碼:search(self, index=None, doc_type=None, body=None, params=None)
其中index,doc_type,body都好理解,與平時查詢時的一樣,body為DSL查詢語句,
params即在URL后添加的參數,使用 curl 查詢時URL參數如:?size=0 但在python中使用時賦值方式需要將其設為字典,如:params={“size":0},否則查詢時會無法解析
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))