docker部署elasticsearch + ik分词器

Elasticsearch

构建docker容器

  1. 创建文件夹
    mkdir -p docker/es/data
    
    mkdir -p docker/es/plugins
    
  2. 配置文件
    cd /docker/es
    
    echo "http.host: 0.0.0.0" >> elasticsearch.yml
    
  3. docker run
    docker run --name es7 \
    -p 19200:9200 -p 19300:9300 \
    -e "discovery.type=single-node" \
    -e ES_JAVA_OPTS="-Xms128m -Xmx512m" \
    -v docker/es/data:/usr/share/elasticsearch/data \
    -v docker/es/plugins:/usr/share/elasticsearch/plugins \
    -v docker/es/elasticsearch.yml:/usr/share/elasticsearch/config/elasticsearch.yml \
    -d elasticsearch:7.17.4
    
  4. 开启账号密码
    1. 修改elasticsearch.yml
      vim docker/es/elasticsearch.yml
      
      http.cors.enabled: true
      http.cors.allow-origin: "*"
      http.cors.allow-headers: Authorization
      xpack.security.enabled: true
      xpack.security.transport.ssl.enabled: true
      
    2. 重启容器
      docker restart es7
      
    3. 进入容器
      docker exec -it es7 /bin/bash
      
    4. 设置密码
      ./bin/elasticsearch-setup-passwords interactive
      
    5. 再次重启容器
      docker restart es7
      
  5. 查看es服务

安装ik分词器

  1. 进入容器
    docker exec -it es7 /bin/bash
    
  2. 下载插件
    ./bin/elasticsearch-plugin install https://github.com/medcl/elasticsearch-analysis-ik/releases/download/v7.17.4/elasticsearch-analysis-ik-7.17.4.zip
    
  3. 重启容器
    docker restart es7
    
  4. 查看es插件
  5. 验证插件功能
    url -X GET -H "Content-Type: application/json"  "http://127.0.0.1:19200/_analyze?pretty=true" -d'{"text":"中华五千年华夏","analyzer": "ik_smart"}'
    
    结果:
    {
       "tokens" : [{
           "token" : "中华",
           "start_offset" : 0,
           "end_offset" : 2,
           "type" : "CN_WORD",
           "position" : 0
         },
         {
           "token" : "五千年",
           "start_offset" : 2,
           "end_offset" : 5,
           "type" : "TYPE_CQUAN",
           "position" : 1
         },
         {
           "token" : "华夏",
           "start_offset" : 5,
           "end_offset" : 7,
           "type" : "CN_WORD",
           "position" : 2
       }]
     }
    
posted @ 2023-01-15 18:07  苏戏  阅读(80)  评论(0编辑  收藏  举报