Docker部署ElasticSearch数据库+analysis-ik分词器插件

文章目录

    • 一、部署ElasticSearch数据库
    • 二、添加分词器插件(analysis-ik)
    • 三、测试ElasticSearch数据库+analysis-ik分词器插件

一、部署ElasticSearch数据库

1、准备工作

docker pull docker.elastic.co/elasticsearch/elasticsearch:7.17.6
Pwd="/data/software/elasticsearch/"
mkdir ${Pwd}/{data,plugins,config,logs} -p
chmod  -R 777 ${Pwd}
echo "vm.max_map_count=262144" >> /etc/sysctl.conf

2、创建配置文件

cat > ${Pwd}/config/elasticsearch.yml << EOF
cluster.name: "docker-cluster"
network.host: 0.0.0.0
EOF

3、运行容器

docker run -itd --name elasticsearch \
    -v ${Pwd}/data:/usr/share/elasticsearch/data \
    -v ${Pwd}/plugins:/usr/share/elasticsearch/plugins \
    -v ${Pwd}/config/elasticsearch.yml:/usr/share/elasticsearch/config/elasticsearch.yml \
    -v ${Pwd}/logs:/usr/share/elasticsearch/logs \
    -v /etc/localtime:/etc/localtime \
    -v /etc/sysctl.conf:/etc/sysctl.conf \
    -e ES_JAVA_OPTS="-Xms512m -Xmx512m" \
    -e "node.name=es1" \
    -e "discovery.seed_hosts=es1" \
    -e "cluster.initial_master_nodes=es1" \
    -e "http.host=0.0.0.0" \
    -p 9200:9200 \
    -p 9300:9300 \
    --privileged \
    --restart=always \
    docker.elastic.co/elasticsearch/elasticsearch:7.17.6

4、设置vm.max_map_count 防止启动失败

docker exec -it elasticsearch sysctl -w vm.max_map_count=262144

二、添加分词器插件(analysis-ik)

1、首先在github下载指定版本的(和ES版本对应)analysis-ik分词器 analysis-ik下载地址
Docker部署ElasticSearch数据库+analysis-ik分词器插件_第1张图片
2、将下载包cp到容器内部 && 安装插件

docker cp elasticsearch-analysis-ik-7.17.6.zip elasticsearch:/tmp
docker exec -it elasticsearch /bin/bash
cd bin
./elasticsearch-plugin install file:///tmp/elasticsearch-analysis-ik-7.17.6.zip 

-> Installing file:///tmp/elasticsearch-analysis-ik-7.17.6.zip
-> Downloading file:///tmp/elasticsearch-analysis-ik-7.17.6.zip
[=================================================] 100%?? 
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
@     WARNING: plugin requires additional permissions     @
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
* java.net.SocketPermission * connect,resolve
See https://docs.oracle.com/javase/8/docs/technotes/guides/security/permissions.html
for descriptions of what these permissions allow and the associated risks.

Continue with installation? [y/N]y #  输入Y
-> Installed analysis-ik
-> Please restart Elasticsearch to activate any plugins installed

3、安装完成后重启elasticsearch

docker restart elasticsearch

三、测试ElasticSearch数据库+analysis-ik分词器插件

为了方便测试我这里使用Postman工具进行测试:

1、测试ElasticSearch数据库

Docker部署ElasticSearch数据库+analysis-ik分词器插件_第2张图片
2、查看已经安装的插件

Docker部署ElasticSearch数据库+analysis-ik分词器插件_第3张图片
3、验证分词器功能是否正常
创建test索引
Docker部署ElasticSearch数据库+analysis-ik分词器插件_第4张图片
进行分词测试
Docker部署ElasticSearch数据库+analysis-ik分词器插件_第5张图片

使用服务器中的curl命令进行测试:

1、测试ElasticSearch数据库

curl http://16.32.15.115:9200

{
  "name" : "es1",
  "cluster_name" : "docker-cluster",
  "cluster_uuid" : "dC8v3zOoQgGWqgt0smdKtw",
  "version" : {
    "number" : "7.17.6",
    "build_flavor" : "default",
    "build_type" : "docker",
    "build_hash" : "f65e9d338dc1d07b642e14a27f338990148ee5b6",
    "build_date" : "2022-08-23T11:08:48.893373482Z",
    "build_snapshot" : false,
    "lucene_version" : "8.11.1",
    "minimum_wire_compatibility_version" : "6.8.0",
    "minimum_index_compatibility_version" : "6.0.0-beta1"
  },
  "tagline" : "You Know, for Search"
}

2、查看已经安装的插件

curl http://16.32.15.115:9200/_cat/plugins

es1 analysis-ik 7.17.6

3、验证分词器功能是否正常
创建test2索引

curl -XPUT http://16.32.15.115:9200/test2/

{"acknowledged":true,"shards_acknowledged":true,"index":"test2"}

进行分词测试

curl -X POST -H "Content-Type: application/json" -d '{
  "analyzer": "ik_smart",
  "text": "社会你腾哥,人狠话不多!!!"
}' http://16.32.15.115:9200/test2/_analyze --silent | jq

注意:为了显示返回json数据我添加了 --silent | jq 参数,此参数需要安装jq命令,如果没有去掉参数就行。

  • –silent参数:用于禁止显示进度条和错误信息:
  • | jq命令:将curl返回的结果通过管道符号传递给jq命令进行JSON数据处理。

返回内容:

{
  "tokens": [
    {
      "token": "社会",
      "start_offset": 0,
      "end_offset": 2,
      "type": "CN_WORD",
      "position": 0
    },
    {
      "token": "你",
      "start_offset": 2,
      "end_offset": 3,
      "type": "CN_CHAR",
      "position": 1
    },
    {
      "token": "腾",
      "start_offset": 3,
      "end_offset": 4,
      "type": "CN_CHAR",
      "position": 2
    },
    {
      "token": "哥",
      "start_offset": 4,
      "end_offset": 5,
      "type": "CN_CHAR",
      "position": 3
    },
    {
      "token": "人",
      "start_offset": 6,
      "end_offset": 7,
      "type": "CN_CHAR",
      "position": 4
    },
    {
      "token": "狠",
      "start_offset": 7,
      "end_offset": 8,
      "type": "CN_CHAR",
      "position": 5
    },
    {
      "token": "话",
      "start_offset": 8,
      "end_offset": 9,
      "type": "CN_CHAR",
      "position": 6
    },
    {
      "token": "不多",
      "start_offset": 9,
      "end_offset": 11,
      "type": "CN_WORD",
      "position": 7
    }
  ]
}

你可能感兴趣的:(#,1-Docker容器,docker,elasticsearch,数据库)