|
|
@@ -0,0 +1,120 @@
|
|
|
+package org.elasticsearch.dsl.parser.query.method.term;
|
|
|
+
|
|
|
+import com.alibaba.druid.sql.ast.SQLExpr;
|
|
|
+import com.alibaba.druid.sql.ast.expr.SQLMethodInvokeExpr;
|
|
|
+import org.apache.commons.collections.MapUtils;
|
|
|
+import org.apache.commons.lang.StringUtils;
|
|
|
+import org.elasticsearch.common.unit.Fuzziness;
|
|
|
+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.parser.query.method.AbstractAtomMethodQueryParser;
|
|
|
+import org.elasticsearch.dsl.parser.query.method.IConditionMethodQueryBuilder;
|
|
|
+import org.elasticsearch.index.query.FuzzyQueryBuilder;
|
|
|
+import org.elasticsearch.index.query.QueryBuilder;
|
|
|
+import org.elasticsearch.index.query.QueryBuilders;
|
|
|
+
|
|
|
+import java.util.Map;
|
|
|
+
|
|
|
+public class FuzzyAtomQueryParser extends AbstractAtomMethodQueryParser {
|
|
|
+
|
|
|
+ public FuzzyAtomQueryParser(ParseActionListener parseActionListener) {
|
|
|
+ super(parseActionListener);
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ protected void checkQueryMethod(SQLMethodInvokeExpr methodQueryExpr, String queryAs, Object[] sqlArgs) {
|
|
|
+ if (Boolean.FALSE == "fuzzy".equalsIgnoreCase(methodQueryExpr.getMethodName())) {
|
|
|
+ throw new ElasticSql2DslException(String.format("[syntax error] Expected fuzzy query method name is [fuzzy],but get [%s]", methodQueryExpr.getMethodName()));
|
|
|
+ }
|
|
|
+
|
|
|
+ int paramCount = methodQueryExpr.getParameters().size();
|
|
|
+ if (paramCount != 2 && paramCount != 3) {
|
|
|
+ throw new ElasticSql2DslException(String.format("[syntax error] There's no %s args method: fuzzy", paramCount));
|
|
|
+ }
|
|
|
+
|
|
|
+ SQLExpr textExpr = methodQueryExpr.getParameters().get(1);
|
|
|
+
|
|
|
+ String text = ElasticSqlArgTransferHelper.transferSqlArg(textExpr, sqlArgs, false).toString();
|
|
|
+ if (StringUtils.isEmpty(text)) {
|
|
|
+ throw new ElasticSql2DslException("[syntax error] Fuzzy text can not be blank!");
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ @SuppressWarnings("unchecked")
|
|
|
+ protected AtomQuery parseMethodQueryExpr(SQLMethodInvokeExpr methodQueryExpr, String queryAs, Object[] sqlArgs) {
|
|
|
+ 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) {
|
|
|
+ FuzzyQueryBuilder fuzzyQuery = QueryBuilders.fuzzyQuery(queryFieldName, parameters[0].toString());
|
|
|
+
|
|
|
+ if (parameters.length == 2 && parameters[1] != null) {
|
|
|
+ Map<String, String> tExtraParamMap = (Map<String, String>) parameters[1];
|
|
|
+ setExtraMatchQueryParam(fuzzyQuery, tExtraParamMap);
|
|
|
+ }
|
|
|
+
|
|
|
+ return fuzzyQuery;
|
|
|
+ }
|
|
|
+ });
|
|
|
+ }
|
|
|
+
|
|
|
+ private void setExtraMatchQueryParam(FuzzyQueryBuilder fuzzyQuery, Map<String, String> extraParamMap) {
|
|
|
+ if (MapUtils.isEmpty(extraParamMap)) {
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ if (extraParamMap.containsKey("boost")) {
|
|
|
+ String val = extraParamMap.get("boost");
|
|
|
+ fuzzyQuery.boost(Float.valueOf(val));
|
|
|
+ }
|
|
|
+ if (extraParamMap.containsKey("transpositions")) {
|
|
|
+ String val = extraParamMap.get("transpositions");
|
|
|
+ fuzzyQuery.transpositions(Boolean.parseBoolean(val));
|
|
|
+ }
|
|
|
+ if (extraParamMap.containsKey("prefix_length")) {
|
|
|
+ String val = extraParamMap.get("prefix_length");
|
|
|
+ fuzzyQuery.prefixLength(Integer.valueOf(val));
|
|
|
+ }
|
|
|
+ if (extraParamMap.containsKey("max_expansions")) {
|
|
|
+ String val = extraParamMap.get("max_expansions");
|
|
|
+ fuzzyQuery.maxExpansions(Integer.valueOf(val));
|
|
|
+ }
|
|
|
+ if (extraParamMap.containsKey("rewrite")) {
|
|
|
+ String val = extraParamMap.get("rewrite");
|
|
|
+ fuzzyQuery.rewrite(val);
|
|
|
+ }
|
|
|
+
|
|
|
+ if (extraParamMap.containsKey("fuzziness")) {
|
|
|
+ String val = extraParamMap.get("fuzziness");
|
|
|
+
|
|
|
+ if ("ZERO".equalsIgnoreCase(val) || "0".equals(val)) {
|
|
|
+ fuzzyQuery.fuzziness(Fuzziness.ZERO);
|
|
|
+ }
|
|
|
+ if ("ONE".equalsIgnoreCase(val) || "1".equals(val)) {
|
|
|
+ fuzzyQuery.fuzziness(Fuzziness.ONE);
|
|
|
+ }
|
|
|
+
|
|
|
+ if ("TWO".equalsIgnoreCase(val) || "2".equals(val)) {
|
|
|
+ fuzzyQuery.fuzziness(Fuzziness.TWO);
|
|
|
+ }
|
|
|
+
|
|
|
+ if ("AUTO".equalsIgnoreCase(val)) {
|
|
|
+ fuzzyQuery.fuzziness(Fuzziness.AUTO);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+}
|