Elastic Search 入门

1 简介

Elastic Search 是一个基于 Lucene 的开源搜索引擎,它可以快速地储存、搜索和分析海量数据。

2 快速上手

使用 docker 安装 elasticsearch

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# https://www.elastic.co/guide/en/kibana/current/docker.html
docker network create elastic
docker pull docker.elastic.co/elasticsearch/elasticsearch:7.14.1
sudo mkdir /usr/share/elasticsearch/
sudo chmod 777 /usr/share/elasticsearch/
mkdir config
vi config/elasticsearch.yml
# add
cluster.name: elastic-single-node
network.host: 0.0.0.0
discovery.type: single-node
xpack.security.enabled: true # 安全机制开启
bootstrap.memory_lock: true # 锁定物理内存地址,避免es使用swap交换分区

docker run -d --restart=always --name elasticsearch --net elastic -p 9200:9200 -p 9300:9300 \
-v /usr/share/elasticsearch/config/elasticsearch.yml:/usr/share/elasticsearch/config/elasticsearch.yml \
-v /usr/share/elasticsearch/data:/usr/share/elasticsearch/data \
-e "ES_JAVA_OPTS=-Xms5120m -Xmx10240m" \
docker.elastic.co/elasticsearch/elasticsearch:7.14.1

# set password
docker exec -it elasticsearch bash
./bin/elasticsearch-setup-passwords interactive
# set all password 123456

安装 kibana

kibana 是一个基于 elasticsearch 的管理工具,它以可视化的方式,让我们更好的管理数据。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
docker pull docker.elastic.co/kibana/kibana:7.14.1
sudo mkdir /usr/share/kibana/
sudo chmod 777 /usr/share/kibana/
cd /usr/share/kibana/
mkdir config
vi config/kibana.yml
# add
server.publicBaseUrl: "http://0.0.0.0:5601" # TODO TEST
server.host: "0"
server.shutdownTimeout: "5s"
elasticsearch.hosts: [ "http://elasticsearch:9200" ]
elasticsearch.username: kibana_system
elasticsearch.password: 123456
i18n.locale: zh-CN

docker run -d --restart=always --name kibana --net elastic -p 5601:5601 \
-v /usr/share/kibana/config/kibana.yml:/usr/share/kibana/config/kibana.yml \
docker.elastic.co/kibana/kibana:7.14.1
# open in http://localhost:5601

安装 IK 分词插件

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# download the same version package in  https://github.com/medcl/elasticsearch-analysis-ik/releases/tag/v7.14.1
wget https://github.com/medcl/elasticsearch-analysis-ik/releases/download/v7.14.1/elasticsearch-analysis-ik-7.14.1.zip

# copy to container
docker cp elasticsearch-analysis-ik-7.14.1.zip elasticsearch:/usr/share/elasticsearch/plugins/

# unzip plugin
docker exec -it elasticsearch /bin/bash
cd plugins
mkdir analysis-ik
mv elasticsearch-analysis-ik-7.14.1.zip analysis-ik/
cd analysis-ik
unzip elasticsearch-analysis-ik-7.14.1.zip
rm -f elasticsearch-analysis-ik-7.14.1.zip

# restart elasticsearch
docker restart elasticsearch

# test analysis
curl -XGET "http://elasticsearch:9200/intent/_analyze?pretty=true" -H 'Content-Type: application/json' -d'
{
"analyzer": "ik_smart",
"text": "上海自来水来自海上"
}'

3 常见接口

Create Index

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
curl -XPUT "http://localhost:9200/intent" -H 'Content-Type: application/json' -d'
{
"settings": {
"number_of_replicas": 1,
"number_of_shards": 5
},
"mappings": {
"properties": {
"question": {
"type": "text",
"analyzer": "ik_max_word",
"search_analyzer": "ik_max_word"
},
"intent": {
"type": "text"
}
}
}
}'

Insert Data

1
2
3
4
5
curl -XPOST "http://localhost:9200/intent/_doc" -H "Content-Type: application/json" -d'
{
"example": "abc",
"intent": "123"
}'

Search Data

1
2
3
4
5
6
7
8
curl -XGET "http://localhost:9200/intent/_search" -H 'Content-Type: application/json' -d'
{
"query": {
"match": {
"question": "这款是偏大的吗"
}
}
}'

Delete Data

1
curl -XDELETE "http://localhost:9200/intent"

4 参考

评论