ES分词

ES默认分词为英文分词(使用空格来进行分词)不符合中文分词要求。例如

GET _analyze
{
"text":"我不喜欢你"
}

会得到如下分词结果

复制代码
{
  "tokens" : [
    {
      "token" : "",
      "start_offset" : 0,
      "end_offset" : 1,
      "type" : "<IDEOGRAPHIC>",
      "position" : 0
    },
    {
      "token" : "",
      "start_offset" : 1,
      "end_offset" : 2,
      "type" : "<IDEOGRAPHIC>",
      "position" : 1
    },
    {
      "token" : "",
      "start_offset" : 2,
      "end_offset" : 3,
      "type" : "<IDEOGRAPHIC>",
      "position" : 2
    },
    {
      "token" : "",
      "start_offset" : 3,
      "end_offset" : 4,
      "type" : "<IDEOGRAPHIC>",
      "position" : 3
    },
    {
      "token" : "",
      "start_offset" : 4,
      "end_offset" : 5,
      "type" : "<IDEOGRAPHIC>",
      "position" : 4
    }
  ]
}
View Code
复制代码

在实际开发中需要安装分词器(IK),安装分词器

解压elasticsearch-analysis-ik-7.6.1

在ES目录里的plugins目录里创建ik文件夹

拷贝elasticsearch-analysis-ik-7.6.1文件里的所有内容到ik文件夹中。如图

重启ES和kibana

如果ES加载过程中如下

 

 说明插件配置成功

再在kibana中执行如下解析

GET _analyze
{
"text":"我不喜欢你",
"analyzer": "ik_max_word"
}

输出结果

 

复制代码
{
  "tokens" : [
    {
      "token" : "",
      "start_offset" : 0,
      "end_offset" : 1,
      "type" : "CN_CHAR",
      "position" : 0
    },
    {
      "token" : "不喜欢",
      "start_offset" : 1,
      "end_offset" : 4,
      "type" : "CN_WORD",
      "position" : 1
    },
    {
      "token" : "喜欢",
      "start_offset" : 2,
      "end_offset" : 4,
      "type" : "CN_WORD",
      "position" : 2
    },
    {
      "token" : "",
      "start_offset" : 4,
      "end_offset" : 5,
      "type" : "CN_CHAR",
      "position" : 3
    }
  ]
}
View Code
复制代码

自定义分词

在ik\config文件中创建test.dic文件输入如下内容

修改IKAnalyzer.cfg.xml配置文件

 

 重启ES和kibana

执行如下语句

GET _analyze
{
"text":"我不喜欢你",
"analyzer": "ik_max_word"
}

发现分词结果会多出刚才添加的分词内容。如下

 

posted @   都市之夜  阅读(827)  评论(0编辑  收藏  举报
编辑推荐:
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
阅读排行:
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 【译】Visual Studio 中新的强大生产力特性
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 【设计模式】告别冗长if-else语句:使用策略模式优化代码结构
点击右上角即可分享
微信分享提示