ES检索elasticsearch实现python库方法

打印 上一主题 下一主题

主题 904|帖子 904|积分 2712

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企服之家,中国第一个企服评测及商务社交产业平台。
回复

使用道具 举报

0 个回复

倒序浏览

快速回复

您需要登录后才可以回帖 登录 or 立即注册

本版积分规则

三尺非寒

金牌会员
这个人很懒什么都没写!
快速回复 返回顶部 返回列表