elasticseach集群记录
elasticseach实践记录
安装
环境: macos
es版本:7.6.2
集群模式:单例模式
docker安装
sh:
docker run -d --name elasticsearch --net riosNetwork -p 9200:9200 -p 9300:9300 -e "discovery.type=single-node" elasticsearch:7.6.2
docker-compose.yml:
version: "3.6"
services:
elasticsearch:
image: elasticsearch:7.6.2
volumes:
- ~/docker_map/elasticsearch2/data:/usr/share/elasticsearch/data
ports:
- "9200:9200"
- "9300:9300"
environment:
ES_JAVA_OPTS: "-Xmx256m -Xms256m"
discovery.type: "single-node"
networks:
default:
external:
name: riosNetwork
执行完成:
docker已启动:
验证是否安装成功
curl 'http://localhost:9200/?pretty'
返回:
{
"name" : "c6bc494831c6",
"cluster_name" : "docker-cluster",
"cluster_uuid" : "w5dg5AojT4K1B1_2m055iQ",
"version" : {
"number" : "7.6.2",
"build_flavor" : "default",
"build_type" : "docker",
"build_hash" : "ef48eb35cf30adf4db14086e8aabd07ef6fb113f",
"build_date" : "2020-03-26T06:34:37.794943Z",
"build_snapshot" : false,
"lucene_version" : "8.4.0",
"minimum_wire_compatibility_version" : "6.8.0",
"minimum_index_compatibility_version" : "6.0.0-beta1"
},
"tagline" : "You Know, for Search"
}
表示安装成功 !!!
查看节点使用情况
http://localhost:9200/_cat/nodes?v
返回:
安装监控插件
https://www.elastic.co/cn/downloads/marvel
Marvel是Elasticsearch的管理和监控工具,但是在es5.0之后,就不单独存在了,被整合到x-Pack里了,而x-pack不能独立安装,要求必须同时安装了:
- ES
- Kibana
x-pack安装步骤
安装中文分词
docker-compose exec elasticsearch elasticsearch-plugin install https://github.com/medcl/elasticsearch-analysis-ik/releases/download/v7.6.2/elasticsearch-analysis-ik-7.6.2.zip
重启下docker:
docker-compose restart elasticsearch
这种方式安装插件经常会因为资源是在外网上,导致安装失败。
可通过先在宿主机上将插件下载下来,然后copy到docker容器中进行本地安装:
wget https://github.com/medcl/elasticsearch-analysis-ik/releases/download/v7.6.2/elasticsearch-analysis-ik-7.6.2.zip
copy 到docker中:
docker cp elasticsearch-analysis-ik-7.6.2.zip [docker name]
然后进入docker安装:
./bin/elasticsearch-plugin install elasticsearch-analysis-ik-7.6.2.zip
使用中文分词
官方教程很简单:
https://github.com/medcl/elasticsearch-analysis-ik
创建一个索引:
curl -XPUT http://localhost:9200/xiaoheitest
创建索引的maping规则:
curl -XPOST http://localhost:9200/xiaoheitest/_mapping -H 'Content-Type:application/json' -d'
{
"properties": {
"content": {
"type": "text",
"analyzer": "ik_max_word",
"search_analyzer": "ik_smart"
}
}
}'
一般查询条件用ik_smart就ok了,是比较粗粒度的分词。
返回内容匹配使用ik_max_word,最大精底的分词。
添加索引中的文档内容
curl -XPOST http://localhost:9200/xiaoheitest/_create/1 -H ‘Content-Type:application/json’ -d’
{“content”:”你今天加班吗?加班的话去吃饭吧”}
‘
curl -XPOST http://localhost:9200/xiaoheitest/_create/2 -H ‘Content-Type:application/json’ -d’
{“content”:”「宋战磊:@星空 @张延宏 上午把语音调通,和周剑一起试试,把问题记录下来@星空 @张延宏 语音能跑起来了嘛?可以了我们一起测测”}
‘
curl -XPOST http://localhost:9200/xiaoheitest/_create/3 -H 'Content-Type:application/json' -d'
{"content":"一天一报的线上问题,今天能搞定么?@宋战磊"}
'
curl -XPOST http://localhost:9200/xiaoheitest/_create/4 -H 'Content-Type:application/json' -d'
{"content":"大家伙们来呀"}
'
curl -XPOST http://localhost:9200/xiaoheitest/_create/5 -H 'Content-Type:application/json' -d'
{"content":"@KN 你名下的bug处理下"}
'
curl -XPOST http://localhost:9200/xiaoheitest/_create/6 -H 'Content-Type:application/json' -d'
{"content":"怎么处理"}
'
curl -XPOST http://localhost:9200/xiaoheitest/_create/7 -H 'Content-Type:application/json' -d'
{"content":"该关的关,该延期的延期,该分配的分配,还用我教你?"}
'
匹配查询一下
curl -XPOST http://localhost:9200/xiaoheitest/_search -H 'Content-Type:application/json' -d'
{
"query" : { "match" : { "content" : "你" }},
"highlight" : {
"pre_tags" : ["<tag1>", "<tag2>"],
"post_tags" : ["</tag1>", "</tag2>"],
"fields" : {
"content" : {}
}
}
}
'
发现返回了匹配数据:
{
"took":880,
"timed_out":false,
"_shards":{
"total":1,
"successful":1,
"skipped":0,
"failed":0
},
"hits":{
"total":{
"value":2,
"relation":"eq"
},
"max_score":1.4698057,
"hits":[
{
"_index":"xiaoheitest",
"_type":"_doc",
"_id":"5",
"_score":1.4698057,
"_source":{
"content":"@KN 你名下的bug处理下"
},
"highlight":{
"content":[
"@KN <tag1>你</tag1>名下的bug处理下"
]
}
},
{
"_index":"xiaoheitest",
"_type":"_doc",
"_id":"1",
"_score":1.3706115,
"_source":{
"content":"你今天加班吗?加班的话去吃饭吧"
},
"highlight":{
"content":[
"<tag1>你</tag1>今天加班吗?加班的话去吃饭吧"
]
}
}
]
}
}
看得出来,全部匹配到的会打个全亮的标签。