SqlParserOrderByTest.java 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. package org.es;
  2. import org.elasticsearch.action.search.SearchRequestBuilder;
  3. import org.es.sql.dsl.bean.ElasticSqlParseResult;
  4. import org.es.sql.dsl.parser.ElasticSql2DslParser;
  5. import org.elasticsearch.search.sort.SortBuilder;
  6. import org.elasticsearch.search.sort.SortBuilders;
  7. import org.elasticsearch.search.sort.SortOrder;
  8. import org.es.sql.util.ElasticMockClient;
  9. import org.junit.Assert;
  10. import org.junit.Test;
  11. public class SqlParserOrderByTest {
  12. @Test
  13. public void testParseEqExpr() {
  14. String sql = "select id,status from index.order order by price asc,id desc,lastUpdateTime asc";
  15. ElasticSql2DslParser sql2DslParser = new ElasticSql2DslParser();
  16. ElasticSqlParseResult parseResult = sql2DslParser.parse(sql);
  17. SortBuilder targetSort = SortBuilders.fieldSort("price").order(SortOrder.ASC);
  18. Assert.assertEquals(parseResult.getOrderBy().get(0).toString(), targetSort.toString());
  19. targetSort = SortBuilders.fieldSort("id").order(SortOrder.DESC);
  20. Assert.assertEquals(parseResult.getOrderBy().get(1).toString(), targetSort.toString());
  21. targetSort = SortBuilders.fieldSort("lastUpdateTime").order(SortOrder.ASC);
  22. Assert.assertEquals(parseResult.getOrderBy().get(2).toString(), targetSort.toString());
  23. sql = "select id,status from index.order order by nvl(price, 0) asc";
  24. sql2DslParser = new ElasticSql2DslParser();
  25. parseResult = sql2DslParser.parse(sql);
  26. targetSort = SortBuilders.fieldSort("price").order(SortOrder.ASC).missing(0);
  27. Assert.assertEquals(parseResult.getOrderBy().get(0).toString(), targetSort.toString());
  28. sql = "select id,status from index.order order by nvl(product.price, 0) asc";
  29. sql2DslParser = new ElasticSql2DslParser();
  30. parseResult = sql2DslParser.parse(sql);
  31. targetSort = SortBuilders.fieldSort("product.price").order(SortOrder.ASC).missing(0);
  32. Assert.assertEquals(parseResult.getOrderBy().get(0).toString(), targetSort.toString());
  33. sql = "select id,status from index.order order by nvl($product.price, 0) asc";
  34. sql2DslParser = new ElasticSql2DslParser();
  35. parseResult = sql2DslParser.parse(sql);
  36. targetSort = SortBuilders.fieldSort("product.price").order(SortOrder.ASC).missing(0).setNestedPath("product");
  37. Assert.assertEquals(parseResult.getOrderBy().get(0).toString(), targetSort.toString());
  38. sql = "select id,status from index.order order by nvl(product.price, 0) asc";
  39. sql2DslParser = new ElasticSql2DslParser();
  40. parseResult = sql2DslParser.parse(sql);
  41. targetSort = SortBuilders.fieldSort("product.price").order(SortOrder.ASC).missing(0);
  42. Assert.assertEquals(parseResult.getOrderBy().get(0).toString(), targetSort.toString());
  43. sql = "select id,status from index.order order by product.price asc, $productTags.sortNo desc";
  44. sql2DslParser = new ElasticSql2DslParser();
  45. parseResult = sql2DslParser.parse(sql);
  46. targetSort = SortBuilders.fieldSort("product.price").order(SortOrder.ASC);
  47. Assert.assertEquals(parseResult.getOrderBy().get(0).toString(), targetSort.toString());
  48. targetSort = SortBuilders.fieldSort("productTags.sortNo").order(SortOrder.DESC).setNestedPath("productTags");
  49. Assert.assertEquals(parseResult.getOrderBy().get(1).toString(), targetSort.toString());
  50. }
  51. @Test
  52. public void testX() {
  53. ElasticMockClient esClient = new ElasticMockClient();
  54. String sql = "select * from index.order where status='SUCCESS' order by nvl(pride, 0) asc, script_sort('doc[\"price\"].value * 1.5 / vp', 'number', 'vp:2.1') desc routing by 'CA','CB' limit 0, 20";
  55. ElasticSql2DslParser sql2DslParser = new ElasticSql2DslParser();
  56. //解析SQL
  57. ElasticSqlParseResult parseResult = sql2DslParser.parse(sql);
  58. //生成DSL,可用于rest api调用
  59. String dsl = parseResult.toDsl();
  60. //toRequest方法接收一个client对象参数,用于生成SearchRequestBuilder
  61. SearchRequestBuilder searchReq = parseResult.toRequest(esClient);
  62. //执行查询
  63. //SearchResponse response = searchReq.execute().actionGet();
  64. System.out.println(dsl);
  65. }
  66. }