Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit a5f647d

Browse files
committed
Merge commit '27d4ba8bd3de0c4f6bb1e15ec7cbefa21eb9093f' into elastic2.0
Conflicts: src/main/java/org/nlpcn/es4sql/query/maker/Maker.java
2 parents f4f8dcc + 27d4ba8 commit a5f647d

File tree

2 files changed

+17
-4
lines changed

2 files changed

+17
-4
lines changed

src/main/java/org/nlpcn/es4sql/query/maker/Maker.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,9 @@ private ToXContent make(Condition cond, String name, Object value) throws SqlPar
140140
}
141141
case LIKE:
142142
case NLIKE:
143-
String queryStr = ((String) value).replace('%', '*').replace('_', '?');
143+
String queryStr = ((String) value).replace("[%]","ESCAPEDPERCENTAGE").replace("[_]","ESCAPEDUNDERSCORE");
144+
queryStr = queryStr.replace('%', '*').replace('_', '?');
145+
queryStr = queryStr.replace("ESCAPEDPERCENTAGE","%").replace("ESCAPEDUNDERSCORE","_");
144146
x = QueryBuilders.wildcardQuery(name, queryStr);
145147
break;
146148
case GT:

src/test/java/org/nlpcn/es4sql/SqlParserTests.java

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import com.alibaba.druid.sql.ast.expr.SQLIntegerExpr;
55
import com.alibaba.druid.sql.ast.expr.SQLPropertyExpr;
66
import com.alibaba.druid.sql.ast.expr.SQLQueryExpr;
7+
import org.elasticsearch.index.query.BoolQueryBuilder;
78
import org.junit.Assert;
89
import org.junit.BeforeClass;
910
import org.junit.Test;
@@ -15,6 +16,7 @@
1516
import org.nlpcn.es4sql.parse.FieldMaker;
1617
import org.nlpcn.es4sql.parse.ScriptFilter;
1718
import org.nlpcn.es4sql.parse.SqlParser;
19+
import org.nlpcn.es4sql.query.maker.QueryMaker;
1820

1921
import java.io.IOException;
2022
import java.sql.SQLFeatureNotSupportedException;
@@ -529,7 +531,7 @@ public void nestedFieldOnWhereNoPathSimpleField() throws SqlParseException {
529531
Where where = select.getWhere().getWheres().get(0);
530532
Assert.assertTrue("where should be condition", where instanceof Condition);
531533
Condition condition = (Condition) where;
532-
Assert.assertTrue("condition should be nested",condition.isNested());
534+
Assert.assertTrue("condition should be nested", condition.isNested());
533535
Assert.assertEquals("message",condition.getNestedPath());
534536
Assert.assertEquals("message.name",condition.getName());
535537
}
@@ -701,7 +703,7 @@ public void termsWithStringTest() throws SqlParseException {
701703
Condition condition = (Condition) select.getWhere().getWheres().get(0);
702704
Object[] values = (Object[]) condition.getValue();
703705
Assert.assertEquals("a",values[0]);
704-
Assert.assertEquals("b",values[1]);
706+
Assert.assertEquals("b", values[1]);
705707
}
706708

707709
@Test
@@ -763,11 +765,20 @@ public void fieldsAsNumbersOnWhere() throws SqlParseException {
763765
LinkedList<Where> wheres = select.getWhere().getWheres();
764766
Assert.assertEquals(1, wheres.size());
765767
Where where = wheres.get(0);
766-
Assert.assertEquals(Condition.class,where.getClass());
768+
Assert.assertEquals(Condition.class, where.getClass());
767769
Condition condition = (Condition) where;
768770
Assert.assertEquals("3", condition.getName());
769771
}
770772

773+
@Test
774+
public void likeTestWithEscaped() throws SqlParseException {
775+
String query = "select * from x where name like '[_]hey_%[%]'";
776+
Select select = parser.parseSelect((SQLQueryExpr) queryToExpr(query));
777+
BoolQueryBuilder explan = QueryMaker.explan(select.getWhere());
778+
String filterAsString = explan.toString();
779+
Assert.assertTrue(filterAsString.contains("_hey?*%"));
780+
}
781+
771782

772783
@Test
773784
public void complexNestedAndOtherQuery() throws SqlParseException {

0 commit comments

Comments
 (0)