ElasticSqlParseResult.java 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  1. package org.es.sql.dsl.bean;
  2. import org.apache.commons.collections.CollectionUtils;
  3. import org.apache.commons.lang.StringUtils;
  4. import org.elasticsearch.action.search.SearchAction;
  5. import org.elasticsearch.action.search.SearchRequestBuilder;
  6. import org.elasticsearch.client.Client;
  7. import org.elasticsearch.index.query.BoolQueryBuilder;
  8. import org.elasticsearch.index.query.QueryBuilders;
  9. import org.elasticsearch.search.aggregations.AbstractAggregationBuilder;
  10. import org.elasticsearch.search.aggregations.AggregationBuilder;
  11. import org.elasticsearch.search.sort.SortBuilder;
  12. import org.es.sql.utils.ElasticMockClient;
  13. import java.util.List;
  14. public class ElasticSqlParseResult {
  15. /*取数开始位置*/
  16. private int from = 0;
  17. /*取数大小*/
  18. private int size = 15;
  19. /*查询索引*/
  20. private List<String> indices;
  21. /*查询文档类*/
  22. private String type;
  23. /*查询索引别名*/
  24. private String queryAs;
  25. /*查询路由值*/
  26. private List<String> routingBy;
  27. /*查询字段列表*/
  28. private List<String> queryFieldList;
  29. /*SQL的where条件*/
  30. private transient BoolQueryBuilder whereCondition;
  31. /*SQL的where条件*/
  32. private transient BoolQueryBuilder matchCondition;
  33. /*SQL的order by条件*/
  34. private transient List<SortBuilder> orderBy;
  35. private transient boolean isTopStatsAgg = false;
  36. private transient List<AbstractAggregationBuilder> groupBy;
  37. public List<String> getQueryFieldList() {
  38. return queryFieldList;
  39. }
  40. public void setQueryFieldList(List<String> queryFieldList) {
  41. this.queryFieldList = queryFieldList;
  42. }
  43. public List<String> getIndices() {
  44. return indices;
  45. }
  46. public void setIndices(List<String> indices) {
  47. this.indices = indices;
  48. }
  49. public String getType() {
  50. return type;
  51. }
  52. public void setType(String type) {
  53. this.type = type;
  54. }
  55. public int getFrom() {
  56. return from;
  57. }
  58. public void setFrom(int from) {
  59. this.from = from;
  60. }
  61. public int getSize() {
  62. return size;
  63. }
  64. public void setSize(int size) {
  65. this.size = size;
  66. }
  67. public String getQueryAs() {
  68. return queryAs;
  69. }
  70. public void setQueryAs(String queryAs) {
  71. this.queryAs = queryAs;
  72. }
  73. public BoolQueryBuilder getWhereCondition() {
  74. return whereCondition;
  75. }
  76. public void setWhereCondition(BoolQueryBuilder whereCondition) {
  77. this.whereCondition = whereCondition;
  78. }
  79. public BoolQueryBuilder getMatchCondition() {
  80. return matchCondition;
  81. }
  82. public void setMatchCondition(BoolQueryBuilder matchCondition) {
  83. this.matchCondition = matchCondition;
  84. }
  85. public List<SortBuilder> getOrderBy() {
  86. return orderBy;
  87. }
  88. public void setOrderBy(List<SortBuilder> orderBy) {
  89. this.orderBy = orderBy;
  90. }
  91. public List<String> getRoutingBy() {
  92. return routingBy;
  93. }
  94. public void setRoutingBy(List<String> routingBy) {
  95. this.routingBy = routingBy;
  96. }
  97. public List<AbstractAggregationBuilder> getGroupBy() {
  98. return groupBy;
  99. }
  100. public void setGroupBy(List<AbstractAggregationBuilder> groupBy) {
  101. this.groupBy = groupBy;
  102. }
  103. public void setTopStatsAgg() {
  104. this.isTopStatsAgg = true;
  105. }
  106. public boolean getIsTopStatsAgg() {
  107. return isTopStatsAgg;
  108. }
  109. public SearchRequestBuilder toRequest(Client client) {
  110. SearchRequestBuilder requestBuilder = new SearchRequestBuilder(client, SearchAction.INSTANCE);
  111. requestBuilder.setFrom(from).setSize(size);
  112. if (CollectionUtils.isNotEmpty(indices)) {
  113. requestBuilder.setIndices(indices.toArray(new String[indices.size()]));
  114. }
  115. if (StringUtils.isNotBlank(type)) {
  116. requestBuilder.setTypes(type);
  117. }
  118. if (whereCondition != null && whereCondition.hasClauses()) {
  119. if (matchCondition != null && matchCondition.hasClauses()) {
  120. requestBuilder.setQuery(matchCondition.filter(whereCondition));
  121. }
  122. else {
  123. requestBuilder.setQuery(QueryBuilders.boolQuery().filter(whereCondition));
  124. }
  125. }
  126. else {
  127. if (matchCondition != null && matchCondition.hasClauses()) {
  128. requestBuilder.setQuery(matchCondition);
  129. }
  130. else {
  131. requestBuilder.setQuery(QueryBuilders.matchAllQuery());
  132. }
  133. }
  134. if (CollectionUtils.isNotEmpty(orderBy)) {
  135. for (SortBuilder sortBuilder : orderBy) {
  136. requestBuilder.addSort(sortBuilder);
  137. }
  138. }
  139. if (CollectionUtils.isNotEmpty(queryFieldList)) {
  140. requestBuilder.setFetchSource(queryFieldList.toArray(new String[queryFieldList.size()]), null);
  141. }
  142. if (CollectionUtils.isNotEmpty(routingBy)) {
  143. requestBuilder.setRouting(routingBy.toArray(new String[routingBy.size()]));
  144. }
  145. if (CollectionUtils.isNotEmpty(groupBy)) {
  146. if (!getIsTopStatsAgg()) {
  147. AggregationBuilder preAgg = null;
  148. for (AbstractAggregationBuilder aggItem : groupBy) {
  149. if (preAgg == null) {
  150. preAgg = (AggregationBuilder) aggItem;
  151. continue;
  152. }
  153. preAgg.subAggregation(aggItem);
  154. preAgg = (AggregationBuilder) aggItem;
  155. }
  156. requestBuilder.addAggregation(groupBy.get(0));
  157. }
  158. else {
  159. for (AbstractAggregationBuilder aggItem : groupBy) {
  160. requestBuilder.addAggregation(aggItem);
  161. }
  162. }
  163. }
  164. return requestBuilder;
  165. }
  166. public String toDsl(Client client) {
  167. return toRequest(client).toString();
  168. }
  169. public String toDsl() {
  170. return toDsl(ElasticMockClient.get());
  171. }
  172. @Override
  173. public String toString() {
  174. String ptn = "index:%s,type:%s,query_as:%s,from:%s,size:%s,routing:%s,dsl:%s";
  175. return String.format(ptn, indices, type, queryAs, from, size, routingBy != null ? routingBy.toString() : "[]", toDsl());
  176. }
  177. }