| | |
| | | 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; |
| | |
| | | |
| | | @Value("${elasticsearch.sync}") |
| | | private boolean elasticsearchSync; |
| | | |
| | | @Value("${elasticsearch.indexName}") |
| | | private String indexName; |
| | | |
| | | /** |
| | | * 检查索引是否已存在 |
| | |
| | | * @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(), |
| | |
| | | "phone", place.getPrincipalPhone(), |
| | | "idCard", place.getPrincipalIdCard(), |
| | | "content", place.getLocation(), |
| | | "communityCode", place.getCommunityCode() |
| | | "communityCode", placeService.getCommunityCode(place.getId()) |
| | | ); |
| | | } catch (IOException e) { |
| | | e.printStackTrace(); |
| | |
| | | * @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(), |
| | |
| | | "phone", null, |
| | | "idCard", null, |
| | | "content", null, |
| | | "communityCode", house.getCommunityCode() |
| | | "communityCode", houseService.getCommunityCode(house.getId()) |
| | | ); |
| | | } catch (IOException e) { |
| | | e.printStackTrace(); |
| | |
| | | * @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(), |
| | |
| | | "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(); |
| | |
| | | * @return |
| | | */ |
| | | public Object selectDocumentPage(IPage<Object> page, EsParam esParam) { |
| | | if (Strings.isBlank(esParam.getIndexName())){ |
| | | esParam.setIndexName(indexName); |
| | | } |
| | | // 判断索引是否存在 |
| | | if (isIndexExists(esParam.getIndexName())) { |
| | | // 全文搜索 |
| | |
| | | // .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); |
| | | |
| | |
| | | */ |
| | | 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; |
| | |
| | | * 修改数据 |
| | | */ |
| | | @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()); |
| | | |
| | |
| | | |
| | | 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); |
| | | |
| | |
| | | } |
| | | |
| | | /** |
| | | * 设置公告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(); |