|
@@ -1,9 +1,20 @@
|
|
|
package org.elasticsearch.dsl.parser.query.method.term;
|
|
package org.elasticsearch.dsl.parser.query.method.term;
|
|
|
|
|
|
|
|
|
|
+import com.alibaba.druid.sql.ast.SQLExpr;
|
|
|
import com.alibaba.druid.sql.ast.expr.SQLMethodInvokeExpr;
|
|
import com.alibaba.druid.sql.ast.expr.SQLMethodInvokeExpr;
|
|
|
|
|
+import org.apache.commons.collections.MapUtils;
|
|
|
|
|
+import org.apache.commons.lang.StringUtils;
|
|
|
import org.elasticsearch.dsl.bean.AtomQuery;
|
|
import org.elasticsearch.dsl.bean.AtomQuery;
|
|
|
|
|
+import org.elasticsearch.dsl.exception.ElasticSql2DslException;
|
|
|
|
|
+import org.elasticsearch.dsl.helper.ElasticSqlArgTransferHelper;
|
|
|
import org.elasticsearch.dsl.listener.ParseActionListener;
|
|
import org.elasticsearch.dsl.listener.ParseActionListener;
|
|
|
import org.elasticsearch.dsl.parser.query.method.AbstractAtomMethodQueryParser;
|
|
import org.elasticsearch.dsl.parser.query.method.AbstractAtomMethodQueryParser;
|
|
|
|
|
+import org.elasticsearch.dsl.parser.query.method.IConditionMethodQueryBuilder;
|
|
|
|
|
+import org.elasticsearch.index.query.PrefixQueryBuilder;
|
|
|
|
|
+import org.elasticsearch.index.query.QueryBuilder;
|
|
|
|
|
+import org.elasticsearch.index.query.QueryBuilders;
|
|
|
|
|
+
|
|
|
|
|
+import java.util.Map;
|
|
|
|
|
|
|
|
public class PrefixAtomQueryParser extends AbstractAtomMethodQueryParser {
|
|
public class PrefixAtomQueryParser extends AbstractAtomMethodQueryParser {
|
|
|
|
|
|
|
@@ -13,28 +24,65 @@ public class PrefixAtomQueryParser extends AbstractAtomMethodQueryParser {
|
|
|
|
|
|
|
|
@Override
|
|
@Override
|
|
|
protected void checkQueryMethod(SQLMethodInvokeExpr methodQueryExpr, String queryAs, Object[] sqlArgs) {
|
|
protected void checkQueryMethod(SQLMethodInvokeExpr methodQueryExpr, String queryAs, Object[] sqlArgs) {
|
|
|
- /*
|
|
|
|
|
- if (Boolean.FALSE == "match".equalsIgnoreCase(matchQueryExpr.getMethodName())) {
|
|
|
|
|
- throw new ElasticSql2DslException(String.format("[syntax error] Expected match query method name is [match],but get [%s]", matchQueryExpr.getMethodName()));
|
|
|
|
|
|
|
+ if (Boolean.FALSE == "prefix".equalsIgnoreCase(methodQueryExpr.getMethodName())) {
|
|
|
|
|
+ throw new ElasticSql2DslException(String.format("[syntax error] Expected prefix query method name is [prefix],but get [%s]", methodQueryExpr.getMethodName()));
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- int paramCount = matchQueryExpr.getParameters().size();
|
|
|
|
|
|
|
+ int paramCount = methodQueryExpr.getParameters().size();
|
|
|
if (paramCount != 2 && paramCount != 3) {
|
|
if (paramCount != 2 && paramCount != 3) {
|
|
|
throw new ElasticSql2DslException(String.format("[syntax error] There's no %s args method: match", paramCount));
|
|
throw new ElasticSql2DslException(String.format("[syntax error] There's no %s args method: match", paramCount));
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- SQLExpr textExpr = matchQueryExpr.getParameters().get(1);
|
|
|
|
|
|
|
+ SQLExpr textExpr = methodQueryExpr.getParameters().get(1);
|
|
|
|
|
|
|
|
String text = ElasticSqlArgTransferHelper.transferSqlArg(textExpr, sqlArgs, false).toString();
|
|
String text = ElasticSqlArgTransferHelper.transferSqlArg(textExpr, sqlArgs, false).toString();
|
|
|
- if(StringUtils.isEmpty(text)) {
|
|
|
|
|
- throw new ElasticSql2DslException("[syntax error] Search text can not be blank!");
|
|
|
|
|
|
|
+ if (StringUtils.isEmpty(text)) {
|
|
|
|
|
+ throw new ElasticSql2DslException("[syntax error] Prefix text can not be blank!");
|
|
|
}
|
|
}
|
|
|
- */
|
|
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
@Override
|
|
|
@SuppressWarnings("unchecked")
|
|
@SuppressWarnings("unchecked")
|
|
|
protected AtomQuery parseMethodQueryExpr(SQLMethodInvokeExpr methodQueryExpr, String queryAs, Object[] sqlArgs) {
|
|
protected AtomQuery parseMethodQueryExpr(SQLMethodInvokeExpr methodQueryExpr, String queryAs, Object[] sqlArgs) {
|
|
|
- return null;
|
|
|
|
|
|
|
+ SQLExpr queryField = methodQueryExpr.getParameters().get(0);
|
|
|
|
|
+ SQLExpr textExpr = methodQueryExpr.getParameters().get(1);
|
|
|
|
|
+
|
|
|
|
|
+ Map<String, String> extraParamMap = null;
|
|
|
|
|
+ if (methodQueryExpr.getParameters().size() == 3) {
|
|
|
|
|
+ SQLExpr ExtraParamExpr = methodQueryExpr.getParameters().get(2);
|
|
|
|
|
+ String extraParam = ElasticSqlArgTransferHelper.transferSqlArg(ExtraParamExpr, sqlArgs, false).toString();
|
|
|
|
|
+
|
|
|
|
|
+ extraParamMap = buildExtraMethodQueryParamsMap(extraParam);
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ Object text = ElasticSqlArgTransferHelper.transferSqlArg(textExpr, sqlArgs, false);
|
|
|
|
|
+
|
|
|
|
|
+ return parseCondition(queryField, new Object[]{text, extraParamMap}, queryAs, new IConditionMethodQueryBuilder() {
|
|
|
|
|
+ @Override
|
|
|
|
|
+ public QueryBuilder buildQuery(String queryFieldName, Object[] parameters) {
|
|
|
|
|
+ PrefixQueryBuilder prefixQuery = QueryBuilders.prefixQuery(queryFieldName, parameters[0].toString());
|
|
|
|
|
+
|
|
|
|
|
+ if (parameters.length == 2 && parameters[1] != null) {
|
|
|
|
|
+ Map<String, String> tExtraParamMap = (Map<String, String>) parameters[1];
|
|
|
|
|
+ setExtraMatchQueryParam(prefixQuery, tExtraParamMap);
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ return prefixQuery;
|
|
|
|
|
+ }
|
|
|
|
|
+ });
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ private void setExtraMatchQueryParam(PrefixQueryBuilder prefixQuery, Map<String, String> extraParamMap) {
|
|
|
|
|
+ if (MapUtils.isEmpty(extraParamMap)) {
|
|
|
|
|
+ return;
|
|
|
|
|
+ }
|
|
|
|
|
+ if (extraParamMap.containsKey("boost")) {
|
|
|
|
|
+ String val = extraParamMap.get("boost");
|
|
|
|
|
+ prefixQuery.boost(Float.valueOf(val));
|
|
|
|
|
+ }
|
|
|
|
|
+ if (extraParamMap.containsKey("rewrite")) {
|
|
|
|
|
+ String val = extraParamMap.get("rewrite");
|
|
|
|
|
+ prefixQuery.rewrite(val);
|
|
|
|
|
+ }
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|