使用python的request查询elasticsearch文档数据
以下是使用Python的requests库查询Elasticsearch文档数据的示例代码:
import requests
# Elasticsearch服务器的URL
url = 'http://localhost:9200'
# 查询条件
query = {
"query": {
"match": {
"title": "Python"
}
}
}
# 发送POST请求查询数据
response = requests.post(url + '/my_index/_search', json=query)
# 获取查询结果
if response.status_code == 200:
data = response.json()
hits = data.get('hits', {}).get('hits', [])
for hit in hits:
print(hit.get('_id'), hit.get('_source'))
else:
print('查询失败')
在代码中,使用POST请求发送查询条件到Elasticsearch服务器,并将查询结果解析为JSON格式。然后,从查询结果中获取文档的ID和内容,并打印输出。如果查询失败,则打印错误信息。
请注意,这只是一个简单的示例,您需要根据自己的需求进行修改和调整。
原文地址: https://www.cveoy.top/t/topic/bUis 著作权归作者所有。请勿转载和采集!