一、安装elasticsearch
在elasticsearch目录下分别创建data和tmp文件夹
vi /etc/profile
加入配置
export ES_TMPDIR=/mnt/elasticsearch-7.17.7/tmp
export ES_JAVA_OPTS="-Djna.tmpdir=/mnt/elasticsearch-7.17.7/tmp"
vi /etc/sysctl.conf
加入配置
vm.max_map_count = 655360
vi elasticsearch.yml
加入配置
node.name: node-1
path.data: /mnt/elasticsearch-7.17.7/data
path.logs: /mnt/elasticsearch-7.17.7/logs
network.host: 0.0.0.0
http.port: 19200
xpack.security.enabled: true
xpack.license.self_generated.type: basic
xpack.security.transport.ssl.enabled: true
cluster.initial_master_nodes: ["node-1"]
在elasticsearch下的plugins目录下解压分词插件和拼音插件
unzip elasticsearch-analysis-ik-7.17.7.zip -d ik
unzip elasticsearch-analysis-pinyin-7.17.7.zip -d pinyin
创建es用户
groupadd es
useradd es -g es -p es3050
chown -R es:es /mnt/elasticsearch-7.17.7
设置elastic密码
./elasticsearch-setup-passwords interactive
启动elasticsearch
./elasticsearch -d
二、使用elasticsearch搜索
创建索引
curl --user elastic:123456 -H "Content-Type: application/json" -XPUT "http://localhost:19200/hello?pretty" -d'
{
"settings": {
"index": {
"analysis": {
"analyzer": {
"ik_pinyin_analyzer": {
"type": "custom",
"tokenizer": "ik_max_word",
"filter": ["my_pinyin"]
}
},
"filter": {
"my_pinyin": {
"type": "pinyin",
"keep_separate_first_letter": false,
"keep_full_pinyin": true,
"keep_original": false,
"limit_first_letter_length": 10,
"lowercase": true,
"remove_duplicated_term": true
}
}
}
}
}
}'
创建索引映射
curl --user elastic:123456 -H "Content-Type: application/json" -XPUT "http://localhost:19200/hello/_mapping?pretty" -d'
{
"properties":{
"id":{
"type":"long"
},
"name":{
"type":"text",
"index":true,
"analyzer":"ik_pinyin_analyzer"
},
"address":{
"type":"text",
"index":true,
"analyzer":"ik_max_word"
}
}
}'
导入数据
curl --user elastic:123456 -H "Content-Type: application/json" -XPUT "http://localhost:19200/hello/_create/1?pretty" -d'
{
"name":"张三丰",
"address":"武当山"
}'
查询数据
curl --user elastic:123456 -H "Content-Type: application/json" -XPOST "http://localhost:19200/hello/_search?pretty" -d'
{
"query":{
"match":{
"name":"张三"
}
}
}'