package com.winhc.phoenix.example.service.impl; import com.winhc.phoenix.example.dao.SearchDao; import com.winhc.phoenix.example.service.JudgmentDocumentsService; import com.winhc.phoenix.example.util.company.search.CompanyIndexUtils; import com.winhc.phoenix.example.vo.judgment.JudgmentDocumentsSearchContent; import com.winhc.phoenix.example.vo.judgment.JudgmentDocumentsSearchType; import com.winhc.phoenix.example.vo.judgment.JudgmentDocumentsSortType; import com.winhc.phoenix.example.vo.judgment.QueryOrAgg; import com.winhc.tool.bean.IpInfo; import com.winhc.tool.component.WinhcIpParser; import lombok.AllArgsConstructor; import lombok.extern.slf4j.Slf4j; import org.apache.commons.lang3.StringUtils; import org.elasticsearch.index.query.BoolQueryBuilder; import org.elasticsearch.index.query.QueryBuilder; import org.elasticsearch.script.Script; import org.elasticsearch.search.aggregations.AggregationBuilder; import org.elasticsearch.search.aggregations.bucket.terms.TermsAggregationBuilder; import org.elasticsearch.search.aggregations.support.ValueType; import org.elasticsearch.search.sort.*; import org.springframework.stereotype.Service; import java.util.*; import static com.winhc.phoenix.example.util.QueryBuilderUtils.existField; import static com.winhc.phoenix.example.util.QueryBuilderUtils.spanNearQuery4StandardAnalyzer; import static org.elasticsearch.index.query.QueryBuilders.*; /** * @author: XuJiakai * 2022/7/29 14:19 */ @Slf4j @Service @AllArgsConstructor public class JudgmentDocumentsServiceImpl implements JudgmentDocumentsService { private final WinhcIpParser winhcIpParser; private final SearchDao searchDao; public static final String index = "wenshu_detail4"; public static final String type = "wenshu_detail_type"; private static final List highlightField = new ArrayList() {{ add("addenda"); add("case_no"); add("case_no.keyword"); add("clerk"); add("court_name"); add("court_view"); add("fact"); add("judge"); add("judge_result"); add("party_info"); add("title"); }}; private static void mustOrFilter(boolean must, BoolQueryBuilder boolQuery, QueryBuilder queryBuilder) { if (must) { boolQuery.must(queryBuilder); } else { boolQuery.filter(queryBuilder); } } @Override public Object search(JudgmentDocumentsSearchContent searchContent) { BoolQueryBuilder boolQuery = boolQuery(); if (searchContent.isAuthorityCase()) { boolQuery.filter(termsQuery("sample_type", "1", "2", "3")); } else { boolQuery.filter(termQuery("sample_type", "9")); } Map content = searchContent.getContent(); for (Map.Entry entry : content.entrySet()) { if (StringUtils.isBlank(entry.getKey())) { continue; } if (entry.getKey().contains(" ")) { for (String s : entry.getKey().split(" +")) { if (StringUtils.isBlank(s)) { continue; } mustOrFilter(searchContent.isNotSort(), boolQuery, getSearchContentQuery(entry.getValue(), s)); } } else { mustOrFilter(searchContent.isNotSort(), boolQuery, getSearchContentQuery(entry.getValue(), entry.getKey())); } } boolQuery.filter(existField("court_level", Arrays.asList("", "-1"))); int from = searchContent.getFrom(); int size = searchContent.getSize(); String preference = searchContent.getPreference(); JudgmentDocumentsSortType sort = searchContent.getSort(); List sortBuilders = new ArrayList<>(); if (sort == null && !searchContent.isNotSort()) { String ip = searchContent.getIp(); if (StringUtils.isNotBlank(ip)) { IpInfo ipInfo = winhcIpParser.parseIp(ip); if (ipInfo.getIpProvinceCode() != null) { Map map = new HashMap(2); map.put("provinceCode", ipInfo.getIpProvinceCode()); map.put("cityCode", ipInfo.getIpCityCode()); log.info("province: {},city: {}", ipInfo.getIpProvince(), ipInfo.getIpCity()); Script script = CompanyIndexUtils.getScript("if('0'.equals(doc['court_level'].value)) return 100;" + "if(params.provinceCode.equals(doc['court_province_code'].value)&¶ms.cityCode==null) return 100;" + "if(params.provinceCode.equals(doc['court_province_code'].value)&¶ms.cityCode!=null && params.cityCode.equals(doc['court_city_code'].value)) return 100;return 0;", map); ScriptSortBuilder order = SortBuilders.scriptSort(script, ScriptSortBuilder.ScriptSortType.NUMBER).order(SortOrder.DESC); sortBuilders.add(order); } FieldSortBuilder order = SortBuilders.fieldSort("case_score").order(SortOrder.DESC); sortBuilders.add(order); } } List aggList = null; if (QueryOrAgg.AGG.equals(searchContent.getQueryOrAgg())) { aggList = agg(); from = 0; size = 0; } else if (QueryOrAgg.QUERY_AND_AGG.equals(searchContent.getQueryOrAgg())) { aggList = agg(); } Object search = searchDao.search(index , type , boolQuery , null , sortBuilders , null , from , size , preference , highlightField , aggList ); return search; } public List agg() { ArrayList agg = new ArrayList<>(); agg.add(new TermsAggregationBuilder("court_level_agg", ValueType.STRING).field("court_level")); return agg; } private static QueryBuilder getSearchContentQuery(JudgmentDocumentsSearchType searchScope, String content) { BoolQueryBuilder boolQuery = boolQuery(); List fields = searchScope.getFields(); if (fields == null) { Arrays.stream(JudgmentDocumentsSearchType.values()).map(JudgmentDocumentsSearchType::getFields).filter(Objects::nonNull).flatMap(Collection::stream).forEach(e -> { if (JudgmentDocumentsSearchType.KEYWORD_FIELDS.contains(e)) { boolQuery.should(termQuery(e, content)); } else { boolQuery.should(spanNearQuery4StandardAnalyzer(e, content)); } }); } else { for (String e : searchScope.getFields()) { if (JudgmentDocumentsSearchType.KEYWORD_FIELDS.contains(e)) { boolQuery.should(termQuery(e, content)); } else { boolQuery.should(spanNearQuery4StandardAnalyzer(e, content)); } } } return boolQuery; } }