lin
2024-04-11 dd60de8d90f05c64e3cf166bc617b95a82102c52
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;
   /**
    * 检查索引是否已存在
@@ -446,6 +452,9 @@
    * @return
    */
   public Object selectDocumentPage(IPage<Object> page, EsParam esParam) {
      if (Strings.isBlank(esParam.getIndexName())){
         esParam.setIndexName(indexName);
      }
      // 判断索引是否存在
      if (isIndexExists(esParam.getIndexName())) {
         // 全文搜索
@@ -462,12 +471,12 @@
         //         .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()));
         String communityCode = SpringUtils.getRequestParam("communityCode");
         if (!Strings.isBlank(communityCode)) {
            boolQueryBuilder.must(QueryBuilders.matchQuery("communityCode", communityCode));
@@ -530,16 +539,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 +578,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 +593,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 +624,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();