Elasticsearch 分词器的使用与IK分词器安装

Elasticsearch 分词器安装与使用

ES内置分词器

  • standard:默认分词器,简单会被拆分,英文统一转换为小写
  • simlle:按照非字母分词,英文统一转换为小写
  • whitespace:按照空格分词
  • stop:去除无意义的单词,比如:the、is、a、an
  • keyword:不做分词,把文本的整体当作一个单独的关键词

测试分词结果

指定分词器测试结果

GET    http://192.168.213.154:9200/_analyze
POST   http://192.168.213.154:9200/_analyze

{
    "text":"The Super Man",
    "analyzer":"standard"
}

 测试文本中的字段分词结果

GET    http://192.168.213.154:9200/index_test_1/_analyze
POST   http://192.168.213.154:9200/index_test_1/_analyze

{
    "analyzer": "standard",
    "field": "name",
    "text": "Why so powerful "
}

 索引中使用分词器

  • 可以在创建索引的时候同时创建映射关系的时候使用
  • 也可以在为索引创建映射关系的时候使用
PUT     http://192.168.213.154:9200/index_test_1
{
    "mappings": {
        "properties": {
            "realname": {
            	"type": "text",
            	"index": true
            },
            "username": {
            	"type": "keyword",
            	"index": false
            }
        }
    }
}


---------------------------------------------------------------------


POST    http://192.168.213.154:9200/index_test_1/mapping
{
    "properties": {
        "name": {
        	   "type": "text",
               "analyzer":"stop"
        }
    }
}


安装中文IK分词器

  • github IK分词器下载地址,选择对应ES版本的分词器即可
  • 进入elasticsearch安装目录
  • 在./plugins文件加下创建一个ik文件夹
    • cd /usr/local/elasticsearch/plugins/ 
    • mkdir ik
  • 然后将下载的压缩包解压到这个文件夹下,重启ES即可
    • cd ik
    • unzip /home/chenyb/software/elasticsearch-analysis-ik-7.4.2.zip
    • 记得给ik文件夹授权
      • chown es:es ./ik/

 IK分词器

ik:ik_max_word,细粒度分词

{
    "tokens": [
        {
            "token": "中华人民共和国",
            "start_offset": 0,
            "end_offset": 7,
            "type": "CN_WORD",
            "position": 0
        },
        {
            "token": "中华人民",
            "start_offset": 0,
            "end_offset": 4,
            "type": "CN_WORD",
            "position": 1
        },
        {
            "token": "中华",
            "start_offset": 0,
            "end_offset": 2,
            "type": "CN_WORD",
            "position": 2
        },
        {
            "token": "华人",
            "start_offset": 1,
            "end_offset": 3,
            "type": "CN_WORD",
            "position": 3
        },
        {
            "token": "人民共和国",
            "start_offset": 2,
            "end_offset": 7,
            "type": "CN_WORD",
            "position": 4
        },
        {
            "token": "人民",
            "start_offset": 2,
            "end_offset": 4,
            "type": "CN_WORD",
            "position": 5
        },
        {
            "token": "共和国",
            "start_offset": 4,
            "end_offset": 7,
            "type": "CN_WORD",
            "position": 6
        },
        {
            "token": "共和",
            "start_offset": 4,
            "end_offset": 6,
            "type": "CN_WORD",
            "position": 7
        },
        {
            "token": "国",
            "start_offset": 6,
            "end_offset": 7,
            "type": "CN_CHAR",
            "position": 8
        }
    ]
}

ik:ik_smart,粗力度分词

{
    "tokens": [
        {
            "token": "中华人民共和国",
            "start_offset": 0,
            "end_offset": 7,
            "type": "CN_WORD",
            "position": 0
        }
    ]
}

自定义词库

  • 编辑配置文件
    • 绝对路径,相对路径都可以,多个配置用“;”间隔
    • 命名没有要求,建议dic后缀规范而已
      • vim ./elasticsearch/plugins/ik/config/IKAnalyzer.cfg.xml
    • 重启ES服务生效

​​​​​​​​​​​​​​

 

 

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值