Skip to content

Commit 60d0b3a

Browse files
committed
script-filter support for file/indexed script
1 parent 56702a7 commit 60d0b3a

File tree

2 files changed

+31
-4
lines changed

2 files changed

+31
-4
lines changed

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

Lines changed: 30 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import com.alibaba.druid.sql.ast.SQLExpr;
44
import com.alibaba.druid.sql.ast.expr.SQLBinaryOpExpr;
55
import com.alibaba.druid.sql.ast.expr.SQLMethodInvokeExpr;
6+
import org.elasticsearch.script.ScriptService;
67
import org.nlpcn.es4sql.Util;
78
import org.nlpcn.es4sql.domain.Field;
89
import org.nlpcn.es4sql.domain.KVValue;
@@ -19,14 +20,17 @@
1920
public class ScriptFilter {
2021
private String script;
2122
private Map<String,Object> args;
22-
23+
private ScriptService.ScriptType scriptType;
2324
public ScriptFilter() {
25+
2426
args = null;
27+
scriptType = ScriptService.ScriptType.INLINE;
2528
}
2629

27-
public ScriptFilter(String script, Map<String, Object> args) {
30+
public ScriptFilter(String script, Map<String, Object> args, ScriptService.ScriptType scriptType) {
2831
this.script = script;
2932
this.args = args;
33+
this.scriptType = scriptType;
3034
}
3135

3236
public boolean tryParseFromMethodExpr(SQLMethodInvokeExpr expr) throws SqlParseException {
@@ -58,12 +62,32 @@ public boolean tryParseFromMethodExpr(SQLMethodInvokeExpr expr) throws SqlParseE
5862
SQLExpr right = binaryOpExpr.getRight();
5963
Object value = Util.expr2Object(right);
6064
String key = Util.extendedToString(binaryOpExpr.getLeft());
61-
args.put(key, value);
65+
if(key.equals("script_type")){
66+
parseAndUpdateScriptType(value.toString());
67+
}
68+
else {
69+
args.put(key, value);
70+
}
6271

6372
}
6473
return true;
6574
}
6675

76+
private void parseAndUpdateScriptType(String scriptType) {
77+
String scriptTypeUpper = scriptType.toUpperCase();
78+
switch(scriptTypeUpper){
79+
case "INLINE":
80+
this.scriptType = ScriptService.ScriptType.INLINE;
81+
break;
82+
case "INDEXED":
83+
this.scriptType = ScriptService.ScriptType.INDEXED;
84+
break;
85+
case "FILE":
86+
this.scriptType = ScriptService.ScriptType.FILE;
87+
break;
88+
}
89+
}
90+
6791
public boolean containsParameters(){
6892
return args!=null && args.size() > 0;
6993
}
@@ -72,6 +96,9 @@ public String getScript() {
7296
return script;
7397
}
7498

99+
public ScriptService.ScriptType getScriptType() {
100+
return scriptType;
101+
}
75102

76103
public Map<String, Object> getArgs() {
77104
return args;

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -253,7 +253,7 @@ private ToXContent make(Condition cond, String name, Object value) throws SqlPar
253253
if(scriptFilter.containsParameters()){
254254
params = scriptFilter.getArgs();
255255
}
256-
x = QueryBuilders.scriptQuery(new Script(scriptFilter.getScript(), ScriptService.ScriptType.INLINE, null, params));
256+
x = QueryBuilders.scriptQuery(new Script(scriptFilter.getScript(), scriptFilter.getScriptType(), null, params));
257257
break;
258258
default:
259259
throw new SqlParseException("not define type " + cond.getName());

0 commit comments

Comments
 (0)