1 简介 Elastic Search
是一个基于 Lucene
的开源搜索引擎,它可以快速地储存、搜索和分析海量数据。
2 快速上手(ES7) 2.1 安装 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 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 configvi config/elasticsearch.yml cluster.name: elastic-single-node network.host: 0.0.0.0 discovery.type: single-node xpack.security.enabled: true bootstrap.memory_lock: true 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 docker exec -it elasticsearch bash ./bin/elasticsearch-setup-passwords interactive
2.2 安装 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 configvi config/kibana.yml server.publicBaseUrl: "http://0.0.0.0:5601" 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
3 快速上手(ES8) 3.1 安装 elasticsearch
资源有限,这里演示仅仅为单机单节点。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 docker network create elastic docker pull docker.elastic.co/elasticsearch/elasticsearch:8.2.3 docker run -d --restart=always --name elasticsearch --net elastic -p 9200:9200 -p 9300:9300 \ -v elastic-search-data:/usr/share/elasticsearch/data -e "ES_JAVA_OPTS=-Xms10g -Xmx10g" \ docker.elastic.co/elasticsearch/elasticsearch:8.2.3 docker exec -it elasticsearch bin/elasticsearch-reset-password -u elastic -i docker cp elasticsearch:/usr/share/elasticsearch/config/certs/http_ca.crt . curl --cacert http_ca.crt -u elastic https://localhost:9200
3.2 安装 kibana kibana
是一个基于 elasticsearch 的管理工具,它以可视化的方式,让我们更好的管理数据。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 docker pull docker.elastic.co/kibana/kibana:8.2.3 docker run -d --name kibana --net elastic -p 5601:5601 docker.elastic.co/kibana/kibana:8.2.3 docker exec -it elastic-search bin/elasticsearch-create-enrollment-token --scope kibana docker exec -it kibana bin/kibana-verification-code
4 安装插件 4.1 安装 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 wget https://github.com/medcl/elasticsearch-analysis-ik/releases/download/v7.14.1/elasticsearch-analysis-ik-7.14.1.zip docker cp elasticsearch-analysis-ik-7.14.1.zip elasticsearch:/usr/share/elasticsearch/plugins/ docker exec -it elasticsearch /bin/bash cd pluginsmkdir analysis-ikmv elasticsearch-analysis-ik-7.14.1.zip analysis-ik/cd analysis-ikunzip elasticsearch-analysis-ik-7.14.1.zip rm -f elasticsearch-analysis-ik-7.14.1.zipdocker restart elasticsearch curl -XGET "http://elasticsearch:9200/intent/_analyze?pretty=true" -H 'Content-Type: application/json' -d' { "analyzer": "ik_smart", "text": "上海自来水来自海上" }'
5 常见接口 5.1 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" } } } }'
5.2 Insert Data 1 2 3 4 5 curl -XPOST "http://localhost:9200/intent/_doc" -H "Content-Type: application/json" -d' { "example": "abc", "intent": "123" }'
5.3 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": "这款是偏大的吗" } } }'
5.4 Delete Data 1 curl -XDELETE "http://localhost:9200/intent"
6 常见问题 1 2 3 4 5 6 7 8 9 10 11 12 13 ERROR: max virtual memory areas vm.max_map_count [65530] is too low, increase to at least [262144] sysctl -w vm.max_map_count=262144 sudo vim /etc/sysctl.conf vm.max_map_count=262144 sudo reboot
7 参考