SqlParserOrderByTest.java 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. package org.elasticsearch;
  2. import org.elasticsearch.dsl.ElasticSql2DslParser;
  3. import org.elasticsearch.dsl.ElasticSqlParseResult;
  4. import org.elasticsearch.search.sort.SortBuilder;
  5. import org.elasticsearch.search.sort.SortBuilders;
  6. import org.elasticsearch.search.sort.SortOrder;
  7. import org.junit.Assert;
  8. import org.junit.Test;
  9. public class SqlParserOrderByTest {
  10. @Test
  11. public void testParseEqExpr() {
  12. String sql = "select id,productStatus from index.trx_order order by price asc,id desc,updatedAt asc";
  13. ElasticSql2DslParser sql2DslParser = new ElasticSql2DslParser();
  14. ElasticSqlParseResult parseResult = sql2DslParser.parse(sql);
  15. SortBuilder targetSort = SortBuilders.fieldSort("price").order(SortOrder.ASC);
  16. Assert.assertEquals(parseResult.getSortBuilderList().get(0).toString(), targetSort.toString());
  17. targetSort = SortBuilders.fieldSort("id").order(SortOrder.DESC);
  18. Assert.assertEquals(parseResult.getSortBuilderList().get(1).toString(), targetSort.toString());
  19. targetSort = SortBuilders.fieldSort("updatedAt").order(SortOrder.ASC);
  20. Assert.assertEquals(parseResult.getSortBuilderList().get(2).toString(), targetSort.toString());
  21. sql = "select id,productStatus from index.trx_order order by nvl(price, 0) asc";
  22. sql2DslParser = new ElasticSql2DslParser();
  23. parseResult = sql2DslParser.parse(sql);
  24. targetSort = SortBuilders.fieldSort("price").order(SortOrder.ASC).missing(0);
  25. Assert.assertEquals(parseResult.getSortBuilderList().get(0).toString(), targetSort.toString());
  26. sql = "select id,productStatus from index.trx_order order by nvl(inner_doc(product.price), 0) asc";
  27. sql2DslParser = new ElasticSql2DslParser();
  28. parseResult = sql2DslParser.parse(sql);
  29. targetSort = SortBuilders.fieldSort("product.price").order(SortOrder.ASC).missing(0);
  30. Assert.assertEquals(parseResult.getSortBuilderList().get(0).toString(), targetSort.toString());
  31. sql = "select id,productStatus from index.trx_order order by nvl(nested_doc(product.price), 0) asc";
  32. sql2DslParser = new ElasticSql2DslParser();
  33. parseResult = sql2DslParser.parse(sql);
  34. targetSort = SortBuilders.fieldSort("price").order(SortOrder.ASC).missing(0).setNestedPath("product");
  35. Assert.assertEquals(parseResult.getSortBuilderList().get(0).toString(), targetSort.toString());
  36. sql = "select id,productStatus from index.trx_order order by nvl(product.price, 0) asc";
  37. sql2DslParser = new ElasticSql2DslParser();
  38. parseResult = sql2DslParser.parse(sql);
  39. targetSort = SortBuilders.fieldSort("product.price").order(SortOrder.ASC).missing(0);
  40. Assert.assertEquals(parseResult.getSortBuilderList().get(0).toString(), targetSort.toString());
  41. sql = "select id,productStatus from index.trx_order order by product.price asc,nested_doc(productTags.sortNo) desc";
  42. sql2DslParser = new ElasticSql2DslParser();
  43. parseResult = sql2DslParser.parse(sql);
  44. targetSort = SortBuilders.fieldSort("product.price").order(SortOrder.ASC);
  45. Assert.assertEquals(parseResult.getSortBuilderList().get(0).toString(), targetSort.toString());
  46. targetSort = SortBuilders.fieldSort("sortNo").order(SortOrder.DESC).setNestedPath("productTags");
  47. Assert.assertEquals(parseResult.getSortBuilderList().get(1).toString(), targetSort.toString());
  48. }
  49. }