spuerx 9 年之前
父節點
當前提交
d41cc8a916

+ 477 - 2
README.md

@@ -1,2 +1,477 @@
-# elasticsearch-sql2dsl
-实现SQL到DSL的转换
+SQL to DSL for Elasticsearch
+====================================
+
+elasticsearch-sql2dsl版本
+-------------
+
+sql2dsl version | ES version
+-----------|-----------
+master | 1.4.5
+
+sql2dsl介绍
+-------------
+> `elasticsearch-sql2dsl`是一款能将SQL转换为ES的查询语言DSL的工具包,目前只支持比较简单的查询。
+
+
+使用注意
+------
+> * 在SQL的where条件中,原子条件的左边必须为变量名,右边必须为查询参数值
+> * 针对`Inner Doc`,直接引用即可:`a.b.c.d`
+> * 针对`Nested Doc`,需要在内嵌文档加上`$`符号: `$b.c`
+
+
+## 使用示例
+```bash
+#下面index表示索引名,order表示文档类型名
+select * from index.order
+
+# 1. 默认从第0条数据开始,取15条
+# 2. 因为没有带where条件所以查询是match_all
+{
+  "from" : 0,
+  "size" : 15,
+  "query" : {
+    "match_all" : { }
+  }
+}
+
+# 带上分页参数查询
+select * from index.order limit 0,100
+
+# 1. 分页参数从0开始取100条
+{
+  "from" : 0,
+  "size" : 100,
+  "query" : {
+    "match_all" : { }
+  }
+}
+
+# where条件中带一个status参数
+select * from index.order where status='SUCCESS' limit 0,100
+
+{
+  "from" : 0,
+  "size" : 100,
+  "query" : {
+    "filtered" : {
+      "filter" : {
+        "bool" : {
+          "must" : {
+            "term" : {
+              "status" : "SUCCESS"
+            }
+          }
+        }
+      }
+    }
+  }
+}
+
+# totalPrice 范围查询
+select * from index.order where status='SUCCESS' and totalPrice > 1000 limit 0,100
+
+{
+  "from" : 0,
+  "size" : 100,
+  "query" : {
+    "filtered" : {
+      "filter" : {
+        "bool" : {
+          "must" : [ {
+            "term" : {
+              "status" : "SUCCESS"
+            }
+          }, {
+            "range" : {
+              "totalPrice" : {
+                "from" : 1000,
+                "to" : null,
+                "include_lower" : false,
+                "include_upper" : true
+              }
+            }
+          } ]
+        }
+      }
+    }
+  }
+}
+
+# between...and... 上下限都取
+select * from index.order where status='SUCCESS' and totalPrice between 1000 and 2000 limit 0,100
+
+{
+  "from" : 0,
+  "size" : 100,
+  "query" : {
+    "filtered" : {
+      "filter" : {
+        "bool" : {
+          "must" : [ {
+            "term" : {
+              "status" : "SUCCESS"
+            }
+          }, {
+            "range" : {
+              "totalPrice" : {
+                "from" : 1000,
+                "to" : 2000,
+                "include_lower" : true,
+                "include_upper" : true
+              }
+            }
+          } ]
+        }
+      }
+    }
+  }
+}
+
+# 日期范围查询,下面3条SQL等效
+select * from index.order where status='SUCCESS' and lastUpdateTime > '2017-01-01 00:00:00' limit 0,100
+select * from index.order where status='SUCCESS' and lastUpdateTime > '2017-01-01' limit 0,100
+#日期函数可以自定义日期格式
+select * from index.order where status='SUCCESS' and lastUpdateTime > date('yyyy-MM-dd', '2017-01-01') limit 0,100
+
+{
+  "from" : 0,
+  "size" : 100,
+  "query" : {
+    "filtered" : {
+      "filter" : {
+        "bool" : {
+          "must" : [ {
+            "term" : {
+              "status" : "SUCCESS"
+            }
+          }, {
+            "range" : {
+              "lastUpdateTime" : {
+                "from" : "2017-01-01T00:00:00.000+0800",
+                "to" : null,
+                "include_lower" : false,
+                "include_upper" : true
+              }
+            }
+          } ]
+        }
+      }
+    }
+  }
+}
+
+# 排序条件,price升序,publishDate降序
+select * from index.order where status='SUCCESS' order by price asc, publishDate desc
+
+{
+  "from" : 0,
+  "size" : 15,
+  "query" : {
+    "filtered" : {
+      "filter" : {
+        "bool" : {
+          "must" : {
+            "term" : {
+              "status" : "SUCCESS"
+            }
+          }
+        }
+      }
+    }
+  },
+  "sort" : [ {
+    "price" : {
+      "order" : "asc"
+    }
+  }, {
+    "publishDate" : {
+      "order" : "desc"
+    }
+  } ]
+}
+
+# 使用nvl函数指定默认值
+select * from index.order where status='SUCCESS' order by nvl(price,0) asc, publishDate desc
+
+{
+  "from" : 0,
+  "size" : 15,
+  "query" : {
+    "filtered" : {
+      "filter" : {
+        "bool" : {
+          "must" : {
+            "term" : {
+              "status" : "SUCCESS"
+            }
+          }
+        }
+      }
+    }
+  },
+  "sort" : [ {
+    "price" : {
+      "order" : "asc",
+      "missing" : 0
+    }
+  }, {
+    "publishDate" : {
+      "order" : "desc"
+    }
+  } ]
+}
+
+#内嵌文档排序,指定sort_mode
+select * from index.order where status='SUCCESS' order by nvl(product.$providers.sortNo, 0, 'min') asc, publishDate desc
+
+{
+  "from" : 0,
+  "size" : 15,
+  "query" : {
+    "filtered" : {
+      "filter" : {
+        "bool" : {
+          "must" : {
+            "term" : {
+              "status" : "SUCCESS"
+            }
+          }
+        }
+      }
+    }
+  },
+  "sort" : [ {
+    "product.providers.sortNo" : {
+      "order" : "asc",
+      "missing" : 0,
+      "mode" : "min",
+      "nested_path" : "product.providers"
+    }
+  }, {
+    "publishDate" : {
+      "order" : "desc"
+    }
+  } ]
+}
+
+# Inner Doc 查询
+select * from index.order where seller.name='JD' order by id desc
+
+{
+  "from" : 0,
+  "size" : 15,
+  "query" : {
+    "filtered" : {
+      "filter" : {
+        "bool" : {
+          "must" : {
+            "term" : {
+              "seller.name" : "JD"
+            }
+          }
+        }
+      }
+    }
+  },
+  "sort" : [ {
+    "id" : {
+      "order" : "desc"
+    }
+  } ]
+}
+
+
+#Nested Doc查询
+select * from index.order where product.$providers.name in ('JD', 'TB') and product.price < 1000 order by id desc
+
+{
+  "from" : 0,
+  "size" : 15,
+  "query" : {
+    "filtered" : {
+      "filter" : {
+        "bool" : {
+          "must" : [ {
+            "range" : {
+              "product.price" : {
+                "from" : null,
+                "to" : 1000,
+                "include_lower" : true,
+                "include_upper" : false
+              }
+            }
+          }, {
+            "nested" : {
+              "filter" : {
+                "terms" : {
+                  "product.providers.name" : [ "JD", "TB" ]
+                }
+              },
+              "path" : "product.providers"
+            }
+          } ]
+        }
+      }
+    }
+  },
+  "sort" : [ {
+    "id" : {
+      "order" : "desc"
+    }
+  } ]
+}
+
+#组合条件查询
+select * from index.order where (product.$providers.name in ('JD', 'TB') or product.$providers.channel='ONLINE') and (product.status='OPEN' or product.price < 1000) order by id desc
+
+{
+  "from" : 0,
+  "size" : 15,
+  "query" : {
+    "filtered" : {
+      "filter" : {
+        "bool" : {
+          "must" : [ {
+            "bool" : {
+              "should" : {
+                "nested" : {
+                  "filter" : {
+                    "bool" : {
+                      "should" : [ {
+                        "terms" : {
+                          "product.providers.name" : [ "JD", "TB" ]
+                        }
+                      }, {
+                        "term" : {
+                          "product.providers.channel" : "ONLINE"
+                        }
+                      } ]
+                    }
+                  },
+                  "path" : "product.providers"
+                }
+              }
+            }
+          }, {
+            "bool" : {
+              "should" : [ {
+                "term" : {
+                  "product.status" : "OPEN"
+                }
+              }, {
+                "range" : {
+                  "product.price" : {
+                    "from" : null,
+                    "to" : 1000,
+                    "include_lower" : true,
+                    "include_upper" : false
+                  }
+                }
+              } ]
+            }
+          } ]
+        }
+      }
+    }
+  },
+  "sort" : [ {
+    "id" : {
+      "order" : "desc"
+    }
+  } ]
+}
+
+# 查询字段
+select totalPrice, product.*, product.$seller.* from index.order
+
+{
+  "from" : 0,
+  "size" : 15,
+  "query" : {
+    "match_all" : { }
+  },
+  "_source" : {
+    "includes" : [ "totalPrice", "product.*", "product.seller.*" ],
+    "excludes" : [ ]
+  }
+}
+```
+
+```bash
+#聚合统计
+{
+  "from" : 0,
+  "size" : 15,
+  "query" : {
+    "match_all" : { }
+  },
+  "aggregations" : {
+    "agg_category" : {
+      "terms" : {
+        "field" : "category",
+        "size" : 500,
+        "shard_size" : 1000,
+        "min_doc_count" : 1,
+        "shard_min_doc_count" : 1,
+        "order" : {
+          "_count" : "desc"
+        }
+      },
+      "aggregations" : {
+        "agg_color" : {
+          "terms" : {
+            "field" : "color",
+            "size" : 500,
+            "shard_size" : 1000,
+            "min_doc_count" : 1,
+            "shard_min_doc_count" : 1,
+            "order" : {
+              "_count" : "desc"
+            }
+          },
+          "aggregations" : {
+            "agg_price" : {
+              "range" : {
+                "field" : "price",
+                "ranges" : [ {
+                  "key" : "0-100",
+                  "from" : 0.0,
+                  "to" : 100.0
+                }, {
+                  "key" : "100-200",
+                  "from" : 100.0,
+                  "to" : 200.0
+                }, {
+                  "key" : "200-300",
+                  "from" : 200.0,
+                  "to" : 300.0
+                } ]
+              },
+              "aggregations" : {
+                "min_price" : {
+                  "min" : {
+                    "field" : "price"
+                  }
+                },
+                "avg_price" : {
+                  "avg" : {
+                    "field" : "price"
+                  }
+                }
+              }
+            }
+          }
+        }
+      }
+    }
+  }
+}
+
+```
+
+作者:  [@陈楠][1]
+Email: 465360798@qq.com
+
+<完>
+

+ 5 - 4
src/main/java/org/elasticsearch/dsl/parser/syntax/QueryWhereConditionParser.java

@@ -72,12 +72,13 @@ public class QueryWhereConditionParser implements QueryParser {
         return new SQLCondition(parseAtomFilterCondition(dslContext, sqlExpr), SQLConditionType.Atom);
     }
 
-    private void combineFilterBuilder(List<AtomFilter> combiner, SQLCondition SQLCondition, SQLBoolOperator binOperator) {
-        if (SQLConditionType.Atom == SQLCondition.getSQLConditionType() || SQLCondition.getOperator() == binOperator) {
-            combiner.addAll(SQLCondition.getFilterList());
+    private void combineFilterBuilder(List<AtomFilter> combiner, SQLCondition sqlCondition, SQLBoolOperator binOperator) {
+        if (SQLConditionType.Atom == sqlCondition.getSQLConditionType() || sqlCondition.getOperator() == binOperator) {
+            combiner.addAll(sqlCondition.getFilterList());
         }
         else {
-            BoolFilterBuilder subBoolFilter = mergeAtomFilter(SQLCondition.getFilterList(), binOperator);
+            //todo binOperator -> sqlCondition.getOperator()
+            BoolFilterBuilder subBoolFilter = mergeAtomFilter(sqlCondition.getFilterList(), sqlCondition.getOperator());
             combiner.add(new AtomFilter(subBoolFilter));
         }
     }

+ 2 - 2
src/test/java/org/elasticsearch/SqlParserLimitTest.java

@@ -9,7 +9,7 @@ import org.junit.Test;
 public class SqlParserLimitTest {
     @Test
     public void testParseLimitExpr() {
-        String sql = "select id,productStatus from index.trx_order trx where trx.status='SUCCESS' limit 5,15";
+        String sql = "select id,status from index.order t where t.status='SUCCESS' limit 5,15";
         ElasticSql2DslParser sql2DslParser = new ElasticSql2DslParser();
         ElasticSqlParseResult parseResult = sql2DslParser.parse(sql);
 
@@ -19,7 +19,7 @@ public class SqlParserLimitTest {
 
     @Test
     public void testParseLimitExprWithArgs() {
-        String sql = "select id,productStatus from index.trx_order trx where trx.status='SUCCESS' limit ?,?";
+        String sql = "select id,status from index.order t where t.status='SUCCESS' limit ?,?";
         ElasticSql2DslParser sql2DslParser = new ElasticSql2DslParser();
         ElasticSqlParseResult parseResult = sql2DslParser.parse(sql, new Object[]{5, 15});
 

+ 10 - 28
src/test/java/org/elasticsearch/SqlParserListenerTest.java

@@ -1,46 +1,28 @@
 package org.elasticsearch;
 
-import com.google.common.collect.Lists;
-import org.elasticsearch.dsl.enums.QueryFieldType;
-import org.elasticsearch.dsl.parser.ElasticSql2DslParser;
 import org.elasticsearch.dsl.bean.ElasticSqlQueryField;
-import org.elasticsearch.dsl.bean.ElasticSqlParseResult;
-import org.elasticsearch.dsl.parser.listener.ParseActionListenerAdapter;
 import org.elasticsearch.dsl.enums.SQLConditionOperator;
+import org.elasticsearch.dsl.parser.ElasticSql2DslParser;
+import org.elasticsearch.dsl.parser.listener.ParseActionListenerAdapter;
+import org.junit.Assert;
 import org.junit.Test;
 
-import java.util.List;
-
 public class SqlParserListenerTest {
     @Test
     public void testParseActionListener() {
-        final List<String> avaliableIndex = Lists.newArrayList("2016");
-
-        String sql = "select id,productStatus from index.trx_order trx where trx.status = 'SUCCESS' and updatedAt > '2017-01-01' limit 5,15";
+        String sql = "select id,status from index.order t where t.status = 'SUCCESS' and lastUpdatedTime > '2017-01-01' limit 5,15";
         ElasticSql2DslParser sql2DslParser = new ElasticSql2DslParser();
-        ElasticSqlParseResult parseResult = sql2DslParser.parse(sql, new ParseActionListenerAdapter() {
+        sql2DslParser.parse(sql, new ParseActionListenerAdapter() {
             @Override
             public void onAtomConditionParse(ElasticSqlQueryField paramName, Object[] paramValues, SQLConditionOperator operator) {
-                if (paramName.getQueryFieldType() == QueryFieldType.RootDocField
-                        && "updatedAt".equals(paramName.getSimpleQueryFieldName())) {
-                    if (SQLConditionOperator.GreaterThan == operator || SQLConditionOperator.GreaterThanOrEqual == operator) {
-
-                    }
-
-                    if (SQLConditionOperator.LessThan == operator || SQLConditionOperator.LessThanOrEqual == operator) {
-
-                    }
-
-                    if (SQLConditionOperator.BetweenAnd == operator) {
-
-                    }
-
-                    if (SQLConditionOperator.Equality == operator || SQLConditionOperator.In == operator) {
+                if (SQLConditionOperator.Equality == operator) {
+                    Assert.assertEquals("status", paramName.getQueryFieldFullName());
+                }
 
-                    }
+                if (SQLConditionOperator.GreaterThan == operator) {
+                    Assert.assertEquals("lastUpdatedTime", paramName.getQueryFieldFullName());
                 }
             }
         });
-        System.out.println(parseResult.toDsl());
     }
 }

+ 14 - 7
src/test/java/org/elasticsearch/SqlParserOrderByTest.java

@@ -12,7 +12,7 @@ import org.junit.Test;
 public class SqlParserOrderByTest {
     @Test
     public void testParseEqExpr() {
-        String sql = "select id,productStatus from index.trx_order order by price asc,id desc,updatedAt asc";
+        String sql = "select id,status from index.order order by price asc,id desc,lastUpdateTime asc";
         ElasticSql2DslParser sql2DslParser = new ElasticSql2DslParser();
         ElasticSqlParseResult parseResult = sql2DslParser.parse(sql);
 
@@ -20,35 +20,35 @@ public class SqlParserOrderByTest {
         Assert.assertEquals(parseResult.getOrderBy().get(0).toString(), targetSort.toString());
         targetSort = SortBuilders.fieldSort("id").order(SortOrder.DESC);
         Assert.assertEquals(parseResult.getOrderBy().get(1).toString(), targetSort.toString());
-        targetSort = SortBuilders.fieldSort("updatedAt").order(SortOrder.ASC);
+        targetSort = SortBuilders.fieldSort("lastUpdateTime").order(SortOrder.ASC);
         Assert.assertEquals(parseResult.getOrderBy().get(2).toString(), targetSort.toString());
 
-        sql = "select id,productStatus from index.trx_order order by nvl(price, 0) asc";
+        sql = "select id,status from index.order order by nvl(price, 0) asc";
         sql2DslParser = new ElasticSql2DslParser();
         parseResult = sql2DslParser.parse(sql);
         targetSort = SortBuilders.fieldSort("price").order(SortOrder.ASC).missing(0);
         Assert.assertEquals(parseResult.getOrderBy().get(0).toString(), targetSort.toString());
 
-        sql = "select id,productStatus from index.trx_order order by nvl(product.price, 0) asc";
+        sql = "select id,status from index.order order by nvl(product.price, 0) asc";
         sql2DslParser = new ElasticSql2DslParser();
         parseResult = sql2DslParser.parse(sql);
         targetSort = SortBuilders.fieldSort("product.price").order(SortOrder.ASC).missing(0);
         Assert.assertEquals(parseResult.getOrderBy().get(0).toString(), targetSort.toString());
 
-        sql = "select id,productStatus from index.trx_order order by nvl($product.price, 0) asc";
+        sql = "select id,status from index.order order by nvl($product.price, 0) asc";
         sql2DslParser = new ElasticSql2DslParser();
         parseResult = sql2DslParser.parse(sql);
         targetSort = SortBuilders.fieldSort("product.price").order(SortOrder.ASC).missing(0).setNestedPath("product");
         Assert.assertEquals(parseResult.getOrderBy().get(0).toString(), targetSort.toString());
 
 
-        sql = "select id,productStatus from index.trx_order order by nvl(product.price, 0) asc";
+        sql = "select id,status from index.order order by nvl(product.price, 0) asc";
         sql2DslParser = new ElasticSql2DslParser();
         parseResult = sql2DslParser.parse(sql);
         targetSort = SortBuilders.fieldSort("product.price").order(SortOrder.ASC).missing(0);
         Assert.assertEquals(parseResult.getOrderBy().get(0).toString(), targetSort.toString());
 
-        sql = "select id,productStatus from index.trx_order order by product.price asc, $productTags.sortNo desc";
+        sql = "select id,status from index.order order by product.price asc, $productTags.sortNo desc";
         sql2DslParser = new ElasticSql2DslParser();
         parseResult = sql2DslParser.parse(sql);
         targetSort = SortBuilders.fieldSort("product.price").order(SortOrder.ASC);
@@ -57,4 +57,11 @@ public class SqlParserOrderByTest {
         Assert.assertEquals(parseResult.getOrderBy().get(1).toString(), targetSort.toString());
     }
 
+    @Test
+    public void testX() {
+        String sql = "select totalPrice, product.*, product.$seller.* from index.order";
+        ElasticSql2DslParser sql2DslParser = new ElasticSql2DslParser();
+        System.out.println(sql2DslParser.parse(sql).toDsl());
+    }
+
 }

+ 10 - 17
src/test/java/org/elasticsearch/SqlParserRoutingTest.java

@@ -9,34 +9,27 @@ import org.junit.Test;
 public class SqlParserRoutingTest {
     @Test
     public void testParseRoutingExpr() {
-        String sql = "select * from index.trx_order trx where trx.status='SUCCESS' order by id asc routing by '801','802' limit 5,15";
+        String sql = "select * from index.order t where t.status='SUCCESS' order by id asc routing by 'A','B' limit 5,15";
         ElasticSql2DslParser sql2DslParser = new ElasticSql2DslParser();
         ElasticSqlParseResult parseResult = sql2DslParser.parse(sql);
 
-        Assert.assertTrue("801".equalsIgnoreCase(parseResult.getRoutingBy().get(0)));
-        Assert.assertTrue("802".equalsIgnoreCase(parseResult.getRoutingBy().get(1)));
+        Assert.assertTrue("A".equalsIgnoreCase(parseResult.getRoutingBy().get(0)));
+        Assert.assertTrue("B".equalsIgnoreCase(parseResult.getRoutingBy().get(1)));
 
-        sql = "select * from index.trx_order trx where trx.status='SUCCESS' order by id asc routing by '801' limit 5,15";
+        sql = "select * from index.order t where t.status='SUCCESS' order by id asc routing by 'A' limit 5,15";
         sql2DslParser = new ElasticSql2DslParser();
         parseResult = sql2DslParser.parse(sql);
-        Assert.assertTrue("801".equalsIgnoreCase(parseResult.getRoutingBy().get(0)));
+        Assert.assertTrue("A".equalsIgnoreCase(parseResult.getRoutingBy().get(0)));
     }
 
+
     @Test
     public void testParseRoutingExprWithArgs() {
-        String sql = "select * from index.trx_order trx where trx.status='SUCCESS' order by id asc routing by ?,? limit 5,15";
+        String sql = "select * from index.order t where t.status='SUCCESS' order by id asc routing by ?,? limit 5,15";
         ElasticSql2DslParser sql2DslParser = new ElasticSql2DslParser();
-        ElasticSqlParseResult parseResult = sql2DslParser.parse(sql, new Object[]{"801", "802"});
+        ElasticSqlParseResult parseResult = sql2DslParser.parse(sql, new Object[]{"A", "B"});
 
-        Assert.assertTrue("801".equalsIgnoreCase(parseResult.getRoutingBy().get(0)));
-        Assert.assertTrue("802".equalsIgnoreCase(parseResult.getRoutingBy().get(1)));
+        Assert.assertTrue("A".equalsIgnoreCase(parseResult.getRoutingBy().get(0)));
+        Assert.assertTrue("B".equalsIgnoreCase(parseResult.getRoutingBy().get(1)));
     }
-//
-//    @Test
-//    public void testJoin() {
-//        String syntax = "select * from index.trx_order trx nested join product.xx x";
-//        ElasticSql2DslParser sql2DslParser = new ElasticSql2DslParser();
-//        ElasticSqlParseResult parseResult = sql2DslParser.parse(syntax);
-//    }
-
 }

+ 25 - 26
src/test/java/org/elasticsearch/SqlParserSelectFieldTest.java

@@ -11,18 +11,18 @@ public class SqlParserSelectFieldTest {
 
     @Test
     public void testParseFromSource() {
-        String sql = "select id,productStatus from index.trx_order trx";
+        String sql = "select id,status from index.order t";
         ElasticSql2DslParser sql2DslParser = new ElasticSql2DslParser();
         ElasticSqlParseResult parseResult = sql2DslParser.parse(sql);
 
         Assert.assertEquals(parseResult.getIndices().get(0), "index");
-        Assert.assertEquals(parseResult.getType(), "trx_order");
-        Assert.assertEquals(parseResult.getQueryAs(), "trx");
+        Assert.assertEquals(parseResult.getType(), "order");
+        Assert.assertEquals(parseResult.getQueryAs(), "t");
     }
 
     @Test
     public void testParseDefaultLimit() {
-        String sql = "select id,productStatus from index.trx_order";
+        String sql = "select id,status from index.order";
         ElasticSql2DslParser sql2DslParser = new ElasticSql2DslParser();
         ElasticSqlParseResult parseResult = sql2DslParser.parse(sql);
 
@@ -32,45 +32,44 @@ public class SqlParserSelectFieldTest {
 
     @Test
     public void testParseOriSelectField() {
-        String sql = "select id,productStatus from index.trx_order";
+        String sql = "select id,status from index.order t";
         ElasticSql2DslParser sql2DslParser = new ElasticSql2DslParser();
         ElasticSqlParseResult parseResult = sql2DslParser.parse(sql);
 
         Assert.assertTrue(parseResult.getQueryFieldList().size() == 2);
         Assert.assertEquals(parseResult.getQueryFieldList().get(0), "id");
-        Assert.assertEquals(parseResult.getQueryFieldList().get(1), "productStatus");
+        Assert.assertEquals(parseResult.getQueryFieldList().get(1), "status");
     }
 
     @Test
     public void testSelectAllField() {
-        String sql = "select * from index.trx_order order by id asc routing by '801','802','803' limit 1,2";
+        String sql = "select * from index.order order by id asc routing by 'A','B','C' limit 1,2";
         ElasticSql2DslParser sql2DslParser = new ElasticSql2DslParser();
         ElasticSqlParseResult parseResult = sql2DslParser.parse(sql);
-        System.out.println(parseResult.toString());
         Assert.assertTrue(CollectionUtils.isEmpty(parseResult.getQueryFieldList()));
     }
 
     @Test
     public void testInnerDocField() {
-        String sql = "select id,product.productStatus from index.trx_order";
+        String sql = "select id,product.status from index.order";
         ElasticSql2DslParser sql2DslParser = new ElasticSql2DslParser();
         ElasticSqlParseResult parseResult = sql2DslParser.parse(sql);
         Assert.assertEquals(parseResult.getQueryFieldList().get(0), "id");
-        Assert.assertEquals(parseResult.getQueryFieldList().get(1), "product.productStatus");
+        Assert.assertEquals(parseResult.getQueryFieldList().get(1), "product.status");
 
-        sql = "select trx.product.productStatus from index.trx_order trx";
+        sql = "select id,product.status from index.order t";
         parseResult = sql2DslParser.parse(sql);
-        Assert.assertEquals(parseResult.getQueryFieldList().get(0), "product.productStatus");
+        Assert.assertEquals(parseResult.getQueryFieldList().get(1), "product.status");
 
-        sql = "select tmp.product.productStatus from index.trx_order trx";
+        sql = "select tmp.product.status from index.order t";
         parseResult = sql2DslParser.parse(sql);
-        Assert.assertEquals(parseResult.getQueryFieldList().get(0), "tmp.product.productStatus");
+        Assert.assertEquals(parseResult.getQueryFieldList().get(0), "tmp.product.status");
 
-        sql = "select product.productStatus from index.trx_order trx";
+        sql = "select product.status from index.order t";
         parseResult = sql2DslParser.parse(sql);
-        Assert.assertEquals(parseResult.getQueryFieldList().get(0), "product.productStatus");
+        Assert.assertEquals(parseResult.getQueryFieldList().get(0), "product.status");
 
-        sql = "select id,product.* from index.trx_order trx";
+        sql = "select id,product.* from index.order t";
         parseResult = sql2DslParser.parse(sql);
         Assert.assertEquals(parseResult.getQueryFieldList().get(0), "id");
         Assert.assertEquals(parseResult.getQueryFieldList().get(1), "product.*");
@@ -78,25 +77,25 @@ public class SqlParserSelectFieldTest {
 
     @Test
     public void testNestedDocField() {
-        String sql = "select id,$product.productStatus from index.trx_order";
+        String sql = "select id,$product.status from index.order";
         ElasticSql2DslParser sql2DslParser = new ElasticSql2DslParser();
         ElasticSqlParseResult parseResult = sql2DslParser.parse(sql);
         Assert.assertEquals(parseResult.getQueryFieldList().get(0), "id");
-        Assert.assertEquals(parseResult.getQueryFieldList().get(1), "product.productStatus");
+        Assert.assertEquals(parseResult.getQueryFieldList().get(1), "product.status");
 
-        sql = "select trx.$product.productStatus from index.trx_order trx";
+        sql = "select t.$product.status from index.order t";
         parseResult = sql2DslParser.parse(sql);
-        Assert.assertEquals(parseResult.getQueryFieldList().get(0), "product.productStatus");
+        Assert.assertEquals(parseResult.getQueryFieldList().get(0), "product.status");
 
-        sql = "select $tmp.product.productStatus from index.trx_order trx";
+        sql = "select $tmp.product.status from index.order t";
         parseResult = sql2DslParser.parse(sql);
-        Assert.assertEquals(parseResult.getQueryFieldList().get(0), "tmp.product.productStatus");
+        Assert.assertEquals(parseResult.getQueryFieldList().get(0), "tmp.product.status");
 
-        sql = "select product.productStatus from index.trx_order trx";
+        sql = "select product.status from index.order t";
         parseResult = sql2DslParser.parse(sql);
-        Assert.assertEquals(parseResult.getQueryFieldList().get(0), "product.productStatus");
+        Assert.assertEquals(parseResult.getQueryFieldList().get(0), "product.status");
 
-        sql = "select id,product.* from index.trx_order trx";
+        sql = "select id,product.* from index.order t";
         parseResult = sql2DslParser.parse(sql);
         Assert.assertEquals(parseResult.getQueryFieldList().get(0), "id");
         Assert.assertEquals(parseResult.getQueryFieldList().get(1), "product.*");

+ 23 - 29
src/test/java/org/elasticsearch/SqlParserWhereConditionTest.java

@@ -18,38 +18,38 @@ import org.junit.Test;
 public class SqlParserWhereConditionTest {
     @Test
     public void testParseEqExpr() {
-        String sql = "select id,productStatus from index.trx_order trx where trx.status='SUCCESS'";
+        String sql = "select id,status from index.order t where t.status='SUCCESS'";
         ElasticSql2DslParser sql2DslParser = new ElasticSql2DslParser();
         ElasticSqlParseResult parseResult = sql2DslParser.parse(sql);
         FilterBuilder targetFilter = FilterBuilders.boolFilter().must(FilterBuilders.termFilter("status", "SUCCESS"));
         Assert.assertEquals(parseResult.getWhereCondition().toString(), targetFilter.toString());
 
-        sql = "select id,productStatus from index.trx_order trx where trx.price='123.4'";
+        sql = "select id,status from index.order t where t.price='123.4'";
         parseResult = sql2DslParser.parse(sql);
         targetFilter = FilterBuilders.boolFilter().must(FilterBuilders.termFilter("price", "123.4"));
         Assert.assertEquals(parseResult.getWhereCondition().toString(), targetFilter.toString());
 
-        sql = "select id,productStatus from index.trx_order trx where product.price='123.4'";
+        sql = "select id,status from index.order t where product.price='123.4'";
         parseResult = sql2DslParser.parse(sql);
         targetFilter = FilterBuilders.boolFilter().must(FilterBuilders.termFilter("product.price", "123.4"));
         Assert.assertEquals(parseResult.getWhereCondition().toString(), targetFilter.toString());
 
-        sql = "select id,productStatus from index.trx_order trx where $product.price='123.4'";
+        sql = "select id,status from index.order t where $product.price='123.4'";
         parseResult = sql2DslParser.parse(sql);
         targetFilter = FilterBuilders.boolFilter().must(FilterBuilders.nestedFilter("product", FilterBuilders.termFilter("product.price", "123.4")));
         Assert.assertEquals(parseResult.getWhereCondition().toString(), targetFilter.toString());
 
-        sql = "select id,productStatus from index.trx_order trx where trx.$product.price='123.4'";
+        sql = "select id,status from index.order t where t.$product.price='123.4'";
         parseResult = sql2DslParser.parse(sql);
         targetFilter = FilterBuilders.boolFilter().must(FilterBuilders.nestedFilter("product", FilterBuilders.termFilter("product.price", "123.4")));
         Assert.assertEquals(parseResult.getWhereCondition().toString(), targetFilter.toString());
 
-        sql = "select id,productStatus from index.trx_order trx where abc.trx.$product.price='123.4'";
+        sql = "select id,status from index.order t where abc.t.$product.price='123.4'";
         parseResult = sql2DslParser.parse(sql);
-        targetFilter = FilterBuilders.boolFilter().must(FilterBuilders.nestedFilter("abc.trx.product", FilterBuilders.termFilter("abc.trx.product.price", "123.4")));
+        targetFilter = FilterBuilders.boolFilter().must(FilterBuilders.nestedFilter("abc.t.product", FilterBuilders.termFilter("abc.t.product.price", "123.4")));
         Assert.assertEquals(parseResult.getWhereCondition().toString(), targetFilter.toString());
 
-        sql = "select id,productStatus from index.trx_order trx where trx.product.price='123.4'";
+        sql = "select id,status from index.order t where t.product.price='123.4'";
         parseResult = sql2DslParser.parse(sql);
         targetFilter = FilterBuilders.boolFilter().must(FilterBuilders.termFilter("product.price", "123.4"));
         Assert.assertEquals(parseResult.getWhereCondition().toString(), targetFilter.toString());
@@ -60,17 +60,17 @@ public class SqlParserWhereConditionTest {
 
         ElasticSql2DslParser sql2DslParser = new ElasticSql2DslParser();
 
-        String sql = "select id,productStatus from index.trx_order trx where trx.price > 123.4";
+        String sql = "select id,status from index.order t where t.price > 123.4";
         ElasticSqlParseResult parseResult = sql2DslParser.parse(sql);
         FilterBuilder targetFilter = FilterBuilders.boolFilter().must(FilterBuilders.rangeFilter("price").gt(123.4));
         Assert.assertEquals(parseResult.getWhereCondition().toString(), targetFilter.toString());
 
-        sql = "select id,productStatus from index.trx_order trx where product.price > 123.4";
+        sql = "select id,status from index.order t where product.price > 123.4";
         parseResult = sql2DslParser.parse(sql);
         targetFilter = FilterBuilders.boolFilter().must(FilterBuilders.rangeFilter("product.price").gt(123.4));
         Assert.assertEquals(parseResult.getWhereCondition().toString(), targetFilter.toString());
 
-        sql = "select id,productStatus from index.trx_order trx where $product.price > 123.4";
+        sql = "select id,status from index.order t where $product.price > 123.4";
         parseResult = sql2DslParser.parse(sql);
         targetFilter = FilterBuilders.boolFilter().must(FilterBuilders.nestedFilter("product", FilterBuilders.rangeFilter("product.price").gt(123.4)));
         Assert.assertEquals(parseResult.getWhereCondition().toString(), targetFilter.toString());
@@ -82,37 +82,37 @@ public class SqlParserWhereConditionTest {
 
         ElasticSql2DslParser sql2DslParser = new ElasticSql2DslParser();
 
-        String sql = "select id,productStatus from index.trx_order trx where trx.updatedAt > '2017-01-25'";
+        String sql = "select id,status from index.order t where t.lastUpdateTime > '2017-01-25'";
         ElasticSqlParseResult parseResult = sql2DslParser.parse(sql);
-        FilterBuilder targetFilter = FilterBuilders.boolFilter().must(FilterBuilders.rangeFilter("updatedAt").gt("2017-01-25T00:00:00.000+0800"));
+        FilterBuilder targetFilter = FilterBuilders.boolFilter().must(FilterBuilders.rangeFilter("lastUpdateTime").gt("2017-01-25T00:00:00.000+0800"));
         Assert.assertEquals(parseResult.getWhereCondition().toString(), targetFilter.toString());
 
-        sql = "select id,productStatus from index.trx_order trx where trx.updatedAt > '2017-01-25 13:32'";
+        sql = "select id,status from index.order t where t.lastUpdateTime > '2017-01-25 13:32'";
         parseResult = sql2DslParser.parse(sql);
-        targetFilter = FilterBuilders.boolFilter().must(FilterBuilders.rangeFilter("updatedAt").gt("2017-01-25T13:32:00.000+0800"));
+        targetFilter = FilterBuilders.boolFilter().must(FilterBuilders.rangeFilter("lastUpdateTime").gt("2017-01-25T13:32:00.000+0800"));
         Assert.assertEquals(parseResult.getWhereCondition().toString(), targetFilter.toString());
 
-        sql = "select id,productStatus from index.trx_order trx where trx.updatedAt > '2017-01-25 13:32:59'";
+        sql = "select id,status from index.order t where t.lastUpdateTime > '2017-01-25 13:32:59'";
         parseResult = sql2DslParser.parse(sql);
-        targetFilter = FilterBuilders.boolFilter().must(FilterBuilders.rangeFilter("updatedAt").gt("2017-01-25T13:32:59.000+0800"));
+        targetFilter = FilterBuilders.boolFilter().must(FilterBuilders.rangeFilter("lastUpdateTime").gt("2017-01-25T13:32:59.000+0800"));
         Assert.assertEquals(parseResult.getWhereCondition().toString(), targetFilter.toString());
 
-        sql = "select id,productStatus from index.trx_order trx where trx.updatedAt > date('yyyy/MM/dd hh:mm:ss', '2017/01/25 13:32:59')";
+        sql = "select id,status from index.order t where t.lastUpdateTime > date('yyyy/MM/dd hh:mm:ss', '2017/01/25 13:32:59')";
         parseResult = sql2DslParser.parse(sql);
-        targetFilter = FilterBuilders.boolFilter().must(FilterBuilders.rangeFilter("updatedAt").gt("2017-01-25T13:32:59.000+0800"));
+        targetFilter = FilterBuilders.boolFilter().must(FilterBuilders.rangeFilter("lastUpdateTime").gt("2017-01-25T13:32:59.000+0800"));
         Assert.assertEquals(parseResult.getWhereCondition().toString(), targetFilter.toString());
 
 
-        sql = "select id,productStatus from index.trx_order trx where trx.updatedAt > date('yyyy/MM/dd hh-mm', '2017/01/25 13-32')";
+        sql = "select id,status from index.order t where t.lastUpdateTime > date('yyyy/MM/dd hh-mm', '2017/01/25 13-32')";
         parseResult = sql2DslParser.parse(sql);
-        targetFilter = FilterBuilders.boolFilter().must(FilterBuilders.rangeFilter("updatedAt").gt("2017-01-25T13:32:00.000+0800"));
+        targetFilter = FilterBuilders.boolFilter().must(FilterBuilders.rangeFilter("lastUpdateTime").gt("2017-01-25T13:32:00.000+0800"));
         Assert.assertEquals(parseResult.getWhereCondition().toString(), targetFilter.toString());
 
 
-        sql = "select id,productStatus from index.trx_order trx where trx.updatedAt between date('yyyy/MM/dd hh-mm', '2017/01/25 13-32') and '2018-10-25'";
+        sql = "select id,status from index.order t where t.lastUpdateTime between date('yyyy/MM/dd hh-mm', '2017/01/25 13-32') and '2018-10-25'";
         parseResult = sql2DslParser.parse(sql);
         targetFilter = FilterBuilders.boolFilter().must(
-                FilterBuilders.rangeFilter("updatedAt")
+                FilterBuilders.rangeFilter("lastUpdateTime")
                         .gte("2017-01-25T13:32:00.000+0800")
                         .lte("2018-10-25T00:00:00.000+0800")
         );
@@ -125,7 +125,6 @@ public class SqlParserWhereConditionTest {
         String sql = "select * from index where a.$b.c.$d.e > 2";
         ElasticSql2DslParser sql2DslParser = new ElasticSql2DslParser();
         ElasticSqlParseResult parseResult = sql2DslParser.parse(sql);
-        System.out.println(parseResult.toString());
     }
 
     @Test
@@ -140,9 +139,6 @@ public class SqlParserWhereConditionTest {
         FilterBuilder topFilter = FilterBuilders.boolFilter().must(categoryNameTerm).must(bookAuthorNestedFilter).must(bookPublisherCodeNestedFilter).must(bookProviderNameNestedFilter);
 
         searchReq.setQuery(QueryBuilders.filteredQuery(null, topFilter));
-
-        System.out.println(searchReq);
-
     }
 
     @Test
@@ -170,7 +166,5 @@ public class SqlParserWhereConditionTest {
 
         searchReq.addAggregation(topAgg);
 
-        System.out.println(searchReq);
-
     }
 }