Skip to content
This repository was archived by the owner on Sep 4, 2019. It is now read-only.

Commit 05ec01a

Browse files
author
jiangnan
committed
添加like,not like查询
1 parent cc3a2bb commit 05ec01a

File tree

4 files changed

+39
-2
lines changed

4 files changed

+39
-2
lines changed

CHANGELOG

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,3 +13,5 @@
1313
2019-4-11: 添加Function Score
1414

1515
2019-4-24: 将elasticsearch-sql添加为elasticsearch插件
16+
17+
2019-4-28: 添加like not like 查询

README.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@ CHANGELOG
6565
2019-4-8: 添加高亮显示<br/>
6666
2019-4-11: 添加Function Score<br/>
6767
2019-4-24: 将elasticsearch-sql添加为elasticsearch插件
68+
2019-4-28: 添加like not like 查询
6869

6970
[CHANGELOG](https://github.com/iamazy/elasticsearch-sql/edit/master/CHANGELOG)
7071

@@ -189,9 +190,10 @@ public Map<String, Object> get(String cluster,String index,String type, String i
189190
- [x] ES Highlighter
190191
- [x] ES Boosting
191192
- [x] ES Function Score
193+
- [x] SQL Like
192194

193195
#### 未来将要添加的功能
194-
- [x] ES Highlighter (已添加)
196+
- [x] ES Highlighter
195197
- [ ] elasticsearch-sql[NLPChina]组件中我未添加的功能!!!
196198

197199
<font size="10">☀️</font>未来的想法是将功能完善的跟NLPChina团队一样多嘻嘻

src/main/java/io/github/iamazy/elasticsearch/dsl/sql/enums/SqlConditionOperator.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,5 +16,7 @@ public enum SqlConditionOperator {
1616
IsNotNull,
1717
In,
1818
NotIn,
19-
BetweenAnd
19+
BetweenAnd,
20+
Like,
21+
NotLike
2022
}

src/main/java/io/github/iamazy/elasticsearch/dsl/sql/parser/query/exact/BinaryQueryParser.java

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,19 @@
11
package io.github.iamazy.elasticsearch.dsl.sql.parser.query.exact;
22

3+
import com.alibaba.druid.sql.ast.SQLExpr;
34
import com.alibaba.druid.sql.ast.expr.SQLBinaryOpExpr;
45
import com.alibaba.druid.sql.ast.expr.SQLBinaryOperator;
6+
import com.alibaba.druid.sql.ast.expr.SQLCharExpr;
57
import com.alibaba.druid.sql.ast.expr.SQLNullExpr;
68
import io.github.iamazy.elasticsearch.dsl.sql.enums.SqlConditionOperator;
79
import io.github.iamazy.elasticsearch.dsl.sql.exception.ElasticSql2DslException;
810
import io.github.iamazy.elasticsearch.dsl.sql.helper.ElasticSqlArgConverter;
911
import io.github.iamazy.elasticsearch.dsl.sql.model.AtomicQuery;
12+
import io.github.iamazy.elasticsearch.dsl.sql.model.SqlCondition;
1013
import org.elasticsearch.index.query.ExistsQueryBuilder;
1114
import org.elasticsearch.index.query.QueryBuilder;
1215
import org.elasticsearch.index.query.QueryBuilders;
16+
import org.elasticsearch.index.query.RegexpQueryBuilder;
1317

1418

1519
public class BinaryQueryParser extends AbstractExactQueryParser {
@@ -32,6 +36,8 @@ public AtomicQuery parseBinaryQuery(SQLBinaryOpExpr binQueryExpr, String queryAs
3236
});
3337
}
3438

39+
40+
3541
//GT GTE LT LTE
3642
if (SQLBinaryOperator.GreaterThan == binaryOperator || SQLBinaryOperator.GreaterThanOrEqual == binaryOperator
3743
|| SQLBinaryOperator.LessThan == binaryOperator || SQLBinaryOperator.LessThanOrEqual == binaryOperator) {
@@ -84,6 +90,31 @@ else if (SqlConditionOperator.LessThanOrEqual == operator12) {
8490
});
8591
}
8692

93+
if(SQLBinaryOperator.Like == binaryOperator || SQLBinaryOperator.NotLike == binaryOperator){
94+
if(binQueryExpr.getRight() instanceof SQLCharExpr) {
95+
SQLCharExpr rightExpr = (SQLCharExpr) binQueryExpr.getRight();
96+
SQLExpr leftExpr = binQueryExpr.getLeft();
97+
SqlConditionOperator operator=SQLBinaryOperator.Like == binaryOperator?SqlConditionOperator.Like:SqlConditionOperator.NotLike;
98+
return parseCondition(binQueryExpr.getLeft(),operator,null,queryAs,((queryFieldName, operator1, rightParamValues) -> {
99+
String rightText=rightExpr.getText();
100+
if(rightExpr.getText().contains("%")){
101+
rightText=rightText.replace("%","*");
102+
}
103+
if(rightExpr.getText().contains("_")){
104+
rightText=rightText.replace("_","?");
105+
}
106+
RegexpQueryBuilder regexpQueryBuilder = QueryBuilders.regexpQuery(leftExpr.toString(), rightText);
107+
if(operator1.equals(SqlConditionOperator.Like)) {
108+
return regexpQueryBuilder;
109+
}else{
110+
return QueryBuilders.boolQuery().mustNot(regexpQueryBuilder);
111+
}
112+
}));
113+
}else{
114+
throw new ElasticSql2DslException("[syntax error] Like/NotLike expr right part should be a char expr");
115+
}
116+
}
117+
87118
throw new ElasticSql2DslException(String.format("[syntax error] Can not support binary query type[%s]", binQueryExpr.toString()));
88119
}
89120
}

0 commit comments

Comments
 (0)