Skip to content

Commit bd4d1c1

Browse files
committed
terms/term filter - better work with string literals
1 parent 64f6455 commit bd4d1c1

File tree

5 files changed

+59
-22
lines changed

5 files changed

+59
-22
lines changed

src/main/java/org/nlpcn/es4sql/domain/Condition.java

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package org.nlpcn.es4sql.domain;
22

33
import java.util.Arrays;
4+
import java.util.HashMap;
45
import java.util.Map;
56

67
import com.google.common.collect.BiMap;
@@ -16,12 +17,22 @@
1617
public class Condition extends Where {
1718

1819
public enum OPEAR {
19-
EQ, GT, LT, GTE, LTE, N, LIKE, NLIKE, IS, ISN, IN, NIN , BETWEEN ,NBETWEEN , GEO_INTERSECTS , GEO_BOUNDING_BOX , GEO_DISTANCE , GEO_DISTANCE_RANGE, GEO_POLYGON , GEO_CELL, IN_TERMS , IDS_QUERY;
20+
EQ, GT, LT, GTE, LTE, N, LIKE, NLIKE, IS, ISN, IN, NIN , BETWEEN ,NBETWEEN , GEO_INTERSECTS , GEO_BOUNDING_BOX , GEO_DISTANCE , GEO_DISTANCE_RANGE, GEO_POLYGON , GEO_CELL, IN_TERMS , TERM , IDS_QUERY;
2021

21-
public static Map<String,OPEAR> methodNameToOpear = ImmutableMap.of("in_terms",IN_TERMS,"terms",IN_TERMS,"ids",IDS_QUERY,"ids_query",IDS_QUERY);
22-
private static BiMap<OPEAR, OPEAR> negatives;
22+
public static Map<String,OPEAR> methodNameToOpear;
2323

24+
private static BiMap<OPEAR, OPEAR> negatives;
2425

26+
static {
27+
methodNameToOpear = new HashMap<>();
28+
methodNameToOpear.put("term",TERM);
29+
methodNameToOpear.put("matchterm",TERM);
30+
methodNameToOpear.put("match_term",TERM);
31+
methodNameToOpear.put("terms",IN_TERMS);
32+
methodNameToOpear.put("in_terms",IN_TERMS);
33+
methodNameToOpear.put("ids",IDS_QUERY);
34+
methodNameToOpear.put("ids_query",IDS_QUERY);
35+
}
2536
static {
2637
negatives = HashBiMap.create(7);
2738
negatives.put(EQ, N);

src/main/java/org/nlpcn/es4sql/parse/SqlParser.java

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -209,24 +209,20 @@ else if (expr instanceof SQLMethodInvokeExpr) {
209209

210210
private Object[] getMethodValuesWithSubQueries(SQLMethodInvokeExpr method) throws SqlParseException {
211211
List<Object> values = new ArrayList<>();
212-
boolean foundSubQuery = false;
213212
for(SQLExpr innerExpr : method.getParameters()){
214213
if(innerExpr instanceof SQLQueryExpr){
215-
foundSubQuery = true;
216214
Select select = parseSelect((MySqlSelectQueryBlock) ((SQLQueryExpr) innerExpr).getSubQuery().getQuery());
217215
values.add(new SubQueryExpression(select));
218216
}
217+
else if(innerExpr instanceof SQLTextLiteralExpr){
218+
values.add(((SQLTextLiteralExpr)innerExpr).getText());
219+
}
219220
else {
220221
values.add(innerExpr);
221222
}
222223

223224
}
224-
Object[] conditionValues ;
225-
if(foundSubQuery)
226-
conditionValues = values.toArray();
227-
else
228-
conditionValues = method.getParameters().toArray();
229-
return conditionValues;
225+
return values.toArray();
230226
}
231227

232228
private Object[] parseValue(List<SQLExpr> targetList) throws SqlParseException {

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

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -113,16 +113,6 @@ private ToXContent make(Condition cond, String name, SQLMethodInvokeExpr value)
113113
bqb = FilterBuilders.queryFilter((QueryBuilder) bqb);
114114
}
115115
break;
116-
case "match_term":
117-
case "matchterm":
118-
case "term":
119-
if(isQuery){
120-
bqb = QueryBuilders.termQuery(name,value.getParameters().get(0));
121-
}
122-
else {
123-
bqb = FilterBuilders.termFilter(name,value.getParameters().get(0));
124-
}
125-
break;
126116
default:
127117
throw new SqlParseException("it did not support this query method " + value.getMethodName());
128118

@@ -286,6 +276,16 @@ private ToXContent make(Condition cond, String name, Object value) throws SqlPar
286276
x = FilterBuilders.termsFilter(name,termValues);
287277
}
288278
break;
279+
280+
case TERM:
281+
Object term =( (Object[]) value)[0];
282+
if(isQuery){
283+
x = QueryBuilders.termQuery(name,term);
284+
}
285+
else {
286+
x = FilterBuilders.termFilter(name,term);
287+
}
288+
break;
289289
case IDS_QUERY:
290290
Object[] idsParameters = (Object[]) value;
291291
String[] ids;

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

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -306,7 +306,7 @@ public void inTestWithStrings() throws IOException, SqlParseException, SQLFeatur
306306
}
307307

308308
@Test
309-
public void inTermsTestWithStrings() throws IOException, SqlParseException, SQLFeatureNotSupportedException{
309+
public void inTermsTestWithIdentifiersTreatLikeStrings() throws IOException, SqlParseException, SQLFeatureNotSupportedException{
310310
SearchHits response = query(String.format("SELECT name FROM %s/gotCharacters WHERE name.firstname = IN_TERMS(daenerys,eddard) LIMIT 1000", TEST_INDEX));
311311
SearchHit[] hits = response.getHits();
312312
Assert.assertEquals(2, response.getTotalHits());
@@ -315,6 +315,16 @@ public void inTermsTestWithStrings() throws IOException, SqlParseException, SQLF
315315
assertThat(firstname, isOneOf("Daenerys", "Eddard"));
316316
}
317317
}
318+
@Test
319+
public void inTermsTestWithStrings() throws IOException, SqlParseException, SQLFeatureNotSupportedException{
320+
SearchHits response = query(String.format("SELECT name FROM %s/gotCharacters WHERE name.firstname = IN_TERMS('daenerys','eddard') LIMIT 1000", TEST_INDEX));
321+
SearchHit[] hits = response.getHits();
322+
Assert.assertEquals(2, response.getTotalHits());
323+
for(SearchHit hit : hits) {
324+
String firstname = ((Map<String,Object>) hit.getSource().get("name")).get("firstname").toString();
325+
assertThat(firstname, isOneOf("Daenerys", "Eddard"));
326+
}
327+
}
318328

319329
@Test
320330
public void inTermsTestWithNumbers() throws IOException, SqlParseException, SQLFeatureNotSupportedException{

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

Lines changed: 20 additions & 0 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.BoolFilterBuilder;
78
import org.junit.Assert;
89
import org.junit.BeforeClass;
910
import org.junit.Test;
@@ -13,6 +14,7 @@
1314
import org.nlpcn.es4sql.exception.SqlParseException;
1415
import org.nlpcn.es4sql.parse.ElasticSqlExprParser;
1516
import org.nlpcn.es4sql.parse.SqlParser;
17+
import org.nlpcn.es4sql.query.maker.FilterMaker;
1618

1719
import java.io.IOException;
1820
import java.sql.SQLFeatureNotSupportedException;
@@ -692,6 +694,24 @@ public void parseJoinWithOneTableOrderByRemoveAlias() throws SqlParseException {
692694

693695
}
694696

697+
@Test
698+
public void termsWithStringTest() throws SqlParseException {
699+
String query = "select * from x where y = IN_TERMS('a','b')";
700+
Select select = parser.parseSelect((SQLQueryExpr) queryToExpr(query));
701+
Condition condition = (Condition) select.getWhere().getWheres().get(0);
702+
Object[] values = (Object[]) condition.getValue();
703+
Assert.assertEquals("a",values[0]);
704+
Assert.assertEquals("b",values[1]);
705+
}
706+
707+
@Test
708+
public void termWithStringTest() throws SqlParseException {
709+
String query = "select * from x where y = TERM('a')";
710+
Select select = parser.parseSelect((SQLQueryExpr) queryToExpr(query));
711+
Condition condition = (Condition) select.getWhere().getWheres().get(0);
712+
Object[] values = (Object[]) condition.getValue();
713+
Assert.assertEquals("a",values[0]);
714+
}
695715

696716

697717
private SQLExpr queryToExpr(String query) {

0 commit comments

Comments
 (0)