三尺非寒 发表于 2025-3-5 11:19:42

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企服之家,中国第一个企服评测及商务社交产业平台。
页: [1]
查看完整版本: ES检索elasticsearch实现python库方法