JudgmentDocumentsServiceImpl.java 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. package com.winhc.phoenix.example.service.impl;
  2. import com.winhc.phoenix.example.dao.SearchDao;
  3. import com.winhc.phoenix.example.service.JudgmentDocumentsService;
  4. import com.winhc.phoenix.example.util.company.search.CompanyIndexUtils;
  5. import com.winhc.phoenix.example.vo.judgment.JudgmentDocumentsSearchContent;
  6. import com.winhc.phoenix.example.vo.judgment.JudgmentDocumentsSearchType;
  7. import com.winhc.phoenix.example.vo.judgment.JudgmentDocumentsSortType;
  8. import com.winhc.phoenix.example.vo.judgment.QueryOrAgg;
  9. import com.winhc.tool.bean.IpInfo;
  10. import com.winhc.tool.component.WinhcIpParser;
  11. import lombok.AllArgsConstructor;
  12. import lombok.extern.slf4j.Slf4j;
  13. import org.apache.commons.lang3.StringUtils;
  14. import org.elasticsearch.index.query.BoolQueryBuilder;
  15. import org.elasticsearch.index.query.QueryBuilder;
  16. import org.elasticsearch.script.Script;
  17. import org.elasticsearch.search.aggregations.AggregationBuilder;
  18. import org.elasticsearch.search.aggregations.bucket.terms.TermsAggregationBuilder;
  19. import org.elasticsearch.search.aggregations.support.ValueType;
  20. import org.elasticsearch.search.sort.*;
  21. import org.springframework.stereotype.Service;
  22. import java.util.*;
  23. import static com.winhc.phoenix.example.util.QueryBuilderUtils.existField;
  24. import static com.winhc.phoenix.example.util.QueryBuilderUtils.spanNearQuery4StandardAnalyzer;
  25. import static org.elasticsearch.index.query.QueryBuilders.*;
  26. /**
  27. * @author: XuJiakai
  28. * 2022/7/29 14:19
  29. */
  30. @Slf4j
  31. @Service
  32. @AllArgsConstructor
  33. public class JudgmentDocumentsServiceImpl implements JudgmentDocumentsService {
  34. private final WinhcIpParser winhcIpParser;
  35. private final SearchDao searchDao;
  36. public static final String index = "wenshu_detail4";
  37. public static final String type = "wenshu_detail_type";
  38. private static final List<String> highlightField = new ArrayList<String>() {{
  39. add("addenda");
  40. add("case_no");
  41. add("case_no.keyword");
  42. add("clerk");
  43. add("court_name");
  44. add("court_view");
  45. add("fact");
  46. add("judge");
  47. add("judge_result");
  48. add("party_info");
  49. add("title");
  50. }};
  51. private static void mustOrFilter(boolean must, BoolQueryBuilder boolQuery, QueryBuilder queryBuilder) {
  52. if (must) {
  53. boolQuery.must(queryBuilder);
  54. } else {
  55. boolQuery.filter(queryBuilder);
  56. }
  57. }
  58. @Override
  59. public Object search(JudgmentDocumentsSearchContent searchContent) {
  60. BoolQueryBuilder boolQuery = boolQuery();
  61. if (searchContent.isAuthorityCase()) {
  62. boolQuery.filter(termsQuery("sample_type", "1", "2", "3"));
  63. } else {
  64. boolQuery.filter(termQuery("sample_type", "9"));
  65. }
  66. Map<String, JudgmentDocumentsSearchType> content = searchContent.getContent();
  67. for (Map.Entry<String, JudgmentDocumentsSearchType> entry : content.entrySet()) {
  68. if (StringUtils.isBlank(entry.getKey())) {
  69. continue;
  70. }
  71. if (entry.getKey().contains(" ")) {
  72. for (String s : entry.getKey().split(" +")) {
  73. if (StringUtils.isBlank(s)) {
  74. continue;
  75. }
  76. mustOrFilter(searchContent.isNotSort(), boolQuery, getSearchContentQuery(entry.getValue(), s));
  77. }
  78. } else {
  79. mustOrFilter(searchContent.isNotSort(), boolQuery, getSearchContentQuery(entry.getValue(), entry.getKey()));
  80. }
  81. }
  82. boolQuery.filter(existField("court_level", Arrays.asList("", "-1")));
  83. int from = searchContent.getFrom();
  84. int size = searchContent.getSize();
  85. String preference = searchContent.getPreference();
  86. JudgmentDocumentsSortType sort = searchContent.getSort();
  87. List<SortBuilder> sortBuilders = new ArrayList<>();
  88. if (sort == null && !searchContent.isNotSort()) {
  89. String ip = searchContent.getIp();
  90. if (StringUtils.isNotBlank(ip)) {
  91. IpInfo ipInfo = winhcIpParser.parseIp(ip);
  92. if (ipInfo.getIpProvinceCode() != null) {
  93. Map<String, Object> map = new HashMap<String, Object>(2);
  94. map.put("provinceCode", ipInfo.getIpProvinceCode());
  95. map.put("cityCode", ipInfo.getIpCityCode());
  96. log.info("province: {},city: {}", ipInfo.getIpProvince(), ipInfo.getIpCity());
  97. Script script = CompanyIndexUtils.getScript("if('0'.equals(doc['court_level'].value)) return 100;" +
  98. "if(params.provinceCode.equals(doc['court_province_code'].value)&&params.cityCode==null) return 100;" +
  99. "if(params.provinceCode.equals(doc['court_province_code'].value)&&params.cityCode!=null && params.cityCode.equals(doc['court_city_code'].value)) return 100;return 0;", map);
  100. ScriptSortBuilder order = SortBuilders.scriptSort(script, ScriptSortBuilder.ScriptSortType.NUMBER).order(SortOrder.DESC);
  101. sortBuilders.add(order);
  102. }
  103. FieldSortBuilder order = SortBuilders.fieldSort("case_score").order(SortOrder.DESC);
  104. sortBuilders.add(order);
  105. }
  106. }
  107. List<AggregationBuilder> aggList = null;
  108. if (QueryOrAgg.AGG.equals(searchContent.getQueryOrAgg())) {
  109. aggList = agg();
  110. from = 0;
  111. size = 0;
  112. } else if (QueryOrAgg.QUERY_AND_AGG.equals(searchContent.getQueryOrAgg())) {
  113. aggList = agg();
  114. }
  115. Object search = searchDao.search(index
  116. , type
  117. , boolQuery
  118. , null
  119. , sortBuilders
  120. , null
  121. , from
  122. , size
  123. , preference
  124. , highlightField
  125. , aggList
  126. );
  127. return search;
  128. }
  129. public List<AggregationBuilder> agg() {
  130. ArrayList<AggregationBuilder> agg = new ArrayList<>();
  131. agg.add(new TermsAggregationBuilder("court_level_agg", ValueType.STRING).field("court_level"));
  132. return agg;
  133. }
  134. private static QueryBuilder getSearchContentQuery(JudgmentDocumentsSearchType searchScope, String content) {
  135. BoolQueryBuilder boolQuery = boolQuery();
  136. List<String> fields = searchScope.getFields();
  137. if (fields == null) {
  138. Arrays.stream(JudgmentDocumentsSearchType.values()).map(JudgmentDocumentsSearchType::getFields).filter(Objects::nonNull).flatMap(Collection::stream).forEach(e -> {
  139. if (JudgmentDocumentsSearchType.KEYWORD_FIELDS.contains(e)) {
  140. boolQuery.should(termQuery(e, content));
  141. } else {
  142. boolQuery.should(spanNearQuery4StandardAnalyzer(e, content));
  143. }
  144. });
  145. } else {
  146. for (String e : searchScope.getFields()) {
  147. if (JudgmentDocumentsSearchType.KEYWORD_FIELDS.contains(e)) {
  148. boolQuery.should(termQuery(e, content));
  149. } else {
  150. boolQuery.should(spanNearQuery4StandardAnalyzer(e, content));
  151. }
  152. }
  153. }
  154. return boolQuery;
  155. }
  156. }