ElasticSearch6.x 基于SpringBoot 实现ElasticSearch的多条件查询

浅浅的花香味﹌ 2023-10-18 22:27 122阅读 0赞

SpringBoot 功能封装涉及ElasticSearch的多条件查询方法约定如下:

public SearchResponse searchConstantScoreQuery(String[] indexs, String[] types, String name, Object value, float boost)

public SearchResponse searchBoolQuery(String[] indexs, String[] types, List musts, List mustNots, List shoulds, List filters)

public SearchResponse searchDisMaxQuery(String[] indexs, String[] types, List query, float boost, float breaker)

public SearchResponse searchFunctionScoreQuery(String[] indexs, String[] types, QueryBuilder matchQuery, String fieldName, Object origin, Object scale)

public SearchResponse searchBoostiQuery(String[] indexs, String[] types, QueryBuilder positiveQuery, QueryBuilder negativeQuery, float boost)

在上一篇文中说到:ElasticSearch6.x 基于SpringBoot 实现ElasticSearch连接功能封装,将约定的方法填充到ElasticSearchIndexUtil.java 工具类中。

  1. @Component
  2. public class ElasticSearchIndexUtil {
  3. // 引入 Ela 连接实列化对象
  4. @Autowired
  5. private TransportClient client;
  6. /**
  7. * 功能描述:多条件检索之constant score query
  8. * @param indexs 索引数组
  9. * @param types 类型数组
  10. * @param name 文档属性名称
  11. * @param value 文档属性值
  12. * @param boost 权重值
  13. * @return
  14. * @throws InterruptedException
  15. * @throws ExecutionException
  16. */
  17. public SearchResponse searchConstantScoreQuery(String[] indexs, String[] types, String name, Object value, float boost) throws InterruptedException, ExecutionException{
  18. TermQueryBuilder termQuery = QueryBuilders.termQuery(name, value);
  19. ConstantScoreQueryBuilder constantScoreQuery = QueryBuilders.constantScoreQuery(termQuery).boost(boost);
  20. SearchResponse response = client.prepareSearch(indexs).setTypes(types).setQuery(constantScoreQuery).execute().get();
  21. return response;
  22. }
  23. /**
  24. * 功能描述:多条件查询之bool query
  25. * @param indexs 索引数组
  26. * @param types 类型数组
  27. * @param musts 必须查询
  28. * @param mustNots 不能查询
  29. * @param shoulds 应查询
  30. * @param filters 拦截条件
  31. * @return
  32. * @throws InterruptedException
  33. * @throws ExecutionException
  34. */
  35. public SearchResponse searchBoolQuery(String[] indexs, String[] types, List<QueryBuilder> musts, List<QueryBuilder> mustNots, List<QueryBuilder> shoulds, List<QueryBuilder> filters) throws InterruptedException, ExecutionException{
  36. BoolQueryBuilder boolQuery = QueryBuilders.boolQuery();
  37. musts.stream().forEach(item->{
  38. boolQuery.must(item);
  39. });
  40. mustNots.stream().forEach(item->{
  41. boolQuery.mustNot(item);
  42. });
  43. shoulds.stream().forEach(item ->{
  44. boolQuery.should(item);
  45. });
  46. filters.stream().forEach(item->{
  47. boolQuery.filter(item);
  48. });
  49. SearchResponse response = client.prepareSearch(indexs).setTypes(types).setQuery(boolQuery).execute().get();
  50. return response;
  51. }
  52. /**
  53. * 功能描述:多条件查询之dis max query
  54. * @param indexs 索引数组
  55. * @param types 类型数组
  56. * @param query 查询条件对列
  57. * @param boost 权重值
  58. * @param breaker 判断值
  59. * @return
  60. * @throws ExecutionException
  61. * @throws InterruptedException
  62. */
  63. public SearchResponse searchDisMaxQuery(String[] indexs, String[] types, List<QueryBuilder> query, float boost, float breaker) throws InterruptedException, ExecutionException{
  64. DisMaxQueryBuilder disMaxQuery = QueryBuilders.disMaxQuery();
  65. query.stream().forEach(item ->{
  66. disMaxQuery.add(item);
  67. });
  68. disMaxQuery.boost(boost).tieBreaker(breaker);
  69. SearchResponse response = client.prepareSearch(indexs).setTypes(types).setQuery(disMaxQuery).execute().get();
  70. return response;
  71. }
  72. /**
  73. * 功能描述:多条件查询之function score query
  74. * @param indexs 索引数组
  75. * @param types 类型数组
  76. * @param matchQuery 条件
  77. * @param fieldName 文档属性名称
  78. * @param origin
  79. * @param scale
  80. * @return
  81. * @throws InterruptedException
  82. * @throws ExecutionException
  83. */
  84. public SearchResponse searchFunctionScoreQuery(String[] indexs, String[] types, QueryBuilder matchQuery, String fieldName, Object origin, Object scale) throws InterruptedException, ExecutionException{
  85. FilterFunctionBuilder[] functions = {
  86. new FunctionScoreQueryBuilder.FilterFunctionBuilder(matchQuery, ScoreFunctionBuilders.randomFunction().seed(Math.round(Math.random() * 100))),
  87. new FunctionScoreQueryBuilder.FilterFunctionBuilder(ScoreFunctionBuilders.exponentialDecayFunction(fieldName, origin, scale))
  88. };
  89. SearchResponse response = client.prepareSearch(indexs).setTypes(types).setQuery(QueryBuilders.functionScoreQuery(functions)).execute().get();
  90. return response;
  91. }
  92. /**
  93. * 功能描述:多条件查询之boost query
  94. * @param indexs 索引数组
  95. * @param types 类型数组
  96. * @param positiveQuery 条件
  97. * @param negativeQuery 条件
  98. * @param boost 权重值
  99. * @return
  100. * @throws ExecutionException
  101. * @throws InterruptedException
  102. */
  103. public SearchResponse searchBoostiQuery(String[] indexs, String[] types, QueryBuilder positiveQuery, QueryBuilder negativeQuery, float boost) throws InterruptedException, ExecutionException{
  104. BoostingQueryBuilder boostingQuery = QueryBuilders.boostingQuery(positiveQuery, negativeQuery);
  105. boostingQuery.boost(boost);
  106. SearchResponse response = client.prepareSearch(indexs).setTypes(types).setQuery(boostingQuery).execute().get();
  107. return response;
  108. }
  109. }

发表评论

表情:
评论列表 (有 0 条评论,122人围观)

还没有评论,来说两句吧...

相关阅读