浏览代码

搜索索引更新

chao 4 年之前
父节点
当前提交
5fd5d04380
共有 1 个文件被更改,包括 68 次插入16 次删除
  1. 68 16
      src/main/java/com/caimei365/commodity/service/impl/SearchIndexServiceImpl.java

+ 68 - 16
src/main/java/com/caimei365/commodity/service/impl/SearchIndexServiceImpl.java

@@ -193,6 +193,10 @@ public class SearchIndexServiceImpl implements SearchIndexService {
      */
     @Override
     public ResponseJson<Integer> updateProductIndexById(Integer productId){
+        if (null == productId) {
+            log.info(">>>更新商品,商品Id不能为空!");
+            return ResponseJson.error("商品Id不能为空", null);
+        }
         ProductDO product = searchMapper.searchProductById(productId);
         if (null != product) {
             // 定义星范商品数据
@@ -218,8 +222,13 @@ public class SearchIndexServiceImpl implements SearchIndexService {
                 Integer count = searchQueryService.getCountByDocId("product", productId);
                 if (count > 1) {
                     log.info("商品重复,重复商品id>>>>>>>>>>>" + productId);
-                    deleteProductIndexById(productId);
-                    return updateProductIndexById(productId);
+                    if (mainId > 0) {
+                        // 删除主文档记录
+                        deleteDocByTypeId("id", mainId, "search_main");
+                        return updateProductIndexById(productId);
+                    }
+                    // 主文档不存在,删除商品文档
+                    return deleteProductIndexById(productId);
                 } else {
                     // 推送到阿里云
                     pushProductDocument(mainList, productList, mallProductList);
@@ -243,6 +252,10 @@ public class SearchIndexServiceImpl implements SearchIndexService {
      */
     @Override
     public ResponseJson<Integer> updateSupplierIndexById(Integer shopId) {
+        if (null == shopId) {
+            log.info(">>>更新供应商,供应商Id不能为空!");
+            return ResponseJson.error("供应商Id不能为空", null);
+        }
         SupplierDO supplier = searchMapper.searchSupplierById(shopId);
         if(null != supplier){
              // 定义供应商文档数据
@@ -264,8 +277,13 @@ public class SearchIndexServiceImpl implements SearchIndexService {
                 Integer count = searchQueryService.getCountByDocId("shop", shopId);
                 if (count > 1) {
                     log.info("供应商重复,重复供应商id>>>>>>>>>>>" + shopId);
-                    deleteSupplierIndexById(shopId);
-                    return updateSupplierIndexById(shopId);
+                    if (mainId > 0) {
+                        // 删除主文档记录
+                        deleteDocByTypeId("id", mainId, "search_main");
+                        return updateSupplierIndexById(shopId);
+                    }
+                    // 主文档不存在,删除供应商文档
+                    return deleteSupplierIndexById(shopId);
                 } else {
                     // 将文档列表转换成Json串,并推送到阿里云
                     pushSupplierDocument(mainList, supplierList);
@@ -289,6 +307,10 @@ public class SearchIndexServiceImpl implements SearchIndexService {
      */
     @Override
     public ResponseJson<Integer> updateEquipmentIndexById(Integer equipmentId) {
+        if (null == equipmentId) {
+            log.info(">>>更新项目仪器,项目仪器Id不能为空!");
+            return ResponseJson.error("项目仪器Id不能为空", null);
+        }
         EquipmentDO equipment = searchMapper.searchEquipmentById(equipmentId);
         if(null != equipment){
             // 定义项目仪器文档数据
@@ -310,8 +332,13 @@ public class SearchIndexServiceImpl implements SearchIndexService {
                 Integer count = searchQueryService.getCountByDocId("equipment", equipmentId);
                 if (count > 1) {
                     log.info("项目仪器重复,重复项目仪器id>>>>>>>>>>>" + equipmentId);
-                    deleteEquipmentIndexById(equipmentId);
-                    return updateEquipmentIndexById(equipmentId);
+                    if (mainId > 0) {
+                        // 删除主文档记录
+                        deleteDocByTypeId("id", mainId, "search_main");
+                        return updateEquipmentIndexById(equipmentId);
+                    }
+                    // 主文档不存在,删除项目仪器文档
+                    return deleteEquipmentIndexById(equipmentId);
                 } else {
                     // 将文档列表转换成Json串,并推送到阿里云
                     pushEquipmentDocument(mainList, equipmentList);
@@ -335,6 +362,10 @@ public class SearchIndexServiceImpl implements SearchIndexService {
      */
     @Override
     public ResponseJson<Integer> updateArticleIndexById(Integer articleId) {
+        if (null == articleId) {
+            log.info(">>>更新文章,文章Id不能为空!");
+            return ResponseJson.error("文章Id不能为空", null);
+        }
         ArticleDO article = searchMapper.searchArticleById(articleId);
         if(null != article){
             // 定义文章文档数据
@@ -356,8 +387,13 @@ public class SearchIndexServiceImpl implements SearchIndexService {
                 Integer count = searchQueryService.getCountByDocId("article", articleId);
                 if (count > 1) {
                     log.info("文章重复,重复文章id>>>>>>>>>>>" + articleId);
-                    deleteArticleIndexById(articleId);
-                    return updateArticleIndexById(articleId);
+                    if (mainId > 0) {
+                        // 删除主文档记录
+                        deleteDocByTypeId("id", mainId, "search_main");
+                        return updateArticleIndexById(articleId);
+                    }
+                    // 主文档不存在,删除文章文档
+                    return deleteArticleIndexById(articleId);
                 } else {
                     // 将主文档列表转换成Json串,并推送到阿里云
                     pushArticleDocument(mainList, articleList);
@@ -526,9 +562,13 @@ public class SearchIndexServiceImpl implements SearchIndexService {
                     deleteDoc.put("m_id", mallId);
                 }
             }
-            for (Integer productId : invalidIds) {
+            for (Integer invalidId : invalidIds) {
                 // 根据ID(主键)删除文档
-                deleteDoc.put("p_id", productId);
+                deleteDoc.put("p_id", invalidId);
+                Integer mainId = searchQueryService.getIdByDocId("product", invalidId);
+                if (mainId > -1){
+                    deleteDoc.put("id", mainId);
+                }
             }
             try {
                 String deleteJsonStr = setDeleteDocument(deleteDoc);
@@ -651,9 +691,13 @@ public class SearchIndexServiceImpl implements SearchIndexService {
             List<Integer> invalidIds = searchMapper.findSupplierInvalidIds();
             // 删除文档只需要设置需删除文档主键值即可
             Map<String, Object> deleteDoc = Maps.newLinkedHashMap();
-            for (Integer productId : invalidIds) {
+            for (Integer invalidId : invalidIds) {
                 // 根据ID(主键)删除文档
-                deleteDoc.put("s_id", productId);
+                deleteDoc.put("s_id", invalidId);
+                Integer mainId = searchQueryService.getIdByDocId("shop", invalidId);
+                if (mainId > -1){
+                    deleteDoc.put("id", mainId);
+                }
             }
             try {
                 String deleteJsonStr = setDeleteDocument(deleteDoc);
@@ -765,9 +809,13 @@ public class SearchIndexServiceImpl implements SearchIndexService {
             List<Integer> invalidIds = searchMapper.findEquipmentInvalidIds();
             // 删除文档只需要设置需删除文档主键值即可
             Map<String, Object> deleteDoc = Maps.newLinkedHashMap();
-            for (Integer productId : invalidIds) {
+            for (Integer invalidId : invalidIds) {
                 // 根据ID(主键)删除文档
-                deleteDoc.put("e_id", productId);
+                deleteDoc.put("e_id", invalidId);
+                Integer mainId = searchQueryService.getIdByDocId("equipment", invalidId);
+                if (mainId > -1){
+                    deleteDoc.put("id", mainId);
+                }
             }
             try {
                 String deleteJsonStr = setDeleteDocument(deleteDoc);
@@ -879,9 +927,13 @@ public class SearchIndexServiceImpl implements SearchIndexService {
             List<Integer> invalidIds = searchMapper.findArticleInvalidIds();
             // 删除文档只需要设置需删除文档主键值即可
             Map<String, Object> deleteDoc = Maps.newLinkedHashMap();
-            for (Integer productId : invalidIds) {
+            for (Integer invalidId : invalidIds) {
                 // 根据ID(主键)删除文档
-                deleteDoc.put("a_id", productId);
+                deleteDoc.put("a_id", invalidId);
+                Integer mainId = searchQueryService.getIdByDocId("article", invalidId);
+                if (mainId > -1){
+                    deleteDoc.put("id", mainId);
+                }
             }
             try {
                 String deleteJsonStr = setDeleteDocument(deleteDoc);