ES 简单查询方式:es.search #批量查询方式,:scan

博客围绕ES查询展开,给出了ES查询的代码示例。定义了批量查询函数do_scroll2,创建了clusterModel类用于初始化连接ES,还展示了简单查询和批量查询的具体实现,包括查询条件设置和结果处理。

es查询方式
#!/usr/bin/env python
# -*- coding:utf-8 -*-

from elasticsearch import Elasticsearch
from elasticsearch5.helpers import scan

def do_scroll2(es, index, body):
    try:
        # raises an exception (ScanError) if an error is encountered (some shards fail to execute).
        # 设置为False是为了考虑存在shard故障
        return scan(client=es, query=body, index=index, scroll=u"10m", size=5000, request_timeout=180,
                    raise_on_error=False)
    except Exception as e:
        print("ERROR:",e)
        return
class clusterModel:
    # 初始化 连接es
    def __init__(self,ai_index):
        es_config = {
            "hosts": [
                "********:9200",
                "*******:9200",
                "******:9200"
            ],
            "http_auth": ['nihao', 'helllo'],
            
        }
        self.es = Elasticsearch(**es_config)
        self.ai_index = ai_index

   

    def get_vector_from_es2(self, user_id):
        print("AAAAAA")
        """
        s_body = {
              "query": {
                "bool": {
                  "must": [
                    {
                      "term": {
                        "userId.keyword": {
                          "value": "JINSH***"
                        }
                      }
                    }
                  ]
                }
              }
            }

        #简单查询方式:es.search
        doc = self.es.search(index=self.ai_index, body=s_body, request_timeout=30)
        print("BBBBBB",doc,len(doc["hits"]["hits"]))
        for ele in doc["hits"]["hits"]:
            print("CCCCC:",ele)
        """
        s_body = {
              "query": {
                "bool": {
                  "must": [
                    {
                      "term": {
                        "userId.keyword": {
                          "value": "JINS*****"
                        }
                      }
                    }
                  ]
                }
              }
            }
        #批量查询方式,:scan
        item_set = do_scroll2(self.es, index=self.ai_index, body=s_body)
        count = 0
        for each_record in item_set:
            source = each_record.get("_source")
            print("source:",source)
            user_id = source.get("userId")
            print("user_id:",user_id)
            count +=1
        print("count = ",count)

if __name__ == '__main__':
    ai_index = "**-22"
    modelObject  = clusterModel(ai_index)
    userId = "*****"
    print(userId,modelObject.ai_index)
    modelObject.get_vector_from_es2(userId)

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

小金子的夏天

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值