• Docker部署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
    
    • 1
    • 2
    • 3
    • 4
    • 5

    2、创建配置文件

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

    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
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17

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

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

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

    1、首先在github下载指定版本的(和ES版本对应)analysis-ik分词器 analysis-ik下载地址
    在这里插入图片描述
    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
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18

    3、安装完成后重启elasticsearch

    docker restart elasticsearch
    
    • 1

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

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

    1、测试ElasticSearch数据库

    在这里插入图片描述
    2、查看已经安装的插件


    3、验证分词器功能是否正常
    创建test索引
    在这里插入图片描述
    进行分词测试
    在这里插入图片描述

    使用服务器中的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"
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19

    2、查看已经安装的插件

    curl http://16.32.15.115:9200/_cat/plugins
    
    es1 analysis-ik 7.17.6
    
    • 1
    • 2
    • 3

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

    curl -XPUT http://16.32.15.115:9200/test2/
    
    {"acknowledged":true,"shards_acknowledged":true,"index":"test2"}
    
    • 1
    • 2
    • 3

    进行分词测试

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

    注意:为了显示返回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
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
  • 相关阅读:
    数据结构-排序算法总结
    什么是单向数据流
    文献认证!Kamiya艾美捷抗酒石酸酸性磷酸酶TRAP染色试剂盒
    SQL基础
    InfluxDB学习记录(二)——influxdb的关键概念
    用亚马逊自养号进行测评的好处
    用户登录程序需求
    基于大仓库的微服务差异化构建工具
    真正“搞”懂HTTP协议13之HTTP2
    优先级队列(堆)【Java】
  • 原文地址:https://blog.csdn.net/weixin_45310323/article/details/133216643