# docker启动的elasticsearch-ik服务 ## 介绍 整合`elasticsearch`和`analysis-ik`的一键启动容器,选用版本`7.17.18`。 ## 用法 ```sh docker compose up -d ``` ### 安全问题 在`config/elasticsearch.yml`中已经配置了`xpack.security.enabled: true`,所以在首次启动容器后需要为elasticsearch中的各个账号设置密码: ```sh docker exec -it es bin/elasticsearch-setup-passwords interactive ``` 接下来就可以按照提示分别为各个用户设置密码: - elastic - apm_system - kibana - kibana_system - logstash_system - beats_system - remote_monitoring_user 密码生效后会写入`data`目录中,所在容器是可以`down`掉的。 ## 问题 如果服务启动失败,可能是文件夹文件问题,修改`config`目录和`data`目录的权限: ```sh chmod 777 -R data chmod 755 -R config ``` 然后再试着重新启动服务。 ## arm64平台 如果要在arm平台上使用,请将`docker-compose.yml`文件中的镜像标签改为`wandoubaba517/elasticsearch-ik:7.17.18`。 ## 分词器使用示例 ### 1.create a index ```bash curl -XPUT http://localhost:9200/index ``` ### 2.create a mapping ```bash curl -XPOST http://localhost:9200/index/_mapping -H 'Content-Type:application/json' -d' { "properties": { "content": { "type": "text", "analyzer": "ik_max_word", "search_analyzer": "ik_smart" } } }' ``` ### 3.index some docs ```bash curl -XPOST http://localhost:9200/index/_create/1 -H 'Content-Type:application/json' -d' {"content":"美国留给伊拉克的是个烂摊子吗"} ' ``` ```bash curl -XPOST http://localhost:9200/index/_create/2 -H 'Content-Type:application/json' -d' {"content":"公安部:各地校车将享最高路权"} ' ``` ```bash curl -XPOST http://localhost:9200/index/_create/3 -H 'Content-Type:application/json' -d' {"content":"中韩渔警冲突调查:韩警平均每天扣1艘中国渔船"} ' ``` ```bash curl -XPOST http://localhost:9200/index/_create/4 -H 'Content-Type:application/json' -d' {"content":"中国驻洛杉矶领事馆遭亚裔男子枪击 嫌犯已自首"} ' ``` ### 4.query with highlighting ```bash curl -XPOST http://localhost:9200/index/_search -H 'Content-Type:application/json' -d' { "query" : { "match" : { "content" : "中国" }}, "highlight" : { "pre_tags" : ["<tag1>", "<tag2>"], "post_tags" : ["</tag1>", "</tag2>"], "fields" : { "content" : {} } } } ' ``` Result ```json { "took": 14, "timed_out": false, "_shards": { "total": 5, "successful": 5, "failed": 0 }, "hits": { "total": 2, "max_score": 2, "hits": [ { "_index": "index", "_type": "fulltext", "_id": "4", "_score": 2, "_source": { "content": "中国驻洛杉矶领事馆遭亚裔男子枪击 嫌犯已自首" }, "highlight": { "content": [ "<tag1>中国</tag1>驻洛杉矶领事馆遭亚裔男子枪击 嫌犯已自首 " ] } }, { "_index": "index", "_type": "fulltext", "_id": "3", "_score": 2, "_source": { "content": "中韩渔警冲突调查:韩警平均每天扣1艘中国渔船" }, "highlight": { "content": [ "均每天扣1艘<tag1>中国</tag1>渔船 " ] } } ] } } ```