python 查询elstaticsearch数据
要查询elasticsearch数据,您需要使用elasticsearch-py库。
首先,您需要安装elasticsearch-py库,可以使用以下命令:
pip install elasticsearch
然后,您需要连接到elasticsearch服务器:
from elasticsearch import Elasticsearch
es = Elasticsearch(['localhost'], port=9200)
现在,您可以使用search方法来查询数据:
res = es.search(index="my_index", body={"query": {"match_all": {}}})
在这个例子中,我们使用了match_all查询来获取所有文档。您可以使用不同的查询类型来获取特定的文档。
您还可以添加其他参数来定义查询的范围、大小等:
res = es.search(index="my_index", body={"query": {"match_all": {}}, "from": 0, "size": 10})
这个例子中,我们添加了from和size参数来定义查询的范围和大小。
最后,您可以遍历查询结果并获取需要的数据:
for hit in res['hits']['hits']:
print(hit['_source'])
在这个例子中,我们遍历了查询结果,并打印了每个文档的源数据
原文地址: https://www.cveoy.top/t/topic/ht9x 著作权归作者所有。请勿转载和采集!