安装
https://www.elastic.co/guide/en/elasticsearch/reference/current/docker.html
v5.6.6
docker pull docker.elastic.co/elasticsearch/elasticsearch:6.4.3
docker run -p 29200:9200 -p 29300:9300 -e "discovery.type=single-node" docker.elastic.co/elasticsearch/elasticsearch:6.4.3
v7.10.2
docker pull docker.elastic.co/elasticsearch/elasticsearch:7.10.2
docker run -p 9200:9200 -e "discovery.type=single-node" -e "xpack.security.enabled=true" docker.elastic.co/elasticsearch/elasticsearch:7.10.2
# docker-compose
es7:
image: docker.elastic.co/elasticsearch/elasticsearch:7.10.2
ports:
- "9200:9200"
volumes:
- "./es7/config:/usr/share/elasticsearch/config"
- "./es7/data:/usr/share/elasticsearch/data"
- "./es7/logs:/usr/share/elasticsearch/logs"
- "./es7/plugins:/usr/share/elasticsearch/plugins"
environment:
- discovery.type=single-node
- xpack.security.enabled=false
- TAKE_FILE_OWNERSHIP=true
密码设置
# 获取docker默认配置
docker cp [docker ID]:/usr/share/elasticsearch/config .
# 设置密码--逐一设置
./bin/elasticsearch-setup-passwords interactive
# 设置密码--自动生成随机密码
./bin/elasticsearch-setup-passwords auto
Spring版本支持
- Spring Data Elasticsearch 4.1 - Spring 5.3 - Elasticsearch 7.9.3 - Spring Boot 2.4.x
- Spring Data Elasticsearch 4.0 - Spring 5.2 - Elasticsearch 7.6.2 - Spring Boot 2.3.x
- Spring Data Elasticsearch 3.2 - - Elasticsearch 6.8.1 - Spring Boot 2.2.x
Spring Data Release Train | Spring Data Elasticsearch | Elasticsearch | Spring Boot |
---|---|---|---|
2020.0.0[1] | 4.1.x[1] | 7.9.3 | 2.4.x[1] |
Neumann | 4.0.x | 7.6.2 | 2.3.x |
Moore | 3.2.x | 6.8.12 | 2.2.x |
Lovelace | 3.1.x | 6.2.2 | 2.1.x |
Kay[2] | 3.0.x[2] | 5.5.0 | 2.0.x[2] |
Ingalls[2] | 2.1.x[2] | 2.4.0 | 1.5.x[2] |
备份与导入
https://github.com/taskrabbit/elasticsearch-dump
工具2:esm:https://github.com/medcl/esm-v1/releases 基于go实现,推荐
# 安装
npm install elasticdump -g
# 使用
elasticdump \
--input=http://production.es.com:9200/my_index \
--output=http://staging.es.com:9200/my_index \
--type=analyzer
elasticdump \
--input=http://production.es.com:9200/my_index \
--output=http://staging.es.com:9200/my_index \
--type=mapping
elasticdump \
--input=http://production.es.com:9200/my_index \
--output=http://staging.es.com:9200/my_index \
--type=data
es-head
head工具安装
docker run -p 9100:9100 mobz/elasticsearch-head:5
常用操作
# 查看index
curl 127.0.0.1:9200/_cat/indices?v
curl 127.0.0.1:9200/_cat/indices/${index}/?v
# mapping查看
curl 127.0.0.1:9200/index_name/_mapping
curl 127.0.0.1:9200/index_name/_mapping/${mapping}
# aliases别名查看
curl 127.0.0.1:9200/_aliases
# stats状态查看
curl 127.0.0.1:9200/${index}/_stats
# 数据导入
curl -XPOST 192.168.1.1:9200/_reindex -d '{"source":{"index":"old_index"},"dest":{"index":"new_index","version_type":"internal"}}'
查询API
match、term、match_phrase、query_string的区别
- term查询keyword字段。term不会分词。而keyword字段也不分词。需要完全匹配才可以。
- term查询text字段。因为text字段会分词,而term不分词,所以term查询的条件必须是text字段分词后的某一个。
- match查询keyword字段。match会被分词,而keyword不会被分词,match的需要跟keyword的完全匹配可以。
- match查询text字段match分词,text也分词,只要match的分词结果和text的分词结果有相同的就匹配。
- match_phrase匹配keyword字段。match_phrase会被分词,而keyword不会被分词,match_phrase的需要跟keyword的完全匹配才可以。
- match_phrase匹配text字段。match_phrase是分词的,text也是分词的。match_phrase的分词结果必须在text字段分词中都包含,而且顺序必须相同,而且必须都是连续的。
- query_string查询text类型的字段。和match_phrase区别的是,query_string查询text类型字段,不需要连续,顺序还可以调换。 | | keyword(不会分词) | text(分词) | | —- | —- | —- | | term(不会分词) | 完全匹配 | 是分词后的某一个 | | match(分词) | 完全匹配 | 分词结果有匹配 | | match_phrase(分词) | 完全匹配 | 分词结果都包含,顺序连续且一致 | | query_string(分词) | 完全匹配 | 分词结果都包含,无顺序要求 |
插件安装
分词器hanlp安装-v7.10.2
插件:https://github.com/KennFalcon/elasticsearch-analysis-hanlp
注意使用docker挂载本地目录时需要指定参数:TAKE_FILE_OWNERSHIP=true
./bin/elasticsearch-plugin install https://github.com/KennFalcon/elasticsearch-analysis-hanlp/releases/download/v7.10.2/elasticsearch-analysis-hanlp-7.10.2.zip
# 创建索引
curl -X PUT "localhost:9200/test?pretty"
# 测试分词
curl -X POST "localhost:9200/test/_analyze?pretty" -H 'Content-Type: application/json' -d'
{
"text": "美国阿拉斯加州发生8.0级地震",
"analyzer": "hanlp"
}
'