Elasticsearch安装及使用
Eave
2018.12.23
一、下载elasticsearch
下载地址:https://www.elastic.co/downloads/elasticsearch
wget "https://artifacts.elastic.co/downloads/elasticsearch/elasticsearch-9.5.2-linux-x86_64.tar.gz"
tar -zxf elasticsearch-9.5.2-linux-x86_64.tar.gz
mv elasticsearch-9.5.2 /usr/local/elasticsearch
使用系统的JDK:修改bin/elasticsearch-env文件,在if [ ! -z "$ES_JAVA_HOME" ]; then上方添加ES_JAVA_HOME="/usr/local/java"
···
# 使用系统的JDK
ES_JAVA_HOME="/usr/local/java"
# now set the path to java
if [ ! -z "$ES_JAVA_HOME" ]; then
···
禁用安全配置:修改config/elasticsearch.yml文件,将xpack.security.enabled、xpack.security.enrollment.enabled、xpack.security.http.ssl.enabled、xpack.security.transport.ssl.enabled值全部改为false
···
# Enable security features
xpack.security.enabled: false
xpack.security.enrollment.enabled: false
# Enable encryption for HTTP API client connections, such as Kibana, Logstash, and Agents
xpack.security.http.ssl:
enabled: false
keystore.path: certs/http.p12
# Enable encryption and mutual authentication between cluster nodes
xpack.security.transport.ssl:
enabled: false
verification_mode: certificate
keystore.path: certs/transport.p12
truststore.path: certs/transport.p12
# 禁止通配符删除
action.destructive_requires_name: true
···
修改日志输出格式:修改config/log4j2.properties文件,将*.pattern值全部改为%d{yyyy-MM-dd HH:mm:ss.SSS} [%p]-[%t][%c] %m%n
···
appender.console.layout.pattern=%d{yyyy-MM-dd HH:mm:ss.SSS} [%p]-[%t][%c] %m%n
···
appender.rolling_old.layout.pattern=%d{yyyy-MM-dd HH:mm:ss.SSS} [%p]-[%t][%c] %m%n
···
启动Elasticsearch
runuser -l www -c "nohup /usr/local/elasticsearch/bin/elasticsearch > /dev/null 2>&1 &"
二、安装IK中文分词器
IK版本需要和elasticsearch版本相同
安装方法:https://github.com/medcl/elasticsearch-analysis-ik
bin/elasticsearch-plugin install https://get.infini.cloud/elasticsearch/analysis-ik/9.5.2
手动下载安装(推荐)
下载地址:https://release.infinilabs.com/analysis-ik/stable/
unzip elasticsearch-analysis-ik-9.5.2.zip -d /usr/local/elasticsearch/plugins/analysis-ik
重启Elasticsearch
验证插件是否加载
curl "http://localhost:9200/_cat/plugins?v"
测试分词效果
curl -d '{
"analyzer": "ik_max_word",
"text": "测试文档"
}' -H 'Content-Type: application/json' "http://localhost:9200/_analyze"
三、常用查询
# 创建index
curl -X PUT -d '{
"settings": {
"number_of_shards": 3,
"number_of_replicas": 0,
"index.refresh_interval": "120s"
},
"mappings": {
"dynamic": false,
"properties": {
"id": {
"type": "wildcard"
},
"name": {
"type": "text"
}
}
}
}' -H 'Content-Type: application/json' "http://localhost:9200/my_index"
# 查看完整的索引定义
curl "http://localhost:9200/my_index?pretty"
# 仅查看 mappings
curl "http://localhost:9200/my_index/_mapping?pretty"
# 仅查看 settings
curl "http://localhost:9200/my_index/_settings?pretty"
# PUT方式:指定文档ID,如果ID存在则覆盖,不存在则创建
curl -X PUT -d'{
"id": "202609088222405323264000",
"name": "测试文档"
}' -H 'Content-Type: application/json' "http://localhost:9200/my_index/_doc/202609088222405323264000"
# 增量更新(只更新部分字段)
curl -X POST -d '{
"doc": {
"sales": 1000
}
}' -H 'Content-Type: application/json' "http://localhost:9200/my_index/_update/202609088222405323264000"
# 删除索引
curl -X DELETE "http://localhost:9200/my_index"