linwe
2024-09-03 764d883b5ea3bdc06abbec548b6df0511e567978
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
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.action.admin.indices.refresh.RefreshRequest;
import org.elasticsearch.action.bulk.BulkRequest;
import org.elasticsearch.action.bulk.BulkResponse;
import org.elasticsearch.action.index.IndexRequest;
import org.elasticsearch.action.index.IndexResponse;
import org.elasticsearch.action.search.SearchRequest;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.action.support.master.AcknowledgedResponse;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.client.indices.GetIndexRequest;
import org.elasticsearch.index.query.BoolQueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
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.elasticsearch.search.builder.SearchSourceBuilder;
import org.springblade.common.cache.SysCache;
import org.springblade.common.constant.EsTableConstant;
import org.springblade.common.param.CommonParamSet;
import org.springblade.common.utils.SpringUtils;
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.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;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;
 
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
 
@Service
public class ElasticsearchDocumentService<T> {
 
    @Autowired
    private RestHighLevelClient client;
 
    @Autowired
    private ArticleService articleService;
 
    @Autowired
    private IPlaceService placeService;
 
    @Autowired
    private IHouseService houseService;
 
    @Autowired
    private IHouseholdService householdService;
 
    @Autowired
    private IGridRangeService gridRangeService;
 
    @Autowired
    private IDoorplateAddressService doorplateAddressService;
 
    @Value("${elasticsearch.sync}")
    private boolean elasticsearchSync;
 
    @Value("${elasticsearch.indexName}")
    private String indexName;
 
    /**
     * 检查索引是否已存在
     *
     * @param indexName
     * @return
     */
    public boolean isIndexExists(String indexName) {
        // 检查索引是否已存在
        GetIndexRequest request = new GetIndexRequest(indexName);
        try {
            return client.indices().exists(request, RequestOptions.DEFAULT);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return false;
    }
 
    /**
     * 初始化数据
     *
     * @param esParam
     * @return
     */
    public boolean init(EsParam 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;
    }
 
    /**
     * 初始化数据
     *
     * @param esParam
     * @return
     */
    public boolean initPlace(EsParam esParam) {
        // 查询
        int total = placeService.getAllListTotal();
        int size = 1000;
        int num = total / size;
        for (int i = 1; i <= num + 1; i++) {
            List<PlaceVO> placeVOList = placeService.getAllList((i - 1) * size, size);
            // 遍历
            if (placeVOList.size() > 0) {
                // 创建批量请求对象
                BulkRequest bulkRequest = new BulkRequest();
                for (PlaceVO place : placeVOList) {
                    // 入es库
                    // 初始化Elasticsearch客户端
                    IndexRequest indexRequest = new IndexRequest(esParam.getIndexName());
                    // 不设置id
                    // indexRequest.id("1");
                    indexRequest.source(
                        "tableId", place.getId().toString(),
                        "tableName", "jczz_place",
                        "title", place.getPlaceName(),
                        "name", place.getPrincipal(),
                        "phone", place.getPrincipalPhone(),
                        "idCard", place.getPrincipalIdCard(),
                        "content", place.getLocation(),
                        "communityCode", place.getCommunityCode()
                    );
                    // 加入集合
                    bulkRequest.add(indexRequest);
                }
                BulkResponse bulkResponse = null;
                try {
                    // 执行批量插入
                    bulkResponse = client.bulk(bulkRequest, RequestOptions.DEFAULT);
                    // 刷新索引,确保文档可搜索
                    client.indices().refresh(new RefreshRequest(esParam.getIndexName()), RequestOptions.DEFAULT);
                } catch (IOException e) {
//                    e.printStackTrace();
                }
//                System.out.println("批量插入结果: " + !bulkResponse.hasFailures());
            }
        }
        // 返回
        return true;
    }
 
    /**
     * 初始化公告数据
     *
     * @param esParam
     * @return
     */
    public boolean initArticle(EsParam esParam) {
        // 查询
        int total = articleService.getAllListTotal();
        int size = 10;
        int num = total / size;
        for (int i = 1; i <= num + 1; i++) {
            // 查询
            List<Article> articleList = articleService.getAllList((i - 1) * size, size);
            // 遍历
            if (articleList.size() > 0) {
                BulkRequest bulkRequest = new BulkRequest();
                for (Article article : articleList) {
                    // 初始化Elasticsearch客户端
                    IndexRequest indexRequest = new IndexRequest(esParam.getIndexName());
                    String communityNameListString
                        = SysCache.getAllCommunityNameListString(article.getArticleRange(),article.getId().toString());
                    Map<String, Object> map = new HashMap<>(1);
                    map.put("communityCode",communityNameListString);
                    String toString = new JSONObject(map).toString();
                    // 不设置id
                    // indexRequest.id("1");
                    indexRequest.source("tableId", article.getId().toString(),
                        "tableName", "jczz_article",
                        "title", article.getTitle(),
                        "communityCode", toString,
                        "type", null != article.getType() ? article.getType().toString() : "",
                        "content", article.getContent(),
                        "articleType", article.getArticleType());
                    // 加入集合
                    bulkRequest.add(indexRequest);
                }
                BulkResponse bulkResponse = null;
                try {
                    // 执行批量插入
                    bulkResponse = client.bulk(bulkRequest, RequestOptions.DEFAULT);
                    // 刷新索引,确保文档可搜索
                    client.indices().refresh(new RefreshRequest(esParam.getIndexName()), RequestOptions.DEFAULT);
                } catch (IOException e) {
//                    e.printStackTrace();
                }
//                System.out.println("批量插入结果: " + !bulkResponse.hasFailures());
            }
        }
        // 返回
        return true;
    }
 
    /**
     * 初始化住户数据
     *
     * @param esParam
     * @return
     */
    public boolean initHousehold(EsParam esParam) {
        // 查询
        int total = householdService.getAllListTotal();
        int size = 1000;
        int num = total / size;
        for (int i = 1; i <= num + 1; i++) {
            // 查询
            List<HouseholdVO> householdVOList = householdService.getAllList((i - 1) * size, size);
            // 遍历
            if (householdVOList.size() > 0) {
                BulkRequest bulkRequest = new BulkRequest();
                for (HouseholdVO household : householdVOList) {
                    // 初始化Elasticsearch客户端
                    IndexRequest indexRequest = new IndexRequest(esParam.getIndexName());
                    // 不设置id
                    // indexRequest.id("1");
                    indexRequest.source(
                        "tableId", household.getId().toString(),
                        "tableName", "jczz_household",
                        "title", household.getName(),
                        "relationship", household.getRelationship(),
                        "name", household.getName(),
                        "phone", household.getPhoneNumber(),
                        "idCard", household.getIdCard(),
                        "content", household.getAddress(),
                        "communityCode", household.getCommunityCode()
                    );
                    // 加入集合
                    bulkRequest.add(indexRequest);
                }
                BulkResponse bulkResponse = null;
                try {
                    // 执行批量插入
                    bulkResponse = client.bulk(bulkRequest, RequestOptions.DEFAULT);
                    // 刷新索引,确保文档可搜索
                    client.indices().refresh(new RefreshRequest(esParam.getIndexName()), RequestOptions.DEFAULT);
                } catch (IOException e) {
//                    e.printStackTrace();
                }
//                System.out.println("批量插入结果: " + !bulkResponse.hasFailures());
            }
        }
        // 返回
        return true;
    }
 
    /**
     * 初始化房屋数据
     *
     * @param esParam
     * @return
     */
    public boolean initHouse(EsParam esParam) {
        // 查询
        int total = houseService.getAllListTotal();
        int size = 1000;
        int num = total / size;
        for (int i = 1; i <= num + 1; i++) {
            // 查询
            List<HouseVO> houseVOList = houseService.getAllList((i - 1) * size, size);
            // 遍历
            if (houseVOList.size() > 0) {
                BulkRequest bulkRequest = new BulkRequest();
                for (HouseVO house : houseVOList) {
                    // 初始化Elasticsearch客户端
                    IndexRequest indexRequest = new IndexRequest(esParam.getIndexName());
                    // 不设置id
                    // indexRequest.id("1");
                    indexRequest.source(
                        "tableId", house.getId().toString(),
                        "tableName", "jczz_house",
                        "title", house.getHouseName(),
                        "name", null,
                        "phone", null,
                        "idCard", null,
                        "content", null,
                        "communityCode", house.getCommunityCode()
                    );
                    // 加入集合
                    bulkRequest.add(indexRequest);
                }
                BulkResponse bulkResponse = null;
                try {
                    // 执行批量插入
                    bulkResponse = client.bulk(bulkRequest, RequestOptions.DEFAULT);
                    // 刷新索引,确保文档可搜索
                    client.indices().refresh(new RefreshRequest(esParam.getIndexName()), RequestOptions.DEFAULT);
                } catch (IOException e) {
//                    e.printStackTrace();
                }
//                System.out.println("批量插入结果: " + !bulkResponse.hasFailures());
            }
        }
        // 返回
        return true;
    }
 
    /**
     * 新增公告同步
     *
     * @param esParam
     * @param article
     */
    @Async
    public void addArticle(EsParam esParam, Article article) {
        if (elasticsearchSync) {
            // 查询当前文章范围对应的社区名称字符串集合
            String communityNameListString = SysCache.getAllCommunityNameListString(article.getArticleRange(),article.getId().toString());
            Map<String, Object> map = new HashMap<>(1);
            map.put("communityCode",communityNameListString);
            String toString = new JSONObject(map).toString();
            try {
                indexDocument(esParam.getIndexName(),
                    "tableId", article.getId().toString(),
                    "tableName", "jczz_article",
                    "title", article.getTitle(),
                    "communityCode", toString,
                    "type", null != article.getType() ? article.getType().toString() : "",
                    "content", article.getContent(),
                    "articleType", article.getArticleType()
                );
            } catch (IOException e) {
//                e.printStackTrace();
            }
        }
    }
 
    /**
     * 新增场所同步
     *
     * @param esParam
     * @param place
     */
    @Async
    public void addPlace(EsParam esParam, PlaceEntity place) {
        if (elasticsearchSync) {
            try {
                indexDocument(esParam.getIndexName(),
                    "tableId", place.getId().toString(),
                    "tableName", "jczz_place",
                    "title", place.getPlaceName(),
                    "name", place.getPrincipal(),
                    "phone", place.getPrincipalPhone(),
                    "idCard", place.getPrincipalIdCard(),
                    "content", place.getLocation(),
                    "communityCode", placeService.getCommunityCode(place.getId())
                );
            } catch (IOException e) {
//                e.printStackTrace();
            }
        }
    }
 
    /**
     * 新增房屋同步
     *
     * @param esParam
     * @param house
     */
    @Async
    public void addHouse(EsParam esParam, HouseEntity house) {
        if (elasticsearchSync) {
            try {
                indexDocument(esParam.getIndexName(),
                    "tableId", house.getId().toString(),
                    "tableName", "jczz_house",
                    "title", house.getHouseName(),
                    "name", null,
                    "phone", null,
                    "idCard", null,
                    "content", null,
                    "communityCode", houseService.getCommunityCode(house.getId())
                );
            } catch (IOException e) {
//                e.printStackTrace();
            }
        }
    }
 
    /**
     * 新增住户同步
     *
     * @param esParam
     * @param household
     */
    @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(),
                    "tableName", "jczz_household",
                    "title", household.getName(),
                    "name", household.getName(),
                    "phone", household.getPhoneNumber(),
                    "idCard", household.getIdCard(),
                    "relationship", household.getRelationship().toString(),
                    "content", getAddress(household),
                    "communityCode", householdService.getCommunityCode(household.getId())
                );
            } catch (IOException e) {
//                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;
    }
 
    /**
     * 插入数据到es
     *
     * @param index
     * @param values
     * @throws IOException
     */
    public void indexDocument(String index, String... values) throws IOException {
        // 初始化Elasticsearch客户端
        IndexRequest indexRequest = new IndexRequest(index);
 
        // 不设置id
//        indexRequest.id("1");
        indexRequest.source(values);
 
        IndexResponse indexResponse = client.index(indexRequest, RequestOptions.DEFAULT);
        // 刷新索引,确保文档可搜索
        client.indices().refresh(new RefreshRequest(index), RequestOptions.DEFAULT);
        System.out.println("Indexed document with id: " + indexResponse.getId());
 
    }
 
    /**
     * 查询数据
     *
     * @param page
     * @param esParam
     * @return
     */
    public Object selectDocumentPage(IPage<Object> page, EsParam esParam) {
        if (Strings.isBlank(esParam.getIndexName())){
            esParam.setIndexName(indexName);
        }
        // 判断索引是否存在
        if (isIndexExists(esParam.getIndexName())) {
            // 全文搜索
            SearchRequest searchRequest = new SearchRequest(esParam.getIndexName());
            SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
            //        searchSourceBuilder.query(QueryBuilders.multiMatchQuery(esParam.getSearchKey(),
            //            "content",
            //            "name",
            //            "title",
            //            "location",
            //            "phone",
            //            "idCard",
            //            "communityCode")
            //            .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.matchPhraseQuery("title", 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 roleName = SpringUtils.getRequestParam("roleName");
            String communityCode = SpringUtils.getRequestParam("communityCode");
            if (!Strings.isBlank(communityCode)) {
                if (!Strings.isBlank(roleName) && roleName.equals("inhabitant") && !Strings.isBlank(esParam.getHouseCode())){
                    // 居民根据房屋对应的小区过滤
                    boolQueryBuilder.must(QueryBuilders.matchPhraseQuery("communityCode",gridRangeService.getDistrictIdByHouseCode(esParam.getHouseCode())));
                }else {
                    // 设置查询社区名称,编号有多个,数字类型目前无法匹配到
                    boolQueryBuilder.must(QueryBuilders.matchPhraseQuery("communityCode", communityCode));
                }
            }
            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);
 
            int current = (int) page.getCurrent();
            int size = (int) page.getSize();
            //设置分页
            searchSourceBuilder.size(size);
            searchSourceBuilder.from((current - 1) * size);
 
            //将搜索资源对象设置到搜索客户端中
            searchRequest.source(searchSourceBuilder);
            //查询
            SearchResponse searchResponse = null;
            try {
                searchResponse = client.search(searchRequest, RequestOptions.DEFAULT);
            } catch (IOException e) {
                e.printStackTrace();
            }
            //解析搜索返回值
            SearchHits searchHits = searchResponse.getHits();
            //查询的总数
            long count = searchHits.getTotalHits().value;
            List<Object> list = new ArrayList<>();
            for (SearchHit hit : searchHits) {
//                System.out.println("hit.getId() = " + hit.getId());
                Map<String, Object> result = hit.getSourceAsMap();
                if (null != result){
                    if (result.get("tableName").toString().equals("jczz_article")){
                        result.put("typeName","通知公告");
                    }
                    if (result.get("tableName").toString().equals("jczz_place")){
                        result.put("typeName","经营场所");
                    }
                    if (result.get("tableName").toString().equals("jczz_house")){
                        result.put("typeName","房屋");
                    }
                    if (result.get("tableName").toString().equals("jczz_household")){
                        result.put("typeName","住户");
                    }
                }
                // 文档源数据
//            String source = hit.getSourceAsString();
                list.add(result);
            }
            page.setRecords(list);
            page.setTotal(count);
        }
        return page;
    }
 
    /**
     * 根据索引删除
     */
    public boolean removeBatchByIndexNames(List<String> indexNames) {
        for (String indexName : indexNames) {
            // 索引存在才删除
            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();
                }
            }
        }
        return true;
    }
 
    /**
     * 新增数据--有问题,待调整
     */
    @Async
    public boolean add(EsParam esParam, T item) {
        String str = "tableName," + esParam.getTableName() + ",";
        CommonParamSet commonParamSet =
            new CommonParamSet().setFieldValue(item.getClass(), item, EsTableConstant.articleList, str);
        try {
            indexDocument(esParam.getIndexName(), str);
        } catch (IOException e) {
//            e.printStackTrace();
        }
        return true;
    }
 
 
    /**
     * 修改数据
     */
    @Async
    public void update(EsParam esParam, T item,List<String> columnList) {
        // 判断索引是否存在
        if (elasticsearchSync && isIndexExists(esParam.getIndexName())) {
            // 创建更新请求
            UpdateByQueryRequest updateRequest = new UpdateByQueryRequest(esParam.getIndexName());
 
            // 设置查询条件,例如:更新所有字段text包含"old text"的文档
            updateRequest.setQuery(
                QueryBuilders.boolQuery()
                    .must(QueryBuilders.matchQuery("tableId", esParam.getTableId()))
                    .must(QueryBuilders.matchQuery("tableName", esParam.getTableName()))
            );
 
            Map<String, Object> data = new HashMap<String, Object>();
            data.put("tableName", esParam.getTableName());
            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")) {
                setHouseholdMap((HouseholdEntity) item, data);
            }
            Map<String, Object> param = new HashMap<String, Object>();
            param.put("data", data);
 
            // script will read data param value and assign to document source
            String source = "ctx._source=params.data";
            Script script = new Script(ScriptType.INLINE, "painless", source, param);
            updateRequest.setScript(script);
            // 执行更新操作
            try {
                client.updateByQuery(updateRequest, RequestOptions.DEFAULT);
                // 刷新索引,确保文档可搜索
                client.indices().refresh(new RefreshRequest(esParam.getIndexName()), RequestOptions.DEFAULT);
            } catch (IOException e) {
//                e.printStackTrace();
            }
        }
    }
 
    /**
     * 设置公告map
     * @param item
     * @param data
     */
    private void setArticleMap(Article item, Map<String, Object> data) {
        // 查询当前文章范围对应的社区名称字符串集合
        String communityNameListString = SysCache.getAllCommunityNameListString(item.getArticleRange(),item.getId().toString());
        Map<String, Object> map = new HashMap<>(1);
        map.put("communityCode",communityNameListString);
        String toString = new JSONObject(map).toString();
        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",toString);
    }
    /**
     * 设置场所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 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",getAddress(item));
    }
 
    /**
     * 删除数据--根据条件
     */
    public boolean removeByQuery(EsParam esParam) {
        if (elasticsearchSync && isIndexExists(esParam.getIndexName())) {
            DeleteByQueryRequest deleteByQueryRequest = new DeleteByQueryRequest(esParam.getIndexName());
            // 根据多个条件 生成 boolQueryBuilder
            BoolQueryBuilder boolQueryBuilder = QueryBuilders.boolQuery();
            if (!Strings.isBlank(esParam.getTableId())){
                boolQueryBuilder
                    .must(QueryBuilders.matchQuery("tableId", esParam.getTableId()));
            }
            if (!Strings.isBlank(esParam.getTableName())) {
                boolQueryBuilder
                    .must(QueryBuilders.matchQuery("tableName", esParam.getTableName()));
            }
            deleteByQueryRequest.setQuery(boolQueryBuilder);
            try {
                BulkByScrollResponse bulkResponse =
                    client.deleteByQuery(deleteByQueryRequest, RequestOptions.DEFAULT);
            } catch (IOException e) {
//                e.printStackTrace();
            }
        }
        return true;
    }
}