Skip to content

Commit

Permalink
Merge branch '2.x'
Browse files Browse the repository at this point in the history
  • Loading branch information
gitchennan committed Mar 31, 2017
2 parents d8b67ae + 96e1498 commit fada148
Show file tree
Hide file tree
Showing 44 changed files with 566 additions and 446 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@

import java.util.Map;

public abstract class AbstractAtomMethodQueryParser {
public abstract class AbstractAtomMethodQueryParser implements MethodQueryParser {

protected static final String COMMA = ",";

Expand All @@ -34,10 +34,10 @@ protected interface IConditionMethodQueryBuilder {
}


public final AtomQuery parseAtomMethodQuery(SQLMethodInvokeExpr methodQueryExpr, String queryAs, Object[] sqlArgs) {
checkQueryMethod(methodQueryExpr, queryAs, sqlArgs);
public final AtomQuery parseAtomMethodQuery(MethodInvocation invocation) {
checkQueryMethod(invocation.getMatchQueryExpr(), invocation.getQueryAs(), invocation.getSqlArgs());

return parseMethodQueryExpr(methodQueryExpr, queryAs, sqlArgs);
return parseMethodQueryExpr(invocation.getMatchQueryExpr(), invocation.getQueryAs(), invocation.getSqlArgs());
}

protected AtomQuery parseCondition(SQLExpr queryFieldExpr, Object[] parameters, String queryAs, IConditionMethodQueryBuilder queryBuilder) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
package org.elasticsearch.dsl.parser.query.method;

import com.alibaba.druid.sql.ast.SQLExpr;
import org.elasticsearch.dsl.bean.AtomQuery;
import org.elasticsearch.dsl.bean.ElasticSqlQueryField;
import org.elasticsearch.dsl.enums.QueryFieldType;
import org.elasticsearch.dsl.exception.ElasticSql2DslException;
import org.elasticsearch.dsl.listener.ParseActionListener;
import org.elasticsearch.dsl.parser.sql.QueryFieldParser;
import org.elasticsearch.index.query.QueryBuilder;

import java.util.Map;

public abstract class AbstractFieldSpecificMethodQueryParser extends ParameterizedMethodQueryParser {

protected ParseActionListener parseActionListener;

public AbstractFieldSpecificMethodQueryParser(ParseActionListener parseActionListener) {
this.parseActionListener = parseActionListener;
}

protected abstract QueryBuilder buildQuery(MethodInvocation invocation, String fieldName, Map<String, String> extraParams);

protected abstract SQLExpr getFieldExpr(MethodInvocation invocation);

@Override
protected String getExtraParamString(MethodInvocation invocation) {
//ignore extra params, subclass can override if necessary
return null;
}

@Override
protected AtomQuery parseMethodQueryWithExtraParams(MethodInvocation invocation, Map<String, String> extraParamMap) throws ElasticSql2DslException {
return parseCondition(invocation, extraParamMap);
}

private AtomQuery parseCondition(MethodInvocation invocation, Map<String, String> extraParamMap) {
QueryFieldParser queryFieldParser = new QueryFieldParser();
ElasticSqlQueryField queryField = queryFieldParser.parseConditionQueryField(getFieldExpr(invocation), invocation.getQueryAs());

AtomQuery atomQuery = null;
if (queryField.getQueryFieldType() == QueryFieldType.RootDocField || queryField.getQueryFieldType() == QueryFieldType.InnerDocField) {
QueryBuilder originalQuery = buildQuery(invocation, queryField.getQueryFieldFullName(), extraParamMap);
atomQuery = new AtomQuery(originalQuery);
}

if (queryField.getQueryFieldType() == QueryFieldType.NestedDocField) {
QueryBuilder originalQuery = buildQuery(invocation, queryField.getQueryFieldFullName(), extraParamMap);
atomQuery = new AtomQuery(originalQuery, queryField.getNestedDocContextPath());
}

if (atomQuery == null) {
throw new ElasticSql2DslException(String.format("[syntax error] query condition field can not support type[%s]", queryField.getQueryFieldType()));
}

onAtomMethodQueryConditionParse(queryField, invocation.getSqlArgs());

return atomQuery;
}


private void onAtomMethodQueryConditionParse(ElasticSqlQueryField paramName, Object[] parameters) {
try {
parseActionListener.onAtomMethodQueryConditionParse(paramName, parameters);
}
catch (Exception ex) {
try {
parseActionListener.onFailure(ex);
}
catch (Exception exp) {
//ignore;
}
}
}
}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,18 +1,17 @@
package org.elasticsearch.dsl.parser.query.method;

import com.alibaba.druid.sql.ast.expr.SQLMethodInvokeExpr;
import org.elasticsearch.dsl.bean.AtomQuery;
import org.elasticsearch.dsl.exception.ElasticSql2DslException;

public abstract class CheckableMethodQueryParser implements MethodQueryParser {

abstract void checkQueryMethod(SQLMethodInvokeExpr matchQueryExpr, String queryAs, Object[] sqlArgs);
protected abstract void checkQueryMethod(MethodInvocation invocation) throws ElasticSql2DslException;

abstract AtomQuery parseMethodQueryExpr(SQLMethodInvokeExpr matchQueryExpr, String queryAs, Object[] sqlArgs);
protected abstract AtomQuery parseMethodQueryWithCheck(MethodInvocation invocation) throws ElasticSql2DslException;

@Override
public AtomQuery parseAtomMethodQuery(SQLMethodInvokeExpr methodExpr, String queryAs, Object[] sqlArgs) throws ElasticSql2DslException {
checkQueryMethod(methodExpr, queryAs, sqlArgs);
return parseMethodQueryExpr(methodExpr, queryAs, sqlArgs);
public AtomQuery parseAtomMethodQuery(MethodInvocation invocation) throws ElasticSql2DslException {
checkQueryMethod(invocation);
return parseMethodQueryWithCheck(invocation);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package org.elasticsearch.dsl.parser.query.method;

import com.alibaba.druid.sql.ast.expr.SQLMethodInvokeExpr;

public class MethodInvocation {
private SQLMethodInvokeExpr matchQueryExpr;
private String queryAs;
private Object[] sqlArgs;

public MethodInvocation(SQLMethodInvokeExpr matchQueryExpr, String queryAs, Object[] sqlArgs) {
this.matchQueryExpr = matchQueryExpr;
this.queryAs = queryAs;
this.sqlArgs = sqlArgs;
}

public SQLMethodInvokeExpr getMatchQueryExpr() {
return matchQueryExpr;
}

public String getQueryAs() {
return queryAs;
}

public Object[] getSqlArgs() {
return sqlArgs;
}
}
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
package org.elasticsearch.dsl.parser.query.method;

import com.alibaba.druid.sql.ast.expr.SQLMethodInvokeExpr;
import org.elasticsearch.dsl.bean.AtomQuery;
import org.elasticsearch.dsl.exception.ElasticSql2DslException;

public interface MethodQueryParser {
boolean isMatchMethodInvocation(MethodInvocation invocation);

boolean isMethodOf(SQLMethodInvokeExpr methodExpr);

AtomQuery parseAtomMethodQuery(SQLMethodInvokeExpr methodExpr, String queryAs, Object[] sqlArgs) throws ElasticSql2DslException;
AtomQuery parseAtomMethodQuery(MethodInvocation invocation) throws ElasticSql2DslException;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
package org.elasticsearch.dsl.parser.query.method;

import com.google.common.collect.Maps;
import org.apache.commons.lang.StringUtils;
import org.elasticsearch.dsl.bean.AtomQuery;
import org.elasticsearch.dsl.exception.ElasticSql2DslException;

import java.util.Collections;
import java.util.Map;

public abstract class ParameterizedMethodQueryParser extends CheckableMethodQueryParser {

private static final String COMMA = ",";

private static final String COLON = ":";

protected abstract String getExtraParamString(MethodInvocation invocation);

protected abstract AtomQuery parseMethodQueryWithExtraParams(
MethodInvocation invocation, Map<String, String> extraParamMap) throws ElasticSql2DslException;

@Override
protected AtomQuery parseMethodQueryWithCheck(MethodInvocation invocation) {
Map<String, String> extraParamMap = buildExtraParamMap(invocation);
return parseMethodQueryWithExtraParams(invocation, extraParamMap);
}

private Map<String, String> buildExtraParamMap(MethodInvocation invocation) {
String extraParamString = getExtraParamString(invocation);

if (StringUtils.isBlank(extraParamString)) {
return Collections.emptyMap();
}

Map<String, String> extraParamMap = Maps.newHashMap();
for (String paramPair : extraParamString.split(COMMA)) {
String[] paramPairArr = paramPair.split(COLON);
if (paramPairArr.length == 2) {
extraParamMap.put(paramPairArr[0].trim(), paramPairArr[1].trim());
}
else {
throw new ElasticSql2DslException("Failed to parse query method extra param string!");
}
}
return extraParamMap;
}

protected Boolean isExtraParamsString(String extraParams) {
for (String paramPair : extraParams.split(COMMA)) {
String[] paramPairArr = paramPair.split(COLON);
if (paramPairArr.length != 2) {
return Boolean.FALSE;
}
}
return Boolean.TRUE;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import org.elasticsearch.dsl.exception.ElasticSql2DslException;
import org.elasticsearch.dsl.listener.ParseActionListener;
import org.elasticsearch.dsl.parser.query.method.AbstractAtomMethodQueryParser;
import org.elasticsearch.dsl.parser.query.method.MethodInvocation;

public class FullTextAtomQueryParser {

Expand All @@ -14,14 +15,15 @@ public FullTextAtomQueryParser(ParseActionListener parseActionListener) {
this.parseActionListener = parseActionListener;
}

public static Boolean isFulltextAtomQuery(SQLMethodInvokeExpr methodQueryExpr) {
public Boolean isFulltextAtomQuery(SQLMethodInvokeExpr methodQueryExpr) {
return MatchAtomQueryParser.isMatchQuery(methodQueryExpr) || MultiMatchAtomQueryParser.isMultiMatch(methodQueryExpr) ||
QueryStringAtomQueryParser.isQueryStringQuery(methodQueryExpr) || SimpleQueryStringAtomQueryParser.isSimpleQueryStringQuery(methodQueryExpr);
}

public AtomQuery parseFullTextAtomQuery(SQLMethodInvokeExpr methodQueryExpr, String queryAs, Object[] sqlArgs) {
AbstractAtomMethodQueryParser matchAtomQueryParser = getQueryParser(methodQueryExpr);
return matchAtomQueryParser.parseAtomMethodQuery(methodQueryExpr, queryAs, sqlArgs);
MethodInvocation methodInvocation = new MethodInvocation(methodQueryExpr, queryAs, sqlArgs);
return matchAtomQueryParser.parseAtomMethodQuery(methodInvocation);
}

private AbstractAtomMethodQueryParser getQueryParser(SQLMethodInvokeExpr methodQueryExpr) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import org.elasticsearch.dsl.helper.ElasticSqlMethodInvokeHelper;
import org.elasticsearch.dsl.listener.ParseActionListener;
import org.elasticsearch.dsl.parser.query.method.AbstractAtomMethodQueryParser;
import org.elasticsearch.dsl.parser.query.method.MethodInvocation;
import org.elasticsearch.index.query.MatchQueryBuilder;
import org.elasticsearch.index.query.QueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
Expand All @@ -31,6 +32,11 @@ public static Boolean isMatchQuery(SQLMethodInvokeExpr methodQueryExpr) {
return ElasticSqlMethodInvokeHelper.isMethodOf(MATCH_METHOD, methodQueryExpr.getMethodName());
}

@Override
public boolean isMatchMethodInvocation(MethodInvocation invocation) {
return false;
}

@Override
protected void checkQueryMethod(SQLMethodInvokeExpr methodQueryExpr, String queryAs, Object[] sqlArgs) {
if (Boolean.FALSE == isMatchQuery(methodQueryExpr)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import org.elasticsearch.dsl.helper.ElasticSqlMethodInvokeHelper;
import org.elasticsearch.dsl.listener.ParseActionListener;
import org.elasticsearch.dsl.parser.query.method.AbstractAtomMethodQueryParser;
import org.elasticsearch.dsl.parser.query.method.MethodInvocation;
import org.elasticsearch.index.query.MatchQueryBuilder;
import org.elasticsearch.index.query.MultiMatchQueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
Expand All @@ -31,6 +32,11 @@ public static Boolean isMultiMatch(SQLMethodInvokeExpr methodQueryExpr) {
return ElasticSqlMethodInvokeHelper.isMethodOf(MULTI_MATCH_METHOD, methodQueryExpr.getMethodName());
}

@Override
public boolean isMatchMethodInvocation(MethodInvocation invocation) {
return false;
}

@Override
protected void checkQueryMethod(SQLMethodInvokeExpr methodQueryExpr, String queryAs, Object[] sqlArgs) {
if (Boolean.FALSE == isMultiMatch(methodQueryExpr)) {
Expand Down
Loading

0 comments on commit fada148

Please sign in to comment.