python连接elasticsearch实战(附完整代码)

python连接elasticsearch

from elasticsearch import Elasticsearch
from elasticsearch.helpers import scan  
   
ES_HOSTS = [{'host': '', 'port': 9200, 'scheme': 'http'}] 
es = Elasticsearch(hosts=ES_HOSTS, basic_auth=('账号', '密码'))  
  
# 检查连接是否成功  
if es.ping():  
    print('Yeah Connected!')  
else:  
    print('Oh no :(')

#自定义一个要匹配查询的列表
list_a=['','','','']
#这里举例,查找A项在list_a里的所有项
query_test = {  
    "query": {  
        "terms": {  
            "A": list_a
        }  
    }  
}

# 执行搜索请求  
search_response = es.search(index="索引名", body=query_test )
  
# 检查是否有hits  
if search_response['hits']['hits']:  
    # 处理响应结果  
    for hit in search_response['hits']['hits']:  
        # 获取每个匹配的文档信息  
        source = hit['_source']  
        # 打印A和其他相关信息(假设a是文档中的一个字段)  
        # print(f"A: {source.get('A', '未知')}")
        print(source)

else:  
    print("没有匹配的文档")


#想把匹配结果以dataframe的格式输出
# 初始化一个列表来存储数据  
data = []  
  
# 检查聚合是否存在于响应中  
if 'aggregations' in search_response and 'A_groups' in search_response['aggregations']:  
    # 遍历聚合结果  
    for bucket in search_response['aggregations']['A_groups']['buckets']:  
        # 提取需要的数据并添加到列表中  
        row = {  
            'A': bucket['key'], 
            'B': bucket['b_count']['value'] 
        }  
        data.append(row)  
  
# 使用pandas的DataFrame构造函数创建一个DataFrame  
df= pd.DataFrame(data)

# 最后 检查hits的总数  
if 'hits' in search_response and 'total' in search_response['hits']:  
    total_hits = search_response['hits']['total']  
    if isinstance(total_hits, dict) and 'value' in total_hits:  
        total_hits = total_hits['value']  
    else:  
        pass  
    print(f"Total hits: {total_hits}")  
else:  
    print("没有搜索响应或hits信息")

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值