zhongrj
2024-04-12 47712cc6cd4d52fcd1dde44e2a400ccbb9c6ae7a
src/main/java/org/springblade/es/service/ElasticsearchDocumentService.java
@@ -27,10 +27,13 @@
import org.springblade.es.vo.EsParam;
import org.springblade.modules.article.entity.Article;
import org.springblade.modules.article.service.ArticleService;
import org.springblade.modules.house.entity.HouseEntity;
import org.springblade.modules.house.entity.HouseholdEntity;
import org.springblade.modules.house.service.IHouseService;
import org.springblade.modules.house.service.IHouseholdService;
import org.springblade.modules.house.vo.HouseVO;
import org.springblade.modules.house.vo.HouseholdVO;
import org.springblade.modules.place.entity.PlaceEntity;
import org.springblade.modules.place.service.IPlaceService;
import org.springblade.modules.place.vo.PlaceVO;
import org.springframework.beans.factory.annotation.Autowired;
@@ -65,6 +68,9 @@
   @Value("${elasticsearch.sync}")
   private boolean elasticsearchSync;
   @Value("${elasticsearch.indexName}")
   private String indexName;
   /**
    * 检查索引是否已存在
@@ -336,11 +342,8 @@
    * @param place
    */
   @Async
   public void addPlace(EsParam esParam, PlaceVO place) {
   public void addPlace(EsParam esParam, PlaceEntity place) {
      if (elasticsearchSync) {
         if (!Strings.isBlank(place.getCommunityCode())) {
            place.setCommunityCode(placeService.getCommunityCode(place.getId()));
         }
         try {
            indexDocument(esParam.getIndexName(),
               "tableId", place.getId().toString(),
@@ -350,7 +353,7 @@
               "phone", place.getPrincipalPhone(),
               "idCard", place.getPrincipalIdCard(),
               "content", place.getLocation(),
               "communityCode", place.getCommunityCode()
               "communityCode", placeService.getCommunityCode(place.getId())
            );
         } catch (IOException e) {
            e.printStackTrace();
@@ -365,11 +368,8 @@
    * @param house
    */
   @Async
   public void addHouse(EsParam esParam, HouseVO house) {
   public void addHouse(EsParam esParam, HouseEntity house) {
      if (elasticsearchSync) {
         if (!Strings.isBlank(house.getCommunityCode())) {
            house.setCommunityCode(houseService.getCommunityCode(house.getId()));
         }
         try {
            indexDocument(esParam.getIndexName(),
               "tableId", house.getId().toString(),
@@ -379,7 +379,7 @@
               "phone", null,
               "idCard", null,
               "content", null,
               "communityCode", house.getCommunityCode()
               "communityCode", houseService.getCommunityCode(house.getId())
            );
         } catch (IOException e) {
            e.printStackTrace();
@@ -394,11 +394,8 @@
    * @param household
    */
   @Async
   public void addHousehold(EsParam esParam, HouseholdVO household) {
   public void addHousehold(EsParam esParam, HouseholdEntity household) {
      if (elasticsearchSync) {
         if (!Strings.isBlank(household.getCommunityCode())) {
            household.setCommunityCode(householdService.getCommunityCode(household.getId()));
         }
         try {
            indexDocument(esParam.getIndexName(),
               "tableId", household.getId().toString(),
@@ -407,8 +404,8 @@
               "name", household.getName(),
               "phone", household.getPhoneNumber(),
               "idCard", household.getIdCard(),
               "content", household.getAddress(),
               "communityCode", household.getCommunityCode()
               "content", household.getCurrentAddress(),
               "communityCode", householdService.getCommunityCode(household.getId())
            );
         } catch (IOException e) {
            e.printStackTrace();
@@ -446,6 +443,9 @@
    * @return
    */
   public Object selectDocumentPage(IPage<Object> page, EsParam esParam) {
      if (Strings.isBlank(esParam.getIndexName())){
         esParam.setIndexName(indexName);
      }
      // 判断索引是否存在
      if (isIndexExists(esParam.getIndexName())) {
         // 全文搜索
@@ -462,19 +462,35 @@
         //         .type(MultiMatchQueryBuilder.Type.BEST_FIELDS)
         //      );
         BoolQueryBuilder boolQueryBuilder = QueryBuilders.boolQuery();
         boolQueryBuilder.should(QueryBuilders.matchQuery("content", esParam.getSearchKey()));
         boolQueryBuilder.should(QueryBuilders.matchQuery("name", esParam.getSearchKey()));
//         boolQueryBuilder.should(QueryBuilders.matchQuery("content", esParam.getSearchKey()));
//         boolQueryBuilder.should(QueryBuilders.matchQuery("name", esParam.getSearchKey()));
         boolQueryBuilder.should(QueryBuilders.matchQuery("title", esParam.getSearchKey()));
         boolQueryBuilder.should(QueryBuilders.matchQuery("location", esParam.getSearchKey()));
         boolQueryBuilder.should(QueryBuilders.matchQuery("phone", esParam.getSearchKey()));
         boolQueryBuilder.should(QueryBuilders.matchQuery("idCard", esParam.getSearchKey()));
//         boolQueryBuilder.should(QueryBuilders.matchQuery("location", esParam.getSearchKey()));
//         boolQueryBuilder.should(QueryBuilders.matchQuery("phone", esParam.getSearchKey()));
//         boolQueryBuilder.should(QueryBuilders.matchQuery("idCard", esParam.getSearchKey()));
         if (!Strings.isBlank(esParam.getTableName())) {
            boolQueryBuilder.must(QueryBuilders.matchQuery("tableName", esParam.getTableName()));
         }
         String communityCode = SpringUtils.getRequestParam("communityCode");
         if (!Strings.isBlank(communityCode)) {
            boolQueryBuilder.must(QueryBuilders.matchQuery("communityCode", communityCode));
         }
         if (!Strings.isBlank(esParam.getTableName())) {
            boolQueryBuilder.must(QueryBuilders.matchQuery("tableName", esParam.getTableName()));
         String roleName = SpringUtils.getRequestParam("roleName");
         if (!Strings.isBlank(roleName)){
            if (roleName.equals("wgy") || roleName.equals("mj")){
               // 不查公告
               boolQueryBuilder.mustNot(QueryBuilders.matchQuery("tableName", "jczz_article"));
            }
            if (roleName.equals("inhabitant")){
               // 只查公告
               boolQueryBuilder.must(QueryBuilders.matchQuery("tableName", "jczz_article"));
            }
            if (roleName.equals("wzcj")){
               // 只查场所
               boolQueryBuilder.must(QueryBuilders.matchQuery("tableName", "jczz_place"));
            }
         }
         boolQueryBuilder.minimumShouldMatch(1);
         searchSourceBuilder.query(boolQueryBuilder);
@@ -530,16 +546,19 @@
    */
   public boolean removeBatchByIndexNames(List<String> indexNames) {
      for (String indexName : indexNames) {
         DeleteIndexRequest deleteIndexRequest = new DeleteIndexRequest(indexName);
         try {
            // 执行删除索引操作
            AcknowledgedResponse deleteResponse = client.indices().delete(deleteIndexRequest, RequestOptions.DEFAULT);
         // 索引存在才删除
         if (isIndexExists(indexName)) {
            DeleteIndexRequest deleteIndexRequest = new DeleteIndexRequest(indexName);
            try {
               // 执行删除索引操作
               AcknowledgedResponse deleteResponse = client.indices().delete(deleteIndexRequest, RequestOptions.DEFAULT);
            // 输出操作结果
            boolean acknowledged = deleteResponse.isAcknowledged();
            System.out.println("索引删除成功: " + acknowledged);
         } catch (IOException e) {
            e.printStackTrace();
               // 输出操作结果
               boolean acknowledged = deleteResponse.isAcknowledged();
               System.out.println("索引删除成功: " + acknowledged);
            } catch (IOException e) {
               e.printStackTrace();
            }
         }
      }
      return true;
@@ -566,8 +585,9 @@
    * 修改数据
    */
   @Async
   public void update(EsParam esParam, T item) {
      if (elasticsearchSync) {
   public void update(EsParam esParam, T item,List<String> columnList) {
      // 判断索引是否存在
      if (elasticsearchSync && isIndexExists(esParam.getIndexName())) {
         // 创建更新请求
         UpdateByQueryRequest updateRequest = new UpdateByQueryRequest(esParam.getIndexName());
@@ -580,8 +600,18 @@
         Map<String, Object> data = new HashMap<String, Object>();
         data.put("tableName", esParam.getTableName());
         new CommonParamSet<>().setFieldValueByMap(item.getClass(), item, EsTableConstant.articleList, data);
         if (esParam.getTableName().equals("jczz_article")) {
            setArticleMap((Article) item, data);
         }
         if (esParam.getTableName().equals("jczz_place")) {
            setPlaceMap((PlaceEntity) item, data);
         }
         if (esParam.getTableName().equals("jczz_house")) {
            setHouseMap((HouseEntity) item, data);
         }
         if (esParam.getTableName().equals("jczz_household")) {
            setHousehouldMap((HouseholdEntity) item, data);
         }
         Map<String, Object> param = new HashMap<String, Object>();
         param.put("data", data);
@@ -601,10 +631,62 @@
   }
   /**
    * 设置公告map
    * @param item
    * @param data
    */
   private void setArticleMap(Article item, Map<String, Object> data) {
      data.put("tableId",item.getId());
      data.put("title",item.getTitle());
      data.put("type",item.getType());
      data.put("content",item.getContent());
      data.put("articleType",item.getArticleType());
      data.put("communityCode",item.getArticleRange());
   }
   /**
    * 设置场所map
    * @param item
    * @param data
    */
   private void setPlaceMap(PlaceEntity item, Map<String, Object> data) {
      data.put("communityCode",placeService.getCommunityCode(item.getId()));
      data.put("tableId",item.getId());
      data.put("title",item.getPlaceName());
      data.put("name",item.getPrincipal());
      data.put("phone",item.getPrincipalPhone());
      data.put("idCard",item.getPrincipalIdCard());
      data.put("content",item.getLocation());
   }
   /**
    * 设置房屋map
    * @param item
    * @param data
    */
   private void setHouseMap(HouseEntity item, Map<String, Object> data) {
      data.put("communityCode",houseService.getCommunityCode(item.getId()));
      data.put("tableId",item.getId());
      data.put("title",item.getHouseName());
   }
   /**
    * 设置住户map
    * @param item
    * @param data
    */
   private void setHousehouldMap(HouseholdEntity item, Map<String, Object> data) {
      data.put("communityCode",householdService.getCommunityCode(item.getId()));
      data.put("tableId",item.getId());
      data.put("title",item.getName());
      data.put("name",item.getName());
      data.put("phone",item.getPhoneNumber());
      data.put("idCard",item.getIdCard());
      data.put("content",item.getCurrentAddress());
   }
   /**
    * 删除数据--根据条件
    */
   public boolean removeByQuery(EsParam esParam) {
      if (elasticsearchSync) {
      if (elasticsearchSync && isIndexExists(esParam.getIndexName())) {
         DeleteByQueryRequest deleteByQueryRequest = new DeleteByQueryRequest(esParam.getIndexName());
         // 根据多个条件 生成 boolQueryBuilder
         BoolQueryBuilder boolQueryBuilder = QueryBuilders.boolQuery();