| | |
| | | package org.springblade.es.service; |
| | | |
| | | import com.alibaba.fastjson.JSONObject; |
| | | import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; |
| | | import com.baomidou.mybatisplus.core.metadata.IPage; |
| | | import org.apache.logging.log4j.util.Strings; |
| | | import org.elasticsearch.action.admin.indices.delete.DeleteIndexRequest; |
| | |
| | | import org.elasticsearch.client.indices.GetIndexRequest; |
| | | import org.elasticsearch.index.query.BoolQueryBuilder; |
| | | import org.elasticsearch.index.query.QueryBuilders; |
| | | import org.elasticsearch.index.reindex.*; |
| | | import org.elasticsearch.index.reindex.BulkByScrollResponse; |
| | | import org.elasticsearch.index.reindex.DeleteByQueryRequest; |
| | | import org.elasticsearch.index.reindex.UpdateByQueryRequest; |
| | | import org.elasticsearch.script.Script; |
| | | import org.elasticsearch.script.ScriptType; |
| | | import org.elasticsearch.search.SearchHit; |
| | | import org.elasticsearch.search.SearchHits; |
| | |
| | | import org.springblade.es.vo.EsParam; |
| | | import org.springblade.modules.article.entity.Article; |
| | | import org.springblade.modules.article.service.ArticleService; |
| | | import org.springblade.modules.doorplateAddress.entity.DoorplateAddressEntity; |
| | | import org.springblade.modules.doorplateAddress.service.IDoorplateAddressService; |
| | | import org.springblade.modules.grid.service.IGridRangeService; |
| | | import org.springblade.modules.house.entity.HouseEntity; |
| | | import org.springblade.modules.house.entity.HouseholdEntity; |
| | |
| | | import org.springframework.beans.factory.annotation.Value; |
| | | import org.springframework.scheduling.annotation.Async; |
| | | import org.springframework.stereotype.Service; |
| | | import org.elasticsearch.script.Script; |
| | | |
| | | import java.io.IOException; |
| | | import java.util.ArrayList; |
| | |
| | | |
| | | @Autowired |
| | | private IGridRangeService gridRangeService; |
| | | |
| | | @Autowired |
| | | private IDoorplateAddressService doorplateAddressService; |
| | | |
| | | @Value("${elasticsearch.sync}") |
| | | private boolean elasticsearchSync; |
| | |
| | | * @return |
| | | */ |
| | | public boolean init(EsParam esParam) { |
| | | // 场所 |
| | | initPlace(esParam); |
| | | // 通知文章 |
| | | initArticle(esParam); |
| | | // 房屋 |
| | | initHouse(esParam); |
| | | // 住户 |
| | | initHousehold(esParam); |
| | | if (!Strings.isBlank(esParam.getTableName())) { |
| | | if (esParam.getTableName().equals("jczz_place")) { |
| | | // 场所 |
| | | initPlace(esParam); |
| | | } |
| | | if (esParam.getTableName().equals("jczz_article")) { |
| | | // 通知文章 |
| | | initArticle(esParam); |
| | | } |
| | | if (esParam.getTableName().equals("jczz_house")) { |
| | | // 房屋 |
| | | initHouse(esParam); |
| | | } |
| | | if (esParam.getTableName().equals("jczz_household")) { |
| | | // 住户 |
| | | initHousehold(esParam); |
| | | } |
| | | } |
| | | // 返回 |
| | | return true; |
| | | } |
| | |
| | | // 刷新索引,确保文档可搜索 |
| | | client.indices().refresh(new RefreshRequest(esParam.getIndexName()), RequestOptions.DEFAULT); |
| | | } catch (IOException e) { |
| | | e.printStackTrace(); |
| | | // e.printStackTrace(); |
| | | } |
| | | System.out.println("批量插入结果: " + !bulkResponse.hasFailures()); |
| | | // System.out.println("批量插入结果: " + !bulkResponse.hasFailures()); |
| | | } |
| | | } |
| | | // 返回 |
| | |
| | | // 刷新索引,确保文档可搜索 |
| | | client.indices().refresh(new RefreshRequest(esParam.getIndexName()), RequestOptions.DEFAULT); |
| | | } catch (IOException e) { |
| | | e.printStackTrace(); |
| | | // e.printStackTrace(); |
| | | } |
| | | System.out.println("批量插入结果: " + !bulkResponse.hasFailures()); |
| | | // System.out.println("批量插入结果: " + !bulkResponse.hasFailures()); |
| | | } |
| | | } |
| | | // 返回 |
| | |
| | | "tableId", household.getId().toString(), |
| | | "tableName", "jczz_household", |
| | | "title", household.getName(), |
| | | "relationship", household.getRelationship(), |
| | | "name", household.getName(), |
| | | "phone", household.getPhoneNumber(), |
| | | "idCard", household.getIdCard(), |
| | |
| | | // 刷新索引,确保文档可搜索 |
| | | client.indices().refresh(new RefreshRequest(esParam.getIndexName()), RequestOptions.DEFAULT); |
| | | } catch (IOException e) { |
| | | e.printStackTrace(); |
| | | // e.printStackTrace(); |
| | | } |
| | | System.out.println("批量插入结果: " + !bulkResponse.hasFailures()); |
| | | // System.out.println("批量插入结果: " + !bulkResponse.hasFailures()); |
| | | } |
| | | } |
| | | // 返回 |
| | |
| | | // 刷新索引,确保文档可搜索 |
| | | client.indices().refresh(new RefreshRequest(esParam.getIndexName()), RequestOptions.DEFAULT); |
| | | } catch (IOException e) { |
| | | e.printStackTrace(); |
| | | // e.printStackTrace(); |
| | | } |
| | | System.out.println("批量插入结果: " + !bulkResponse.hasFailures()); |
| | | // System.out.println("批量插入结果: " + !bulkResponse.hasFailures()); |
| | | } |
| | | } |
| | | // 返回 |
| | |
| | | "articleType", article.getArticleType() |
| | | ); |
| | | } catch (IOException e) { |
| | | e.printStackTrace(); |
| | | // e.printStackTrace(); |
| | | } |
| | | } |
| | | } |
| | |
| | | "communityCode", placeService.getCommunityCode(place.getId()) |
| | | ); |
| | | } catch (IOException e) { |
| | | e.printStackTrace(); |
| | | // e.printStackTrace(); |
| | | } |
| | | } |
| | | } |
| | |
| | | "communityCode", houseService.getCommunityCode(house.getId()) |
| | | ); |
| | | } catch (IOException e) { |
| | | e.printStackTrace(); |
| | | // e.printStackTrace(); |
| | | } |
| | | } |
| | | } |
| | |
| | | @Async |
| | | public void addHousehold(EsParam esParam, HouseholdEntity household) { |
| | | if (elasticsearchSync) { |
| | | // 如果关系为空,则默认为19 |
| | | if (household.getRelationship() == null) { |
| | | household.setRelationship(19); |
| | | } |
| | | try { |
| | | indexDocument(esParam.getIndexName(), |
| | | "tableId", household.getId().toString(), |
| | |
| | | "name", household.getName(), |
| | | "phone", household.getPhoneNumber(), |
| | | "idCard", household.getIdCard(), |
| | | "content", household.getCurrentAddress(), |
| | | "relationship", household.getRelationship().toString(), |
| | | "content", getAddress(household), |
| | | "communityCode", householdService.getCommunityCode(household.getId()) |
| | | ); |
| | | } catch (IOException e) { |
| | | e.printStackTrace(); |
| | | // e.printStackTrace(); |
| | | } |
| | | } |
| | | } |
| | | |
| | | /** |
| | | * 获取房屋地址 |
| | | * @param household |
| | | * @return |
| | | */ |
| | | public String getAddress(HouseholdEntity household) { |
| | | if (!Strings.isBlank(household.getHouseCode())){ |
| | | // 查询对应的房屋地址 |
| | | QueryWrapper<DoorplateAddressEntity> queryWrapper = new QueryWrapper<>(); |
| | | queryWrapper.eq("address_code",household.getHouseCode()); |
| | | List<DoorplateAddressEntity> list = doorplateAddressService.list(queryWrapper); |
| | | if (list.size()>0){ |
| | | return list.get(0).getAddressName(); |
| | | } |
| | | // 房屋查不到的情况 |
| | | if (!Strings.isBlank(household.getCurrentAddress())){ |
| | | return household.getCurrentAddress(); |
| | | } |
| | | }else { |
| | | return household.getCurrentAddress(); |
| | | } |
| | | return null; |
| | | } |
| | | |
| | | /** |
| | |
| | | |
| | | // 输出操作结果 |
| | | boolean acknowledged = deleteResponse.isAcknowledged(); |
| | | System.out.println("索引删除成功: " + acknowledged); |
| | | // System.out.println("索引删除成功: " + acknowledged); |
| | | } catch (IOException e) { |
| | | e.printStackTrace(); |
| | | // e.printStackTrace(); |
| | | } |
| | | } |
| | | } |
| | |
| | | try { |
| | | indexDocument(esParam.getIndexName(), str); |
| | | } catch (IOException e) { |
| | | e.printStackTrace(); |
| | | // e.printStackTrace(); |
| | | } |
| | | return true; |
| | | } |
| | |
| | | setHouseMap((HouseEntity) item, data); |
| | | } |
| | | if (esParam.getTableName().equals("jczz_household")) { |
| | | setHousehouldMap((HouseholdEntity) item, data); |
| | | setHouseholdMap((HouseholdEntity) item, data); |
| | | } |
| | | Map<String, Object> param = new HashMap<String, Object>(); |
| | | param.put("data", data); |
| | |
| | | // 刷新索引,确保文档可搜索 |
| | | client.indices().refresh(new RefreshRequest(esParam.getIndexName()), RequestOptions.DEFAULT); |
| | | } catch (IOException e) { |
| | | e.printStackTrace(); |
| | | // e.printStackTrace(); |
| | | } |
| | | } |
| | | } |
| | |
| | | * @param item |
| | | * @param data |
| | | */ |
| | | private void setHousehouldMap(HouseholdEntity item, Map<String, Object> data) { |
| | | private void setHouseholdMap(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("relationship",item.getRelationship()); |
| | | data.put("name",item.getName()); |
| | | data.put("phone",item.getPhoneNumber()); |
| | | data.put("idCard",item.getIdCard()); |
| | | data.put("content",item.getCurrentAddress()); |
| | | data.put("content",getAddress(item)); |
| | | } |
| | | |
| | | /** |
| | |
| | | BulkByScrollResponse bulkResponse = |
| | | client.deleteByQuery(deleteByQueryRequest, RequestOptions.DEFAULT); |
| | | } catch (IOException e) { |
| | | e.printStackTrace(); |
| | | // e.printStackTrace(); |
| | | } |
| | | } |
| | | return true; |