ToB企服应用市场:ToB评测及商务社交产业平台

标题: ES检索elasticsearch实现python库方法 [打印本页]

作者: 三尺非寒    时间: 昨天 11:19
标题: ES检索elasticsearch实现python库方法
1. 毗连到 Elasticsearch 集群

  1. from elasticsearch import Elasticsearch
  2. # 连接到 Elasticsearch 集群
  3. es = Elasticsearch([{'host': 'localhost', 'port': 9200}])
  4. # 检查连接是否成功
  5. if es.ping():
  6.     print('Connected to Elasticsearch')
  7. else:
  8.     print('Could not connect to Elasticsearch')
复制代码
2. 简单的全文搜索

  1. # 定义搜索请求体
  2. search_body = {
  3.     "query": {
  4.         "match": {
  5.             "title": "Elasticsearch Tutorial"
  6.         }
  7.     }
  8. }
  9. # 执行搜索
  10. index_name = "your_index_name"
  11. response = es.search(index=index_name, body=search_body)
  12. # 处理搜索结果
  13. for hit in response['hits']['hits']:
  14.     print(f"Document ID: {hit['_id']}, Score: {hit['_score'
复制代码
免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。




欢迎光临 ToB企服应用市场:ToB评测及商务社交产业平台 (https://dis.qidao123.com/) Powered by Discuz! X3.4