diff --git a/code/smart-community/epmet-module/gov-voice/gov-voice-server/src/main/java/com/epmet/constant/ElasticsearchArticleConstant.java b/code/smart-community/epmet-module/gov-voice/gov-voice-server/src/main/java/com/epmet/constant/ElasticsearchArticleConstant.java new file mode 100644 index 00000000..514bd46f --- /dev/null +++ b/code/smart-community/epmet-module/gov-voice/gov-voice-server/src/main/java/com/epmet/constant/ElasticsearchArticleConstant.java @@ -0,0 +1,26 @@ +package com.epmet.constant; + +/** + * ESDTO常量 + * @author sun + * @since 1.0.0 + */ +public interface ElasticsearchArticleConstant { + + /** + * category 资源类型 : + */ + String APPLICATION = "APPLICATION"; + String SERVICE = "SERVICE"; + String ARTICLE = "ARTICLE"; + String PRODUCT = "PRODUCT"; + + /** + * source 资源来源: + */ + String SELF = "SELF"; + String HUI_SHENG_HUO = "HUI_SHENG_HUO"; + String WU_YE = "WU_YE"; + String ZHAO_PIN = "ZHAO_PIN"; + String SHE_QU = "SHE_QU"; +} diff --git a/code/smart-community/epmet-module/gov-voice/gov-voice-server/src/main/java/com/epmet/controller/ArticleController.java b/code/smart-community/epmet-module/gov-voice/gov-voice-server/src/main/java/com/epmet/controller/ArticleController.java index da150691..6e7ac6d7 100644 --- a/code/smart-community/epmet-module/gov-voice/gov-voice-server/src/main/java/com/epmet/controller/ArticleController.java +++ b/code/smart-community/epmet-module/gov-voice/gov-voice-server/src/main/java/com/epmet/controller/ArticleController.java @@ -530,4 +530,22 @@ public class ArticleController { return new Result(); } + /** + * ES初始化索引 所有相关表都删除吧 + * article 文章表 + * article_content 文章内容表 + * article_cover 文章封面表 + * article_operate_record 文章操作记录表 + * article_publish_range 文章发布范围表 + * article_tags 文章标签表 + * article_visit_record 文章访问记录表 + * @param tokenDto + * @return + */ + @PostMapping("es") + public Result initEs(@LoginUser TokenDto tokenDto) { + articleService.initIndex(tokenDto.getCustomerId(), tokenDto.getUserId()); + return new Result(); + } + } diff --git a/code/smart-community/epmet-module/gov-voice/gov-voice-server/src/main/java/com/epmet/service/ArticleService.java b/code/smart-community/epmet-module/gov-voice/gov-voice-server/src/main/java/com/epmet/service/ArticleService.java index d56c8d03..4a57f73b 100644 --- a/code/smart-community/epmet-module/gov-voice/gov-voice-server/src/main/java/com/epmet/service/ArticleService.java +++ b/code/smart-community/epmet-module/gov-voice/gov-voice-server/src/main/java/com/epmet/service/ArticleService.java @@ -295,4 +295,19 @@ public interface ArticleService extends BaseService { String gridId, String tagName, String tagId); + + + /** + * 初始化ES索引 + * article 文章表 + * article_content 文章内容表 + * article_cover 文章封面表 + * article_operate_record 文章操作记录表 + * article_publish_range 文章发布范围表 + * article_tags 文章标签表 + * article_visit_record 文章访问记录表 + * @param customerId + * @param currentOperUserId + */ + void initIndex(String customerId,String currentOperUserId); } \ No newline at end of file diff --git a/code/smart-community/epmet-module/gov-voice/gov-voice-server/src/main/java/com/epmet/service/impl/ArticleServiceImpl.java b/code/smart-community/epmet-module/gov-voice/gov-voice-server/src/main/java/com/epmet/service/impl/ArticleServiceImpl.java index 662bd99f..4d165781 100644 --- a/code/smart-community/epmet-module/gov-voice/gov-voice-server/src/main/java/com/epmet/service/impl/ArticleServiceImpl.java +++ b/code/smart-community/epmet-module/gov-voice/gov-voice-server/src/main/java/com/epmet/service/impl/ArticleServiceImpl.java @@ -579,10 +579,18 @@ public class ArticleServiceImpl extends BaseServiceImpl pageInfo = new PageInfo<>(list); return new PageData<>(list, pageInfo.getTotal()); } + + + /** + * 初始化ES索引 + * article 文章表 + * article_content 文章内容表 + * article_cover 文章封面表 + * article_operate_record 文章操作记录表 + * article_publish_range 文章发布范围表 + * article_tags 文章标签表 + * article_visit_record 文章访问记录表 + * @param customerId + * @param currentOperUserId + */ + @Override + public void initIndex(String customerId, String currentOperUserId) { + //1.ES删除该ducument下全部索引 + if (elasticsearchUtils.deleteAllArticle(ElasticsearchArticleConstant.ARTICLE)){ + //删除成功 + log.info("删除文章索引ducument,更新es成功"); + }else{ + //删除失败 + log.info("文章更新,更新es失败"); + } + //2.查询得到现在全部可用文章并添加至ES中 + +// ElasticsearchArticleDTO elasticsearchArticleDTO = ElasticsearchArticleDTO.builder().id(article.getId()).author(article.getPublisherName()) +// .abstracts(article.getPreviewContent()).category("ARTICLE").title(article.getTitle()) +// .content(articleContent.getContent()).source(article.getPublisherName()).updateDate(String.valueOf(article.getUpdatedTime())) +// .thumbnail(formDTO.getImgUrl()).uri("/aritcle/"+article.getId()).gridIdList(formDTO.getGridIdList()).build(); +// LambdaQueryWrapper queryWrapper = new LambdaQueryWrapper<>(); +// queryWrapper.eq(ArticleListFormDTO::getStatusFlag, ArticleConstant.PUBLISHED); + ArticleListFormDTO articleListFormDTO = new ArticleListFormDTO(); + articleListFormDTO.setCustomerId(customerId); + articleListFormDTO.setStatusFlag(ArticleConstant.PUBLISHED); + List list = baseDao.selectAllArticle2(articleListFormDTO); + List elasticsearchArticleDTOList = new ArrayList<>(); + for (PublishedListResultDTO publishedListResultDTO : list) { + ElasticsearchArticleDTO elasticsearchArticleDTO = ElasticsearchArticleDTO.builder() + .id(publishedListResultDTO.getArticleId()) + .abstracts(publishedListResultDTO.getPreviewContent()) + .author(publishedListResultDTO.getPublisherName()) + .source(ElasticsearchArticleConstant.SHE_QU) + .category(ElasticsearchArticleConstant.ARTICLE) + .thumbnail(publishedListResultDTO.getImgUrl()) + .updateDate(publishedListResultDTO.getPublishDate()) + .content(publishedListResultDTO.getPreviewContent()) + .title(publishedListResultDTO.getTitle()) + .uri("/aritcle/"+publishedListResultDTO.getArticleId()) + .gridIdList(publishedListResultDTO.getGridIdList()).build(); + elasticsearchArticleDTOList.add(elasticsearchArticleDTO); + } + +// List elasticsearchArticleDTOList = list.stream() +// .map(ElasticsearchArticleDTO::new) +// .collect(Collectors.toList()); + if (elasticsearchUtils.addAllArticle(elasticsearchArticleDTOList)){ + log.info("初始化索引成功"); + }else{ + log.info("初始化索引异常"); + } + } } diff --git a/code/smart-community/epmet-module/gov-voice/gov-voice-server/src/main/java/com/epmet/utils/ElasticsearchUtils.java b/code/smart-community/epmet-module/gov-voice/gov-voice-server/src/main/java/com/epmet/utils/ElasticsearchUtils.java index 0580af99..df94a6b2 100644 --- a/code/smart-community/epmet-module/gov-voice/gov-voice-server/src/main/java/com/epmet/utils/ElasticsearchUtils.java +++ b/code/smart-community/epmet-module/gov-voice/gov-voice-server/src/main/java/com/epmet/utils/ElasticsearchUtils.java @@ -6,11 +6,13 @@ import lombok.extern.slf4j.Slf4j; import okhttp3.*; import org.springframework.stereotype.Component; +import java.util.List; + @Slf4j @Component public class ElasticsearchUtils { - private static final String ElasticsearchTestURL = "http://172.16.9.20:9000"; - private static final String ElasticsearchProdURL = "http://172.16.9.20:9000"; + private static final String ElasticsearchTestURL = "http://ssotest.shuzirizhao.cn"; + private static final String ElasticsearchProdURL = "http://172.16.9.39:9000"; private static final String ElasticsearchURL = ElasticsearchTestURL; private static final String ElasticsearchArticleDelete = "/SysElasticsearch/article/delete/"; @@ -55,6 +57,43 @@ public class ElasticsearchUtils { return false; } + @SneakyThrows + public Boolean addAllArticle(List list) { + ObjectMapper objectMapper = new ObjectMapper(); + String json = objectMapper.writeValueAsString(list); + + OkHttpClient client = new OkHttpClient().newBuilder() + .build(); + MediaType mediaType = MediaType.parse("application/json"); + RequestBody body = RequestBody.create(mediaType, json); + Request request = new Request.Builder() + .url(ElasticsearchURL+ElasticsearchArticleAdd+"/all") + .method("POST", body) + .addHeader("User-Agent", "Apifox/1.0.0 (https://apifox.com)") + .addHeader("Content-Type", "application/json") + .build(); + Response response = client.newCall(request).execute(); + ResponseBody responseBody = response.body(); + // 获取响应的状态码 + int statusCode = response.code(); + + if (response.isSuccessful()) { + // 请求成功,状态码为2xx + if (responseBody != null) { + // 处理响应内容 + String responseString = responseBody.string(); + log.info("接口请求成功,响应内容:" + responseString); + responseBody.close(); + return true; + } + } else { + // 请求失败,状态码不是2xx + log.info("接口请求失败,状态码:" + statusCode); + return false; + } + return false; + } + @SneakyThrows public Boolean updateArticle(ElasticsearchArticleDTO bean) { ObjectMapper objectMapper = new ObjectMapper(); @@ -126,4 +165,40 @@ public class ElasticsearchUtils { } return false; } + + //通过来源删除该索引document下的该来源的全部 + @SneakyThrows + public Boolean deleteAllArticle(String source) { + + + OkHttpClient client = new OkHttpClient().newBuilder() + .build(); + MediaType mediaType = MediaType.parse("text/plain"); + RequestBody body = RequestBody.create(mediaType, ""); + Request request = new Request.Builder() + .url(ElasticsearchURL+ElasticsearchArticleDelete+"source?source="+source) + .delete() + .addHeader("User-Agent", "Apifox/1.0.0 (https://apifox.com)") + .build(); + Response response = client.newCall(request).execute(); + ResponseBody responseBody = response.body(); + // 获取响应的状态码 + int statusCode = response.code(); + + if (response.isSuccessful()) { + // 请求成功,状态码为2xx + if (responseBody != null) { + // 处理响应内容 + String responseString = responseBody.string(); + log.info("接口请求成功,响应内容:" + responseString); + responseBody.close(); + return true; + } + } else { + // 请求失败,状态码不是2xx + log.info("接口请求失败,状态码:" + statusCode); + return false; + } + return false; + } }