-
docker拉取elasticsearch
docker pull elasticsearch:7.0.7
-
创建挂载目录
mkdir /data/es -p
-
启动容器
docker run --name elasticsearch -d -e ES_JAVA_OPTS="-Xms512m -Xmx512m" -e "discovery.type=single-node" -p 9200:9200 -p 9300:9300 elasticsearch:7.7.0
-
从容器内部复制主要文件到宿主机,用作配置文件挂载目录
docker cp elasticsearch:/usr/share/elasticsearch /data/es
-
删除原容器
docker rm -f elasticsearch
-
以挂载形式启动容器
docker run --name elasticsearch -d -e ES_JAVA_OPTS="-Xms512m -Xmx512m" -e "discovery.type=single-node" -p 9200:9200 -p 9300:9300 -v /data/es/elasticsearch:/usr/share/elasticsearch elasticsearch:7.7.0 --name表示镜像启动后的容器名称 -d: 后台运行容器,并返回容器ID; -e: 指定容器内的环境变量 -p: 指定端口映射,格式为:主机(宿主)端口:容器端口 -v: 指定宿主机和容器之间的映射,格式为:宿主机文件目录:容器内部目录
-
服务器开放端口
-
测试运行
docker ps -f name=elasticsearch 第9、10两步操作也可以在挂载目录里来执行,注意映射文件就行
-
安装ik分词器插件
下载分词器压缩包,下载地址: https://github.com/medcl/elasticsearch-analysis-ik/releases/download/v7.7.0/elasticsearch-analysis-ik-7.7.0.zip 将IK分词器上传到/tmp目录中(xftp) #将压缩包移动到容器中 docker cp /tmp/elasticsearch-analysis-ik-7.7.0.zip elasticsearch:/usr/share/elasticsearch/plugins #进入容器 docker exec -it elasticsearch /bin/bash #创建目录 mkdir /usr/share/elasticsearch/plugins/ik #将文件压缩包移动到ik中 mv /usr/share/elasticsearch/plugins/elasticsearch-analysis-ik-7.7.0.zip /usr/share/elasticsearch/plugins/ik #进入目录 cd /usr/share/elasticsearch/plugins/ik #解压 unzip elasticsearch-analysis-ik-7.7.0.zip #删除压缩包 rm -rf elasticsearch-analysis-ik-7.7.0.zip 如果已经有解压好的分词插件文件,直接放到映射目录文件夹下即可 /data/es/elasticsearch/plugins/ ik分词插件、pinyin分词插件
-
导入自定义同义词过滤器
#进入容器 docker exec -it elasticsearch /bin/bash #进入config目录 cd /usr/share/elasticsearch/config #创建词本目录 mkdir analysis文件夹 #创建过滤器词本文件,并填充 touch synonyms.txt & vi synoyms.txt touch tjdata-shop-synonym.txt & vi tjdata-shop-synonym.txt
-
安装kibana
#拉取kibana镜像 docker pull kibana:7.6.2 #启动kibana容器 docker run --name kibana -e ELASTICSEARCH_HOSTS=http://106.54.183.28:9200 -p 5601:5601 -d kibana:7.6.2 #通过浏览器访问 http://<宿主机IP>:5601 来打开 Kibana 的 Web UI
-
创建索引结构(新的索引结构有所变化)
#查看索引结构 curl -XGET 'http://localhost:9200/products?pretty' #进入kibana创建索引 PUT /products { "settings": { "number_of_shards": 1, "analysis": { "filter": { "my_synonym_filter": { "type": "synonym", "synonyms_path": "analysis/synonyms.txt" } }, "analyzer": { "ik_max_word": { "tokenizer": "ik_max_word", "filter": [ "lowercase", "asciifolding", "my_synonym_filter" ] } } } }, "mappings": { "properties": { "alterTime": {"type": "long"}, "asGoodsKeyWord": { "type": "completion", "analyzer": "simple", "preserve_separators": true, "preserve_position_increments": true, "max_input_length": 50 }, "collectNum": {"type": "long"}, "count": {"type": "long"}, "coverHeight": {"type": "long"}, "coverWidth": {"type": "long"}, "createTime": {"type": "long"}, "goodsAddr": { "type": "text", "fields": {"keyword": {"type": "keyword", "ignore_above": 256}} }, "goodsAddrCity": { "type": "text", "fields": {"keyword": {"type": "keyword", "ignore_above": 256}} }, "goodsAddrCounty": { "type": "text", "fields": {"keyword": {"type": "keyword", "ignore_above": 256}} }, "goodsAddrJson": { "type": "text", "fields": {"keyword": {"type": "keyword", "ignore_above": 256}} }, "goodsAddrProvince": { "type": "text", "fields": {"keyword": {"type": "keyword", "ignore_above": 256}} }, "goodsAddrStreet": { "type": "text", "fields": {"keyword": {"type": "keyword", "ignore_above": 256}} }, "goodsCover": { "type": "text", "fields": {"keyword": {"type": "keyword", "ignore_above": 256}} }, "goodsDes": {"type": "text", "analyzer": "ik_max_word"}, "goodsId": {"type": "long"}, "goodsImgUrl": { "type": "text", "fields": {"keyword": {"type": "keyword", "ignore_above": 256}} }, "goodsKeyWord": { "type": "text", "fields": {"keyword": {"type": "keyword", "ignore_above": 256}} }, "goodsName": {"type": "text", "analyzer": "ik_max_word"}, "goodsNameCompletion": { "type": "completion", "analyzer": "simple", "preserve_separators": true, "preserve_position_increments": true, "max_input_length": 50 }, "goodsPrice": {"type": "float"}, "goodsType": { "type": "text", "fields": {"keyword": {"type": "keyword", "ignore_above": 256}} }, "high": {"type": "long"}, "hours": {"type": "long"}, "imgList": { "type": "text", "fields": {"keyword": {"type": "keyword", "ignore_above": 256}} }, "isPublic": {"type": "long"}, "isSell": {"type": "long"}, "listKeyWord": { "type": "text", "fields": {"keyword": {"type": "keyword", "ignore_above": 256}} }, "originalPrice": {"type": "float"}, "page": {"type": "long"}, "publisherIp": { "type": "text", "fields": {"keyword": {"type": "keyword", "ignore_above": 256}} }, "publisherPhoneModel": { "type": "text", "fields": {"keyword": {"type": "keyword", "ignore_above": 256}} }, "receiveWay": { "type": "text", "fields": {"keyword": {"type": "keyword", "ignore_above": 256}} }, "seller": { "properties": { "addr": { "type": "text", "fields": {"keyword": {"type": "keyword", "ignore_above": 256}} }, "alterTime": {"type": "long"}, "createTime": {"type": "long"}, "headImage": { "type": "text", "fields": {"keyword": {"type": "keyword", "ignore_above": 256}} }, "id": {"type": "long"}, "name": { "type": "text", "fields": {"keyword": {"type": "keyword", "ignore_above": 256}} }, "openId": { "type": "text", "fields": {"keyword": {"type": "keyword", "ignore_above": 256}} }, "page": {"type": "long"}, "size": {"type": "long"} } }, "size": {"type": "long"}, "sortId": {"type": "long"}, "userId": {"type": "long"}, "views": {"type": "long"}, "wide": {"type": "long"} } } }
-
重启es
docker restart es
-
向es中导入数据
@Override public Result<?> selectAllProduct() { log.info("SqGoodsServiceImpl selectAllProduct"); try { //1、构建 删除索引请求 Integer goodsId = getGoodsId(); for (int a = 0; a <= 1500; a++) { esUtil.deleteProduct(String.valueOf(a)); } } catch (Exception e) { e.printStackTrace(); } List<SqGoodsEntity> list = sqGoodsDao.selectAllGoods(); if (list == null) { log.error("SqGoodsServiceImpl selectAllProduct 查询全部商品失败"); return Result.error("查询全部商品失败"); } list.stream().forEach(goods -> { goods.setAsGoodsKeyWord(JSON.parseArray(goods.getGoodsKeyWord(), String.class)); goods.setListKeyWord(JSON.parseArray(goods.getGoodsKeyWord(), String.class)); goods.setImgList(JSON.parseArray(goods.getGoodsImgUrl(), String.class)); //es添加商品类型 LambdaQueryWrapper<SqSortEntity> queryWrapper = new LambdaQueryWrapper<SqSortEntity>().eq(SqSortEntity::getSortId, goods.getSortId()); SqSortEntity sqSortEntity = sqSortDao.selectOne(queryWrapper); goods.setGoodsType(sqSortEntity.getSortName()); }); //创建批量新增请求对象 BulkRequest request = new BulkRequest(); for (int i = 0; i < list.size(); i++) { log.info("数据有:" + list.size()); request.add( new IndexRequest("products") .id("" + list.get(i).getGoodsId()) .source(JSON.toJSONString(list.get(i)), XContentType.JSON) ); } //插入是否成功标志,false表示成功,true表示失败 boolean flag = false; try { //客户端发送请求,获取响应对象 BulkResponse responses = client.bulk(request, RequestOptions.DEFAULT); flag = responses.hasFailures(); //结果为false,代表没有失败,即插入成功 log.info("批量插入是否失败:" + responses.hasFailures()); } catch (Exception e) { e.printStackTrace(); } if (flag == true) { return Result.msg("插入失败"); } return Result.msg("插入成功"); }
二、kibana(见上方)
-
为kibana和elasticsearch设置密码
-
进入elasticsearch内部或者找到映射文件elasticsearch.yml
-
修改文件
cluster.name: "docker-cluster" network.host: 0.0.0.0 xpack.security.enabled: true xpack.license.self_generated.type: basic xpack.security.transport.ssl.enabled: true
-
进入/elasticsearch/bin目录
-
执行命令
elasticsearch-setup-passwords interactive(外部不行就进入容器内部到/bin目录下执行)
-
接下来疯狂输入密码,几个密码最好保持相同
-
重启es容器
-
进入kibana的配置文件(内部容器或者映射目录文件kibana.yml)
-
修改文件kibana.yml
# # ** THIS IS AN AUTO-GENERATED FILE ** # # Default Kibana configuration for docker target server.name: kibana server.host: "0" elasticsearch.hosts: [ "http://elasticsearch:9200" ] xpack.monitoring.ui.container.elasticsearch.enabled: true i18n.locale: zh-CN elasticsearch.username: "elastic" elasticsearch.password: "Shiqing601"
-
重启kibana
-
参考地址
https://blog.csdn.net/u012999325/article/details/105451855
-