SearchV8ServiceImpl.java 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  1. package com.winhc.phoenix.example.service.impl;
  2. import com.winhc.phoenix.example.dao.SearchDao;
  3. import com.winhc.phoenix.example.service.SearchService;
  4. import com.winhc.phoenix.example.util.SortUtil;
  5. import lombok.AllArgsConstructor;
  6. import lombok.extern.slf4j.Slf4j;
  7. import org.elasticsearch.common.lucene.search.function.CombineFunction;
  8. import org.elasticsearch.common.lucene.search.function.FiltersFunctionScoreQuery;
  9. import org.elasticsearch.index.query.*;
  10. import org.elasticsearch.index.query.functionscore.FunctionScoreQueryBuilder;
  11. import org.elasticsearch.index.query.functionscore.ScriptScoreFunctionBuilder;
  12. import org.elasticsearch.search.fetch.subphase.FetchSourceContext;
  13. import org.elasticsearch.search.sort.ScriptSortBuilder;
  14. import org.springframework.context.annotation.Primary;
  15. import org.springframework.stereotype.Service;
  16. import java.util.regex.Pattern;
  17. import static org.elasticsearch.index.query.QueryBuilders.*;
  18. /**
  19. * @author: XuJiakai
  20. * 2020/11/19 14:54
  21. */
  22. @Slf4j
  23. @Primary
  24. @Service(value = SearchV8ServiceImpl.index)
  25. @AllArgsConstructor
  26. public class SearchV8ServiceImpl implements SearchService {
  27. private SearchDao searchDao;
  28. public static final String index = "winhc-company-v8";
  29. public static final String type = "company";
  30. private static final String[] includes = new String[]{"cname", "legal_entity*", "estiblish_time", "reg_status_std", "company_type", "province_code", "reg_capital", "logo"};
  31. private static final FetchSourceContext fetchSourceContext = new FetchSourceContext(true, includes, null);
  32. private static final String[] includes_tips = new String[]{"cname.show"};
  33. private static final FetchSourceContext fetchSourceContext_tips = new FetchSourceContext(true, includes_tips, null);
  34. @Override
  35. public Object tips(String content) {
  36. BoolQueryBuilder boolQuery = getBoolQuery(content);
  37. ScriptSortBuilder scriptSortBuilder = SortUtil.getInstance().fastSort;
  38. FunctionScoreQueryBuilder function = functionScoreQuery(boolQuery, new ScriptScoreFunctionBuilder(scriptSortBuilder.script())
  39. )
  40. .scoreMode(FiltersFunctionScoreQuery.ScoreMode.SUM)
  41. .boostMode(CombineFunction.MULTIPLY);
  42. Object search = searchDao.search(index, type, function, null, fetchSourceContext_tips, 0, 5);
  43. return search;
  44. }
  45. @Override
  46. public Object controlGroup(String s) {
  47. QueryBuilder boolQuery = getBoolQuery(s);
  48. return searchDao.search(index, type, boolQuery, null, fetchSourceContext, 0, 10);
  49. }
  50. @Override
  51. public Object query(String content, int from, int size) {
  52. BoolQueryBuilder boolQuery = getBoolQuery(content);
  53. ScriptSortBuilder scriptSortBuilder = SortUtil.getInstance().fastSort;
  54. FunctionScoreQueryBuilder function = functionScoreQuery(boolQuery, new ScriptScoreFunctionBuilder(scriptSortBuilder.script())
  55. )
  56. .boostMode(CombineFunction.MULTIPLY);
  57. Object search = searchDao.search(index, type, function, null, fetchSourceContext, from, size);
  58. return search;
  59. }
  60. private BoolQueryBuilder getBoolQuery(String content) {
  61. BoolQueryBuilder boolQuery = QueryBuilders.boolQuery();
  62. // boolQuery.should(matchPhrasePrefixQuery("cname.value.pinyin", content).analyzer("ik_pinyin_analyzer_search").maxExpansions(5).boost(0.1F));
  63. boolQuery.should(termQuery("cname.value.keyword", content).boost(100));
  64. boolQuery.should(termQuery("history_name.value.keyword", content).boost(100));
  65. /*boolQuery.should(disMaxQuery().add(
  66. QueryBuilders.boolQuery()
  67. .should(termQuery("legal_entity_name.keyword", content).boost(10))
  68. .should(termQuery("holder.name.keyword", content).boost(5.5F))
  69. .should(termQuery("staff.name.keyword", content).boost(5.5F))
  70. ).tieBreaker(0.3F));*/
  71. boolQuery.should(disMaxQuery()
  72. .add(termQuery("legal_entity_name.keyword", content).boost(10))
  73. .add(termQuery("holder.name.keyword", content).boost(5.5F))
  74. .add(termQuery("staff.name.keyword", content).boost(5.5F))
  75. .tieBreaker(0.3F));
  76. /* boolQuery.should(matchQuery("icp", content).boost(6));
  77. boolQuery.should(matchQuery("app_info", content).boost(12));
  78. boolQuery.should(matchQuery("company_tm", content).boost(5));*/
  79. // boolQuery.should(termQuery("icp.keyword", content).boost(5));
  80. // boolQuery.should(termQuery("app_info.keyword", content).boost(8));
  81. // boolQuery.should(termQuery("company_tm.keyword", content).boost(5));
  82. /* boolQuery.should(disMaxQuery().add(
  83. QueryBuilders.boolQuery()
  84. .should(termQuery("icp.keyword", content).boost(20))
  85. .should(termQuery("app_info.keyword", content).boost(40))
  86. .should(termQuery("company_tm.keyword", content).boost(20))
  87. ).tieBreaker(0.3F)
  88. );
  89. */
  90. /* boolQuery.should(disMaxQuery()
  91. .add(termQuery("icp.keyword", content).boost(20))
  92. .add(termQuery("app_info.keyword", content).boost(40))
  93. .add(termQuery("company_tm.keyword", content).boost(20))
  94. .tieBreaker(0.3F)
  95. );*/
  96. boolQuery.should(disMaxQuery()
  97. .add(disMaxQuery()
  98. .add(termQuery("icp.keyword", content).boost(20))
  99. .add(termQuery("app_info.keyword", content).boost(40))
  100. .add(termQuery("company_tm.keyword", content).boost(20))
  101. )
  102. .add(
  103. disMaxQuery()
  104. .add(matchQuery("icp", content).boost(8).operator(Operator.AND).minimumShouldMatch("5"))
  105. .add(matchQuery("app_info", content).boost(19).operator(Operator.AND).minimumShouldMatch("5"))
  106. .add(matchQuery("company_tm", content).boost(7).operator(Operator.AND).minimumShouldMatch("5"))
  107. .tieBreaker(0.3F)
  108. )
  109. /*.add( //todo 在排序上会有影响 ps. 奔驰
  110. boolQuery()
  111. .should(matchQuery("icp", content).boost(8))
  112. .should(matchQuery("app_info", content).boost(19))
  113. .should(matchQuery("company_tm", content).boost(7))
  114. .should(termQuery("icp.keyword", content).boost(20))
  115. .should(termQuery("app_info.keyword", content).boost(40))
  116. .should(termQuery("company_tm.keyword", content).boost(20))
  117. .minimumShouldMatch("5<80%")
  118. )*/
  119. /*.add(
  120. disMaxQuery()
  121. .add(boolQuery().should(matchQuery("icp", content).boost(8)).minimumShouldMatch("5<80%"))
  122. .add(boolQuery().should(matchQuery("app_info", content).boost(19)).minimumShouldMatch("5<80%"))
  123. .add(boolQuery().should(matchQuery("company_tm", content).boost(7)).minimumShouldMatch("5<80%"))
  124. .tieBreaker(0.3F)
  125. )*/
  126. .tieBreaker(0.4F)
  127. );
  128. /* boolQuery.should(disMaxQuery()
  129. .tieBreaker(0.3F)
  130. .add(matchQuery("icp", content).boost(6).minimumShouldMatch("5<80%"))
  131. .add(matchQuery("app_info", content).boost(15).minimumShouldMatch("5<80%"))
  132. .add(matchQuery("company_tm", content).boost(5).minimumShouldMatch("5<80%"))
  133. .add(termQuery("icp.keyword", content).boost(20))
  134. .add(termQuery("app_info.keyword", content).boost(40))
  135. .add(termQuery("company_tm.keyword", content).boost(30))
  136. );*/
  137. /* boolQuery.should(disMaxQuery()
  138. *//* .add(
  139. QueryBuilders.boolQuery()
  140. .should(matchQuery("icp", content).boost(6))
  141. .should(matchQuery("app_info", content).boost(15))
  142. .should(matchQuery("company_tm", content).boost(5))
  143. .minimumShouldMatch("5<80%")
  144. )*//*
  145. .add(
  146. QueryBuilders.boolQuery()
  147. .should(termQuery("icp.keyword", content).boost(20))
  148. .should(termQuery("app_info.keyword", content).boost(40))
  149. .should(termQuery("company_tm.keyword", content).boost(30))
  150. )
  151. .add(
  152. multiMatchQuery(content)
  153. .operator(Operator.AND)
  154. .type(MultiMatchQueryBuilder.Type.CROSS_FIELDS)
  155. .minimumShouldMatch("5<90%")
  156. .tieBreaker(0.3F)
  157. .field("icp", 6)
  158. .field("app_info", 15)
  159. .field("company_tm", 5)
  160. )
  161. .tieBreaker(0.3F)
  162. );*/
  163. /* boolQuery.should(multiMatchQuery(content)
  164. .operator(Operator.AND)
  165. .type(MultiMatchQueryBuilder.Type.CROSS_FIELDS)
  166. .tieBreaker(0.3F)
  167. .field("icp", 3)
  168. .field("app_info", 8)
  169. .field("company_tm", 1.5F)
  170. );*/
  171. boolQuery.should(multiMatchQuery(content)
  172. .operator(Operator.AND)
  173. .type(MultiMatchQueryBuilder.Type.CROSS_FIELDS)
  174. .minimumShouldMatch("5<80%")
  175. .tieBreaker(0.3F)
  176. .field("cname.value", 16)
  177. .field("history_name.value", 12)
  178. .field("holder.name", 6)
  179. .field("staff.name", 6)
  180. .field("legal_entity_name", 6)
  181. );
  182. BoolQueryBuilder boolQuery2 = QueryBuilders.boolQuery();
  183. boolQuery2.must(boolQuery);
  184. return boolQuery2;
  185. }
  186. private static final Pattern pattern = Pattern.compile("^[a-zA-Z ]*$");
  187. private static boolean is_pinyin(String str) {
  188. return pattern.matcher(str).find();
  189. }
  190. }