数据分析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脚本 测试

或者:

相关推荐
Ai_easygo1 小时前
多Agent数据分析报告自动化实战:用CrewAI组支AI团队,丢份数据就出报告(从架构到评估)
人工智能·数据分析·自动化
kingwebo'sZone1 小时前
C#事件声明办法
java·前端·c#
Dylan的码园3 小时前
从Excel到数据库:数据分析全流程与Kettle ETL实战指南
数据库·数据分析·excel
一次旅行3 小时前
【效率秘籍】Shell 函数+别名组合拳,让 80% 的重复命令变 1 个字母
大数据·elasticsearch·搜索引擎
码农学院4 小时前
Elasticsearch构建汽车零配件智能搜索与匹配系统
大数据·elasticsearch·汽车
XIAOYU6720134 小时前
软件工程转数据分析现实吗?适合哪些学生
数据挖掘·数据分析·软件工程
fanstuck10 小时前
1M 上下文能怎么用?我用 Seed Evolving 做了一个招标文件版本差异审查器
服务器·人工智能·数据分析·开源·aigc
Xin_ye1008619 小时前
第三章:内存泄漏的常见“案发现场”
c#·wpf