ToB企服应用市场:ToB评测及商务社交产业平台
标题:
ES检索elasticsearch实现python库方法
[打印本页]
作者:
三尺非寒
时间:
昨天 11:19
标题:
ES检索elasticsearch实现python库方法
1. 毗连到 Elasticsearch 集群
from elasticsearch import Elasticsearch
# 连接到 Elasticsearch 集群
es = Elasticsearch([{'host': 'localhost', 'port': 9200}])
# 检查连接是否成功
if es.ping():
print('Connected to Elasticsearch')
else:
print('Could not connect to Elasticsearch')
复制代码
2. 简单的全文搜索
# 定义搜索请求体
search_body = {
"query": {
"match": {
"title": "Elasticsearch Tutorial"
}
}
}
# 执行搜索
index_name = "your_index_name"
response = es.search(index=index_name, body=search_body)
# 处理搜索结果
for hit in response['hits']['hits']:
print(f"Document ID: {hit['_id']}, Score: {hit['_score'
复制代码
免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。
欢迎光临 ToB企服应用市场:ToB评测及商务社交产业平台 (https://dis.qidao123.com/)
Powered by Discuz! X3.4