数据分析agent(九):es_client_manager.py 和分词器ik_max_word问题

python 复制代码
import asyncio
import random
from typing import Optional

from elasticsearch import AsyncElasticsearch




class ESClientManager:
    def __init__(self):

        self.client:Optional[AsyncElasticsearch]=None

        pass


    def _get_url(self):

        return f"http://127.0.0.1:9200"
        pass


    def init(self):

        self.client=AsyncElasticsearch(hosts=self._get_url())

        pass



    async  def close(self):
        if self.client:
            await self.client.close()
        pass


es_client_manager=ESClientManager()



if __name__ == '__main__':
    es_client_manager.init()
    client=es_client_manager.client

    index_name="product"

    # async def test_index():
    #     result=await client.indices.exists(index=index_name)
    #
    #     print(result)
    #
    #     await es_client_manager.close()
    #
    #
    #
    # asyncio.run(test_index())

    async def test_create():
        result = await client.indices.exists(index=index_name)

        if not result:
            result = await client.indices.create(
                index=index_name,
                mappings={
                    "properties": {
                        "name": {
                            "type": "text",
                            # "analyzer": "ik_max_word"  # 已修正拼写错误
                            "analyzer": "standard"  # 已修正拼写错误
                        },
                        "brand": {
                            "type": "keyword"
                        },
                        "price": {
                            "type": "float"
                        }
                    }
                }
            )
            print(result)

        # 无论索引是否已存在,都确保关闭连接
        await es_client_manager.close()


    #asyncio.run(test_create())

    async  def test_add_doc():
        result=await client.index(
            index=index_name,
            document={
                "name":"huawei华为手机meta80",
                "brand":"华为",
                "price":9222
            }
        )

        print(result)

        await es_client_manager.close()

    # asyncio.run(test_add_doc())

    async def test_add_docs():

        operations=[]

        goods_list=["小米手机116","小米汽车","华为手机meta80","华为汽车","苹果手机","苹果汽车","苹果iphone"]

        for good in goods_list:
            operations.append(
                {"index":{"_index":index_name}}
            )

            operations.append(
                {
                    "name":good,
                    "price":random.randint(1000,9999),
                    "brand":"小米"
                }
            )
        result = await client.bulk(operations=operations)


        print(result)

        await es_client_manager.close()

    asyncio.run(test_add_docs())

当 ES 和 kibana链接时,Ik_max_word失效问题:

E:\笔记2026项目\项目一_问数\1.docker镜像>docker exec -it elasticsearch /usr/share/elasticsearch/bin/elasticsearch-plugin list

analysis-ik

如果没有 analysis-ik

可以重新安装

测试 有时候 与本地 某些冲突有关

可以用python脚本 测试

或者:

相关推荐
生态博士的R笔记5 小时前
R语言广义可加模型:GAM与GAMM的非线性响应,附完整代码
数据分析
Aloudata9 小时前
连锁餐饮 Data Agent 怎么做?如何核实同类客户门店规模、应用场景与实施效果?
大数据·数据分析·指标平台·语义层
zfan5209 小时前
相关性技术分析
数据分析·spss
Elasticsearch11 小时前
Elastic 获得全新的 AWS 数据能力认证
elasticsearch
Elasticsearch12 小时前
Elastic 安全借助 NVIDIA Nemotron 为物理隔离的安全运营中心提供智能体人工智能
elasticsearch
Elasticsearch12 小时前
NVIDIA Nemotron 现已在 Elastic 推理服务(EIS)中可用
elasticsearch
岳涛12 小时前
考古|ES 5.1 给别名补上校验,长度上限文档里写 100 字节、5.6.16 源码是 255
elasticsearch
衡石科技14 小时前
SQL 由模型写出来之后:行级安全在问数链路上的四个注入点
数据库·sql·安全·数据分析
隔窗听雨眠14 小时前
数据分析流水线的代理化改造与能力边界
数据分析·智能体·数据工程
约书亚的博客16 小时前
Elasticsearch 常用命令速查:从集群健康到索引与查询
大数据·elasticsearch·搜索引擎