Browse Source

fix: v8-fast版本查询语句

operator 和 minimum_should_match冲突问题
许家凯 4 years ago
parent
commit
87446f30ff

+ 143 - 0
src/main/java/com/winhc/phoenix/example/service/impl/SearchV8FastServiceImpl.java

@@ -0,0 +1,143 @@
+package com.winhc.phoenix.example.service.impl;
+
+import com.winhc.phoenix.example.dao.SearchDao;
+import com.winhc.phoenix.example.service.SearchService;
+import com.winhc.phoenix.example.util.SortUtil;
+import lombok.AllArgsConstructor;
+import lombok.extern.slf4j.Slf4j;
+import org.elasticsearch.index.query.*;
+import org.elasticsearch.index.query.functionscore.ScriptScoreFunctionBuilder;
+import org.elasticsearch.search.fetch.subphase.FetchSourceContext;
+import org.elasticsearch.search.rescore.QueryRescoreMode;
+import org.elasticsearch.search.rescore.QueryRescorerBuilder;
+import org.elasticsearch.search.sort.FieldSortBuilder;
+import org.elasticsearch.search.sort.ScriptSortBuilder;
+import org.elasticsearch.search.sort.SortBuilders;
+import org.elasticsearch.search.sort.SortOrder;
+import org.springframework.context.annotation.Primary;
+import org.springframework.stereotype.Service;
+
+import java.util.regex.Pattern;
+
+import static org.elasticsearch.index.query.QueryBuilders.*;
+
+/**
+ * @author: XuJiakai
+ * 2020/11/19 14:54
+ */
+@Slf4j
+@Primary
+@Service(value = "v8_fast")
+@AllArgsConstructor
+public class SearchV8FastServiceImpl implements SearchService {
+    private SearchDao searchDao;
+
+    public static final String index = "winhc-company-v8";
+    public static final String type = "company";
+    private static final String[] includes = new String[]{"cname", "legal_entity*", "estiblish_time", "reg_status_std", "company_type", "province_code", "reg_capital", "logo", "new_cid"};
+    private static final FetchSourceContext fetchSourceContext = new FetchSourceContext(true, includes, null);
+
+    private static final String[] includes_tips = new String[]{"cname.show"};
+    private static final FetchSourceContext fetchSourceContext_tips = new FetchSourceContext(true, includes_tips, null);
+
+
+    @Override
+    public Object tips(String content) {
+        BoolQueryBuilder queryBuilder = boolQuery()
+                .should(matchPhrasePrefixQuery("cname.value", content))
+//                .should(matchPhraseQuery("app_info", content))
+                ;
+        FieldSortBuilder company_score_weight = SortBuilders.fieldSort("company_score_weight").order(SortOrder.DESC);
+
+
+        Object search = searchDao.search(index, type, queryBuilder, company_score_weight, fetchSourceContext_tips, 0, 5);
+        return search;
+    }
+
+    @Override
+    public Object controlGroup(String s) {
+        QueryBuilder boolQuery = getBoolQuery(s);
+        return searchDao.search(index, type, boolQuery, null, fetchSourceContext, 0, 10);
+    }
+
+    @Override
+    public Object query(String content, int from, int size) {
+        BoolQueryBuilder boolQuery = getBoolQuery(content);
+        ScriptSortBuilder scriptSortBuilder = SortUtil.getInstance().fastSort;
+
+        QueryRescorerBuilder rescorerBuilder = new QueryRescorerBuilder(functionScoreQuery(new ScriptScoreFunctionBuilder(scriptSortBuilder.script())))
+                .windowSize(20)
+                .setScoreMode(QueryRescoreMode.Multiply);
+
+        Object search = searchDao.search(index, type, boolQuery, rescorerBuilder, null, fetchSourceContext, from, size);
+        return search;
+    }
+
+
+    private BoolQueryBuilder getBoolQuery(String content) {
+        BoolQueryBuilder boolQuery = QueryBuilders.boolQuery();
+
+
+        boolQuery.should(termQuery("cname.value.keyword", content).boost(100));
+        boolQuery.should(termQuery("history_name.value.keyword", content).boost(100));
+
+
+        boolQuery.should(disMaxQuery()
+                .add(disMaxQuery()
+                        .add(termQuery("legal_entity_name.keyword", content).boost(10))
+                        .add(termQuery("holder.name.keyword", content).boost(5.5F))
+                        .add(termQuery("staff.name.keyword", content).boost(5.5F))
+                        .tieBreaker(0.3F))
+                .add(disMaxQuery()
+                        .add(matchQuery("legal_entity_name", content).boost(6).minimumShouldMatch("5<80%"))
+                        .add(matchQuery("holder", content).boost(10).minimumShouldMatch("5<80%"))
+                        .add(matchQuery("staff", content).boost(6).minimumShouldMatch("5<80%"))
+                        .tieBreaker(0.3F)
+                ).tieBreaker(0.3F)
+        );
+
+
+        boolQuery.should(disMaxQuery()
+                .add(disMaxQuery()
+                        .add(termQuery("icp.keyword", content).boost(20))
+                        .add(termQuery("app_info.keyword", content).boost(40))
+                        .add(termQuery("company_tm.keyword", content).boost(20))
+                        .tieBreaker(0.4F)
+                )
+                .add(
+                        disMaxQuery()
+                                .add(matchQuery("icp", content).boost(8).minimumShouldMatch("5"))
+                                .add(matchQuery("app_info", content).boost(19).minimumShouldMatch("5"))
+                                .add(matchQuery("company_tm", content).boost(7).minimumShouldMatch("5"))
+                                .tieBreaker(0.3F)
+                )
+
+                .tieBreaker(0.4F)
+        );
+
+
+        boolQuery.should(multiMatchQuery(content)
+//                .operator(Operator.AND)
+                .type(MultiMatchQueryBuilder.Type.CROSS_FIELDS)
+                .minimumShouldMatch("5<80%")
+                .tieBreaker(0.3F)
+                .field("cname.value", 16)
+                .field("history_name.value", 12)
+
+        );
+
+
+        BoolQueryBuilder boolQuery2 = QueryBuilders.boolQuery()
+                .filter(termQuery("deleted", "0"))
+                .filter(rangeQuery("company_score_weight").gt(0.3F))
+                .must(boolQuery);
+        return boolQuery2;
+    }
+
+    private static final Pattern pattern = Pattern.compile("^[a-zA-Z ]*$");
+
+    private static boolean is_pinyin(String str) {
+        return pattern.matcher(str).find();
+    }
+
+}