前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Docker 通过 Dockfile 安装 elasticsearch 以及 ik ,pinyin 分词

Docker 通过 Dockfile 安装 elasticsearch 以及 ik ,pinyin 分词

作者头像
Alone88
发布2023-10-21 16:03:46
2010
发布2023-10-21 16:03:46
举报
文章被收录于专栏:Alone88Alone88

说明

安装 elasticsearch 的 ik 和 pinyin 分词插件,插件的版本要和 elasticsearch 的版本一致

ik 分词地址: https://github.com/medcl/elasticsearch-analysis-ik/

pinyin分词地址: https://github.com/medcl/elasticsearch-analysis-pinyin/

本文使用 elasticsearch 5.6.9 安装

开始

拉取镜像

代码语言:javascript
复制
docker pull elasticsearch:5.6.9

下载插件包

代码语言:javascript
复制
mkdir docker # 先建个文件夹
# 下载 ik 插件
wget https://github.com/medcl/elasticsearch-analysis-ik/releases/download/v5.6.9/elasticsearch-analysis-ik-5.6.9.zip

# 解压
unzip elasticsearch-analysis-ik-5.6.9.zip -d analysis-ik

# 下载 pinyin 插件
wget https://github.com/medcl/elasticsearch-analysis-pinyin/releases/download/v5.6.9/elasticsearch-analysis-pinyin-5.6.9.zip

#解压
unzip elasticsearch-analysis-pinyin-5.6.9.zip -d analysis-pinyin

..

创建 Dockerfile

代码语言:javascript
复制
FROM elasticsearch:5.6.9
ADD analysis-ik /usr/share/elasticsearch/plugins/analysis-ik  
ADD analysis-pinyin /usr/share/elasticsearch/plugins/analysis-pinyin

.

代码语言:javascript
复制
docker build -f Dockerfile -t elasticsearch-ik-pinyin:5.6.9 .

成功创建显示:

代码语言:javascript
复制
root@Alone88-Uos:~/docker/els6# docker build -f Dockerfile -t elasticsearch-ik-pinyin:5.6.9 .
Sending build context to Docker daemon  18.01MB
Step 1/3 : FROM elasticsearch:5.6.9
 ---> 5c1e1ecfe33a
Step 2/3 : ADD analysis-ik /usr/share/elasticsearch/plugins/analysis-ik
 ---> 883cd55df8a8
Step 3/3 : ADD analysis-pinyin /usr/share/elasticsearch/plugins/analysis-pinyin
 ---> 8c9220f304be
Successfully built 8c9220f304be
Successfully tagged elasticsearch-ik-pinyin:5.6.9

创建容器

代码语言:javascript
复制
docker run -e ES_JAVA_OPTS="-Xms256m -Xmx256m" -d -p 9200:9200 -p 9300:9300 -e "discovery.type=single-node" --name elasticsearch_test elasticsearch-ik-pinyin:5.6.9

-e ES_JAVA_OPTS="-Xms256m -Xmx256m" 是设置 elasticsearch 启动的内存大小,默认是系统一半内存

-e discovery.type 是设置为单节点

elasticsearch-ik-pinyin:5.6.9 就是构建镜像的镜像名和版本号

测试分词

测试拼音

请求 https://127.0.0.1:9200/_analyze 请求方式为 post 请求主体

代码语言:javascript
复制
{   "text": "中华人民共和国国徽",   "analyzer": "pinyin" }

返回

代码语言:javascript
复制
**{
    "tokens":**[
        **{
            "token":"zhong",
            "start_offset":0,
            "end_offset":1,
            "type":"word",
            "position":0
        },
        **{
            "token":"zhrmghggh",
            "start_offset":0,
            "end_offset":9,
            "type":"word",
            "position":0
        },
        **{
            "token":"hua",
            "start_offset":1,
            "end_offset":2,
            "type":"word",
            "position":1
        },
        **{
            "token":"ren",
            "start_offset":2,
            "end_offset":3,
            "type":"word",
            "position":2
        },
        **{
            "token":"min",
            "start_offset":3,
            "end_offset":4,
            "type":"word",
            "position":3
        },
        **{
            "token":"gong",
            "start_offset":4,
            "end_offset":5,
            "type":"word",
            "position":4
        },
        **{
            "token":"he",
            "start_offset":5,
            "end_offset":6,
            "type":"word",
            "position":5
        },
        **{
            "token":"guo",
            "start_offset":6,
            "end_offset":7,
            "type":"word",
            "position":6
        },
        **{
            "token":"guo",
            "start_offset":7,
            "end_offset":8,
            "type":"word",
            "position":7
        },
        **{
            "token":"hui",
            "start_offset":8,
            "end_offset":9,
            "type":"word",
            "position":8
        }
    ]
}

测试 ik 分词

analyzer:可填项有:chinese|ik_max_word|ik_smart,其中chinese是ES的默认分词器选项,ik_max_word(最细粒度划分)和ik_smart(最少划分)是ik中文分词器选项

请求地址: https://127.0.0.1:9200/_analyze 请求方式 : post 请求主体:

代码语言:javascript
复制
**{
    "text":"中华人民共和国国徽",
    "analyzer":"ik_max_word"
}

返回

代码语言:javascript
复制
**{
    "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
        },
        **{
            "token":"国徽",
            "start_offset":7,
            "end_offset":9,
            "type":"CN_WORD",
            "position":9
        }
    ]
}

注:不管是拼音分词器还是IK分词器,当深入搜索一条数据是时,必须是通过分词器分析的数据,才能被搜索到,否则搜索不到

IK分词和拼音分词的组合使用

代码语言:javascript
复制
PUT /my_index
{
  "settings": {
        "analysis": {
            "analyzer": {
                "ik_smart_pinyin": {
                    "type": "custom",
                    "tokenizer": "ik_smart",
                    "filter": ["my_pinyin", "word_delimiter"]
                },
                "ik_max_word_pinyin": {
                    "type": "custom",
                    "tokenizer": "ik_max_word",
                    "filter": ["my_pinyin", "word_delimiter"]
                }
            },
            "filter": {
                "my_pinyin": {
                    "type" : "pinyin",
                    "keep_separate_first_letter" : true,
                    "keep_full_pinyin" : true,
                    "keep_original" : true,
                    "limit_first_letter_length" : 16,
                    "lowercase" : true,
                    "remove_duplicated_term" : true 
                }
            }
        }
  }
  
}

当我们建type时,需要在字段的analyzer属性填写自己的映射

代码语言:javascript
复制
PUT /my_index/my_type/_mapping
{
    "my_type":{
      "properties": {
        "id":{
          "type": "integer"
        },
        "name":{
          "type": "text",
          "analyzer": "ik_smart_pinyin"
        }
      }
    }
}
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 说明
  • 开始
  • 创建容器
  • 测试分词
    • 测试拼音
      • 测试 ik 分词
      • IK分词和拼音分词的组合使用
      相关产品与服务
      容器镜像服务
      容器镜像服务(Tencent Container Registry,TCR)为您提供安全独享、高性能的容器镜像托管分发服务。您可同时在全球多个地域创建独享实例,以实现容器镜像的就近拉取,降低拉取时间,节约带宽成本。TCR 提供细颗粒度的权限管理及访问控制,保障您的数据安全。
      领券
      问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档