docker es官方镜像 安装es使用
官方镜像:
docker pull docker.elastic.co/elasticsearch/elasticsearch:6.5.4
要设置/root/data/esdata可读写
docker run --name es_test_01 -p 9200:9200 -p 9300:9300 -v /root/data/esdata:/usr/share/elasticsearch/data -e ES_JAVA_OPTS="-Xms300m -Xmx300m" -e "discovery.type=single-node" docker.elastic.co/elasticsearch/elasticsearch:6.5.4
直接后台加-d:
docker run -d --name es_test_01 -p 9200:9200 -p 9300:9300 -v /root/data/esdata:/usr/share/elasticsearch/data -e ES_JAVA_OPTS="-Xms300m -Xmx300m" -e "discovery.type=single-node" docker.elastic.co/elasticsearch/elasticsearch:6.5.4
安装ik(版本要对应,之后重启容器):
docker exec -it xxx /bin/bash
./bin/elasticsearch-plugin install https://github.com/medcl/elasticsearch-analysis-ik/releases/download/v6.5.4/elasticsearch-analysis-ik-6.5.4.zip
docker重启:docker restart xxx
测试是否能连接:curl -X GET localhost:9200/
如果能连接,建立索引:/usr/bin/python3.6 /root/backend/deploy_test_server/create_test_esmapping.py 脚本如下
如果能建立索引,查询索引是否成功, curl -XGET localhost:9200/goodlook/post/_search?pretty
# coding=utf-8 from __future__ import print_function from elasticsearch import Elasticsearch # /usr/bin/python3.6 /root/backend/deploy_test_server/create_test_esmapping.py # ES_HOSTS = ['elastic@localhost:9200'] ES_HOSTS = ['localhost:9200'] INDEX_NAME = 'goodlook' DOC_TYPE = 'post' es = Elasticsearch(hosts=ES_HOSTS)
# 如果存在索引先删除, 不存在可以注释掉 res = es.indices.delete(index=INDEX_NAME) request_body = { "mappings": { DOC_TYPE: { "properties": { "star": { "type": "text", "analyzer": "ik_max_word", "search_analyzer": "ik_smart" }, "gender": { "type": "text", "analyzer": "ik_max_word", "search_analyzer": "ik_smart" }, "age": { "type": "text", "analyzer": "ik_max_word", "search_analyzer": "ik_smart" }, "scene": { "type": "text", "analyzer": "ik_max_word", "search_analyzer": "ik_smart" }, "season": { "type": "text", "analyzer": "ik_max_word", "search_analyzer": "ik_smart" }, "style": { "type": "text", "analyzer": "ik_max_word", "search_analyzer": "ik_smart" }, "color": { "type": "text", "analyzer": "ik_max_word", "search_analyzer": "ik_smart" }, "items": { "type": "nested", "properties": { "category": { "type": "text", "analyzer": "ik_max_word", "search_analyzer": "ik_smart" }, "color": { "type": "text", "analyzer": "ik_max_word", "search_analyzer": "ik_smart" }, "attributes": { "type": "text", "analyzer": "ik_max_word", "search_analyzer": "ik_smart" } } } } } } } res = es.indices.create(index=INDEX_NAME, ignore=400, body=request_body) print(" response: {}".format(res))
docker-compose写法
version: '2.0'
services:
elasticsearch:
image: docker.elastic.co/elasticsearch/elasticsearch:6.5.4
container_name: elasticsearch
environment:
- "ES_JAVA_OPTS=-Xms512m -Xmx512m"
volumes:
- esdata1:/usr/share/elasticsearch/data
ports:
- 9200:9200
networks:
- esnet
volumes:
esdata1:
driver: local