From e9f02cfe94d7c992c6de447bcfd9f18499ab2497 Mon Sep 17 00:00:00 2001 From: Chopper Date: Tue, 18 May 2021 14:19:34 +0800 Subject: [PATCH] =?UTF-8?q?=E5=95=86=E5=93=81=E7=B4=A2=E5=BC=95=E5=88=9D?= =?UTF-8?q?=E5=A7=8B=E5=8C=96=E6=B2=A1=E5=8A=9E=E6=B3=95=E6=A0=B9=E6=8D=AE?= =?UTF-8?q?=E5=B1=9E=E6=80=A7=E6=9F=A5=E8=AF=A2=E9=97=AE=E9=A2=98=E5=A4=84?= =?UTF-8?q?=E7=90=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- config/application.yml | 2 +- .../BaseElasticsearchService.java | 245 +++++++++++++++++- .../serviceimpl/GoodsSkuServiceImpl.java | 2 +- .../serviceimpl/EsGoodsIndexServiceImpl.java | 5 +- 4 files changed, 250 insertions(+), 4 deletions(-) diff --git a/config/application.yml b/config/application.yml index d11ce90c..aa79004d 100644 --- a/config/application.yml +++ b/config/application.yml @@ -212,7 +212,7 @@ jasypt: lili: system: - isDemoSite: true + isDemoSite: false statistics: # 在线人数统计 X 小时。这里设置48,即统计过去48小时每小时在线人数 onlineMember: 48 diff --git a/framework/src/main/java/cn/lili/common/elasticsearch/BaseElasticsearchService.java b/framework/src/main/java/cn/lili/common/elasticsearch/BaseElasticsearchService.java index 906f5876..1cf15c42 100644 --- a/framework/src/main/java/cn/lili/common/elasticsearch/BaseElasticsearchService.java +++ b/framework/src/main/java/cn/lili/common/elasticsearch/BaseElasticsearchService.java @@ -3,12 +3,15 @@ package cn.lili.common.elasticsearch; import cn.hutool.core.bean.BeanUtil; import cn.lili.config.elasticsearch.ElasticsearchProperties; import lombok.extern.slf4j.Slf4j; +import org.assertj.core.api.Assertions; import org.elasticsearch.ElasticsearchException; +import org.elasticsearch.action.ActionListener; import org.elasticsearch.action.admin.indices.delete.DeleteIndexRequest; import org.elasticsearch.action.delete.DeleteRequest; import org.elasticsearch.action.index.IndexRequest; import org.elasticsearch.action.search.SearchRequest; import org.elasticsearch.action.search.SearchResponse; +import org.elasticsearch.action.support.master.AcknowledgedResponse; import org.elasticsearch.action.update.UpdateRequest; import org.elasticsearch.client.HttpAsyncResponseConsumerFactory; import org.elasticsearch.client.RequestOptions; @@ -16,6 +19,7 @@ import org.elasticsearch.client.RestHighLevelClient; import org.elasticsearch.client.indices.CreateIndexRequest; import org.elasticsearch.client.indices.CreateIndexResponse; import org.elasticsearch.client.indices.GetIndexRequest; +import org.elasticsearch.client.indices.PutMappingRequest; import org.elasticsearch.common.settings.Settings; import org.elasticsearch.common.xcontent.XContentType; import org.elasticsearch.index.query.QueryBuilders; @@ -24,6 +28,9 @@ import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Qualifier; import java.io.IOException; +import java.util.concurrent.CountDownLatch; +import java.util.concurrent.TimeUnit; +import java.util.concurrent.atomic.AtomicReference; /** * @author paulG @@ -84,15 +91,251 @@ public abstract class BaseElasticsearchService { // Settings for this index request.settings(Settings.builder().put("index.number_of_shards", elasticsearchProperties.getIndex().getNumberOfShards()).put("index.number_of_replicas", elasticsearchProperties.getIndex().getNumberOfReplicas())); + //创建索引 CreateIndexResponse createIndexResponse = client.indices().create(request, COMMON_OPTIONS); - + createMapping(index); log.info(" whether all of the nodes have acknowledged the request : {}", createIndexResponse.isAcknowledged()); log.info(" Indicates whether the requisite number of shard copies were started for each shard in the index before timing out :{}", createIndexResponse.isShardsAcknowledged()); + return; } catch (Exception e) { + e.printStackTrace(); throw new ElasticsearchException("创建索引 {" + index + "} 失败:" + e.getMessage()); } } + public void createMapping(String index) throws Exception { + String source = + " {\n" + + " \"properties\": {\n" + + " \"_class\": {\n" + + " \"type\": \"text\",\n" + + " \"fields\": {\n" + + " \"keyword\": {\n" + + " \"type\": \"keyword\",\n" + + " \"ignore_above\": 256\n" + + " }\n" + + " }\n" + + " },\n" + + " \"attrList\": {\n" + + " \"type\": \"nested\",\n" + + " \"properties\": {\n" + + " \"name\": {\n" + + " \"type\": \"keyword\"\n" + + " },\n" + + " \"type\": {\n" + + " \"type\": \"long\"\n" + + " },\n" + + " \"value\": {\n" + + " \"type\": \"keyword\"\n" + + " }\n" + + " }\n" + + " },\n" + + " \"brandId\": {\n" + + " \"type\": \"text\",\n" + + " \"fielddata\": true,\n" + + " \"fields\": {\n" + + " \"keyword\": {\n" + + " \"type\": \"keyword\",\n" + + " \"ignore_above\": 256\n" + + " }\n" + + " }\n" + + " },\n" + + " \"buyCount\": {\n" + + " \"type\": \"long\"\n" + + " },\n" + + " \"releaseTime\": {\n" + + " \"type\": \"text\",\n" + + " \"fielddata\": true, \n" + + " \"fields\": {\n" + + " \"keyword\": {\n" + + " \"type\": \"keyword\",\n" + + " \"ignore_above\": 256\n" + + " }\n" + + " }\n" + + " },\n" + + " \"categoryPath\": {\n" + + " \"type\": \"text\",\n" + + " \"fielddata\": true,\n" + + " \"fields\": {\n" + + " \"keyword\": {\n" + + " \"type\": \"keyword\",\n" + + " \"ignore_above\": 256\n" + + " }\n" + + " }\n" + + " },\n" + + " \"commentNum\": {\n" + + " \"type\": \"long\"\n" + + " },\n" + + " \"goodsId\": {\n" + + " \"type\": \"text\",\n" + + " \"fields\": {\n" + + " \"keyword\": {\n" + + " \"type\": \"keyword\",\n" + + " \"ignore_above\": 256\n" + + " }\n" + + " }\n" + + " },\n" + + " \"goodsName\": {\n" + + " \"type\": \"text\",\n" + + " \"fielddata\": true, \n" + + " \"analyzer\": \"ik_max_word\",\n" + + " \"fields\": {\n" + + " \"keyword\": {\n" + + " \"type\": \"keyword\",\n" + + " \"ignore_above\": 256\n" + + " }\n" + + " }\n" + + " },\n" + + " \"grade\": {\n" + + " \"type\": \"float\"\n" + + " },\n" + + " \"highPraiseNum\": {\n" + + " \"type\": \"long\"\n" + + " },\n" + + " \"id\": {\n" + + " \"type\": \"text\",\n" + + " \"fields\": {\n" + + " \"keyword\": {\n" + + " \"type\": \"keyword\",\n" + + " \"ignore_above\": 256\n" + + " }\n" + + " }\n" + + " },\n" + + " \"intro\": {\n" + + " \"type\": \"text\",\n" + + " \"fields\": {\n" + + " \"keyword\": {\n" + + " \"type\": \"keyword\",\n" + + " \"ignore_above\": 256\n" + + " }\n" + + " }\n" + + " },\n" + + " \"isAuth\": {\n" + + " \"type\": \"text\",\n" + + " \"fields\": {\n" + + " \"keyword\": {\n" + + " \"type\": \"keyword\",\n" + + " \"ignore_above\": 256\n" + + " }\n" + + " }\n" + + " },\n" + + " \"marketEnable\": {\n" + + " \"type\": \"text\",\n" + + " \"fielddata\": true, \n" + + " \"fields\": {\n" + + " \"keyword\": {\n" + + " \"type\": \"keyword\",\n" + + " \"ignore_above\": 256\n" + + " }\n" + + " }\n" + + " },\n" + + " \"mobileIntro\": {\n" + + " \"type\": \"text\",\n" + + " \"fields\": {\n" + + " \"keyword\": {\n" + + " \"type\": \"keyword\",\n" + + " \"ignore_above\": 256\n" + + " }\n" + + " }\n" + + " },\n" + + " \"point\": {\n" + + " \"type\": \"long\"\n" + + " },\n" + + " \"price\": {\n" + + " \"type\": \"float\"\n" + + " },\n" + + " \"salesModel\": {\n" + + " \"type\": \"text\",\n" + + " \"fields\": {\n" + + " \"keyword\": {\n" + + " \"type\": \"keyword\",\n" + + " \"ignore_above\": 256\n" + + " }\n" + + " }\n" + + " },\n" + + " \"selfOperated\": {\n" + + " \"type\": \"boolean\"\n" + + " },\n" + + " \"sellerId\": {\n" + + " \"type\": \"text\",\n" + + " \"fields\": {\n" + + " \"keyword\": {\n" + + " \"type\": \"keyword\",\n" + + " \"ignore_above\": 256\n" + + " }\n" + + " }\n" + + " },\n" + + " \"sellerName\": {\n" + + " \"type\": \"text\",\n" + + " \"fielddata\": true, \n" + + " \"fields\": {\n" + + " \"keyword\": {\n" + + " \"type\": \"keyword\",\n" + + " \"ignore_above\": 256\n" + + " }\n" + + " }\n" + + " },\n" + + " \"shopCategoryPath\": {\n" + + " \"type\": \"text\",\n" + + " \"fielddata\": true, \n" + + " \"fields\": {\n" + + " \"keyword\": {\n" + + " \"type\": \"keyword\",\n" + + " \"ignore_above\": 256\n" + + " }\n" + + " }\n" + + " },\n" + + " \"sn\": {\n" + + " \"type\": \"text\",\n" + + " \"fields\": {\n" + + " \"keyword\": {\n" + + " \"type\": \"keyword\",\n" + + " \"ignore_above\": 256\n" + + " }\n" + + " }\n" + + " },\n" + + " \"thumbnail\": {\n" + + " \"type\": \"text\",\n" + + " \"fields\": {\n" + + " \"keyword\": {\n" + + " \"type\": \"keyword\",\n" + + " \"ignore_above\": 256\n" + + " }\n" + + " }\n" + + " }\n" + + " }\n" + + " }\n"; + + PutMappingRequest request = new PutMappingRequest(index) + .source(source, XContentType.JSON); +// AcknowledgedResponse putMappingResponse = client.indices().putMapping(request, +// RequestOptions.DEFAULT); +// +// boolean acknowledged = putMappingResponse.isAcknowledged(); +// if (acknowledged) { +// log.error("Succeed to put mapping"); +// } + CountDownLatch latch = new CountDownLatch(1); + AtomicReference response = new AtomicReference(); + client.indices().putMappingAsync( + request, + RequestOptions.DEFAULT, + new ActionListener() { + @Override + public void onResponse(AcknowledgedResponse r) { + response.set(r); + latch.countDown(); + } + + @Override + public void onFailure(Exception e) { + latch.countDown(); + } + }); + latch.await(10, TimeUnit.SECONDS); + Assertions.assertThat(((AcknowledgedResponse) response.get()).isAcknowledged()).isTrue(); + } + /** * Description: 判断某个index是否存在 * diff --git a/framework/src/main/java/cn/lili/modules/goods/serviceimpl/GoodsSkuServiceImpl.java b/framework/src/main/java/cn/lili/modules/goods/serviceimpl/GoodsSkuServiceImpl.java index a7e86c3e..3ec76b83 100644 --- a/framework/src/main/java/cn/lili/modules/goods/serviceimpl/GoodsSkuServiceImpl.java +++ b/framework/src/main/java/cn/lili/modules/goods/serviceimpl/GoodsSkuServiceImpl.java @@ -601,7 +601,7 @@ public class GoodsSkuServiceImpl extends ServiceImpl i sku.setThumbnail(thumbnail); //规格信息 - sku.setId(map.getOrDefault("id", "").toString()); + sku.setId(Convert.toStr(map.get("id"),"").toString()); sku.setSn(Convert.toStr(map.get("sn"))); sku.setWeight(Convert.toDouble(map.get("weight"), 0D)); sku.setPrice(Convert.toDouble(map.get("price"), 0D)); diff --git a/framework/src/main/java/cn/lili/modules/search/serviceimpl/EsGoodsIndexServiceImpl.java b/framework/src/main/java/cn/lili/modules/search/serviceimpl/EsGoodsIndexServiceImpl.java index 257714ae..719e5a75 100644 --- a/framework/src/main/java/cn/lili/modules/search/serviceimpl/EsGoodsIndexServiceImpl.java +++ b/framework/src/main/java/cn/lili/modules/search/serviceimpl/EsGoodsIndexServiceImpl.java @@ -34,6 +34,7 @@ import org.elasticsearch.search.SearchHit; import org.mybatis.spring.MyBatisSystemException; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.data.domain.Page; +import org.springframework.data.elasticsearch.core.ElasticsearchRestTemplate; import org.springframework.stereotype.Service; import org.springframework.util.ObjectUtils; @@ -61,6 +62,8 @@ public class EsGoodsIndexServiceImpl extends BaseElasticsearchService implements private GoodsWordsService goodsWordsService; @Autowired private PromotionService promotionService; + @Autowired + private ElasticsearchRestTemplate elasticsearchRestTemplate; @Override public void addIndex(EsGoodsIndex goods) { @@ -149,7 +152,7 @@ public class EsGoodsIndexServiceImpl extends BaseElasticsearchService implements public void initIndex(List goodsIndexList) { //索引名称拼接 String indexName = elasticsearchProperties.getIndexPrefix() + "_" + EsSuffix.GOODS_INDEX_NAME; - // deleteIndexRequest(indexName); + //如果索引不存在,则创建索引 if (!indexExist(indexName)) { createIndexRequest(indexName);