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 Apr 1, 2017
2 parents fada148 + f62ccd7 commit eb53aa1
Show file tree
Hide file tree
Showing 96 changed files with 1,319 additions and 788 deletions.
29 changes: 15 additions & 14 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ elasticsearch-query-tookit
toolkit version | ES version
-----------|-----------
master | 2.4.4
2.4.4.1 | 2.4.4
2.4.4.2 | 2.4.4
1.x | 1.4.5

介绍
Expand All @@ -32,6 +32,7 @@ SearchRequestBuilder searchReq = parseResult.toRequest(esClient);
//执行查询
SearchResponse response = searchReq.execute().actionGet();
```
注:其中routing by用于指定查询路由值,也可以不指定
生成的DSL如下:
```bash
{
Expand Down Expand Up @@ -74,15 +75,20 @@ SearchResponse response = searchReq.execute().actionGet();
2. 指定连接ES的连接串:jdbc:elastic:192.168.0.109:9300/product_cluster
3. 创建一个SqlMapClient对象,并指定sqlMapConfig.xml路径
```bash
<bean id="elasticDataSource" class="org.elasticsearch.jdbc.api.ElasticSingleConnectionDataSource" destroy-method="destroy">
<property name="driverClassName" value="org.elasticsearch.jdbc.api.ElasticDriver" />
<property name="url" value="jdbc:elastic:192.168.0.109:9300/product_cluster" />
<bean id="elasticDataSource" class="org.elasticsearch.api.ElasticSingleConnectionDataSource" destroy-method="destroy">
<property name="driverClassName" value="org.elasticsearch.api.ElasticDriver" />
<property name="url" value="jdbc:elastic:192.168.0.109:9300/lu-search-cluster" />
<property name="suppressClose" value="true" />
</bean>

<bean id="sqlMapClient" class="org.springframework.orm.ibatis.SqlMapClientFactoryBean">
<property name="dataSource" ref="elasticDataSource" />
<property name="configLocation" value="classpath:sqlMapConfig.xml"/>
</bean>

<bean id="sqlMapClientTemplate" class="org.elasticsearch.ElasticSqlMapClientTemplate">
<property name="sqlMapClient" ref="sqlMapClient"/>
</bean>
```

sqlMapConfig.xml文件内容如下:
Expand Down Expand Up @@ -123,9 +129,8 @@ PRODUCT.xml文件中声明select sql语句
@Repository
public class ProductDao {
@Autowired
@Qualifier("sqlMapClient")
private SqlMapClient sqlMapClient;

@Qualifier("sqlMapClientTemplate")
private ElasticSqlMapExecutor sqlMapClientTemplate;

public List<Product> getProductByCodeAndMatchWord(String matchWord, String productCode) throws SQLException {
Map<String, Object> paramMap = Maps.newHashMap();
Expand All @@ -134,13 +139,8 @@ public class ProductDao {
paramMap.put("routingVal", "A");
paramMap.put("matchWord", matchWord);
paramMap.put("prefixWord", matchWord);
String responseGson = (String) sqlMapClient.queryForObject("PRODUCT.getProductByCodeAndMatchWord", paramMap);

//反序列化查询结果
JdbcSearchResponseResolver responseResolver = new JdbcSearchResponseResolver(responseGson);
JdbcSearchResponse<Product> searchResponse = responseResolver.resolveSearchResponse(Product.class);

return searchResponse.getDocList();
return sqlMapClientTemplate.queryForList("PRODUCT.getProductByCodeAndMatchWord", paramMap, Product.class);

}
}
Expand All @@ -150,8 +150,9 @@ public class ProductDao {
@Test
public void testProductQuery() throws Exception {
BeanFactory factory = new ClassPathXmlApplicationContext("application-context.xml");

ProductDao productDao = factory.getBean(ProductDao.class);

List<Product> productList = productDao.getProductByCodeAndMatchWord("iphone 6s", "IP_6S");
for (Product product : productList) {
System.out.println(product.getProductName());
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -21,22 +21,18 @@ public AbstractFieldSpecificMethodQueryParser(ParseActionListener parseActionLis

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

protected abstract SQLExpr getFieldExpr(MethodInvocation invocation);
protected abstract SQLExpr defineFieldExpr(MethodInvocation invocation);

@Override
protected String getExtraParamString(MethodInvocation invocation) {
protected String defineExtraParamString(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());
ElasticSqlQueryField queryField = queryFieldParser.parseConditionQueryField(defineFieldExpr(invocation), invocation.getQueryAs());

AtomQuery atomQuery = null;
if (queryField.getQueryFieldType() == QueryFieldType.RootDocField || queryField.getQueryFieldType() == QueryFieldType.InnerDocField) {
Expand All @@ -50,15 +46,15 @@ private AtomQuery parseCondition(MethodInvocation invocation, Map<String, String
}

if (atomQuery == null) {
throw new ElasticSql2DslException(String.format("[syntax error] query condition field can not support type[%s]", queryField.getQueryFieldType()));
throw new ElasticSql2DslException(
String.format("[syntax error] query 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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,27 @@

import org.elasticsearch.dsl.bean.AtomQuery;
import org.elasticsearch.dsl.exception.ElasticSql2DslException;
import org.elasticsearch.dsl.helper.ElasticSqlMethodInvokeHelper;

public abstract class CheckableMethodQueryParser implements MethodQueryParser {

protected abstract void checkQueryMethod(MethodInvocation invocation) throws ElasticSql2DslException;
protected abstract void checkMethodInvokeArgs(MethodInvocation invocation) throws ElasticSql2DslException;

protected abstract AtomQuery parseMethodQueryWithCheck(MethodInvocation invocation) throws ElasticSql2DslException;

@Override
public boolean isMatchMethodInvocation(MethodInvocation invocation) {
return ElasticSqlMethodInvokeHelper.isMethodOf(defineMethodNames(), invocation.getMethodName());
}

@Override
public AtomQuery parseAtomMethodQuery(MethodInvocation invocation) throws ElasticSql2DslException {
checkQueryMethod(invocation);
if (!isMatchMethodInvocation(invocation)) {
throw new ElasticSql2DslException(
String.format("[syntax error] Expected method name is one of [%s],but get [%s]",
defineMethodNames(), invocation.getMethodName()));
}
checkMethodInvokeArgs(invocation);
return parseMethodQueryWithCheck(invocation);
}
}
Original file line number Diff line number Diff line change
@@ -1,27 +1,76 @@
package org.elasticsearch.dsl.parser.query.method;

import com.alibaba.druid.sql.ast.SQLExpr;
import com.alibaba.druid.sql.ast.expr.SQLMethodInvokeExpr;
import org.elasticsearch.dsl.helper.ElasticSqlArgTransferHelper;

import java.util.List;

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

public MethodInvocation(SQLMethodInvokeExpr matchQueryExpr, String queryAs, Object[] sqlArgs) {
this.matchQueryExpr = matchQueryExpr;
public MethodInvocation(SQLMethodInvokeExpr methodInvokeExpr, String queryAs, Object[] sqlArgs) {
if (methodInvokeExpr == null) {
throw new IllegalArgumentException("method invoke expression can not be null");
}
this.methodInvokeExpr = methodInvokeExpr;
this.queryAs = queryAs;
this.sqlArgs = sqlArgs;
}

public SQLMethodInvokeExpr getMatchQueryExpr() {
return matchQueryExpr;
}

public String getQueryAs() {
return queryAs;
}

public Object[] getSqlArgs() {
return sqlArgs;
}

public String getMethodName() {
return methodInvokeExpr.getMethodName();
}

public List<SQLExpr> getParameters() {
return methodInvokeExpr.getParameters();
}

public int getParameterCount() {
return methodInvokeExpr.getParameters().size();
}

public SQLExpr getFirstParameter(int index) {
return getParameter(0);
}

public SQLExpr getParameter(int index) {
return methodInvokeExpr.getParameters().get(index);
}

public Object getParameterAsObject(int index) {
SQLExpr paramExpr = methodInvokeExpr.getParameters().get(index);
return ElasticSqlArgTransferHelper.transferSqlArg(paramExpr, sqlArgs, false);
}

public String getParameterAsFormatDate(int index) {
SQLExpr paramExpr = methodInvokeExpr.getParameters().get(index);
return ElasticSqlArgTransferHelper.transferSqlArg(paramExpr, sqlArgs, true).toString();
}

public String getParameterAsString(int index) {
return getParameterAsObject(index).toString();
}

public String getLastParameterAsString() {
return getParameterAsObject(getParameterCount() - 1).toString();
}

public Double getParameterAsDouble(int index) {
return (Double) getParameterAsObject(index);
}

public Long getParameterAsLong(int index) {
return (Long) getParameterAsObject(index);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,12 @@
import org.elasticsearch.dsl.bean.AtomQuery;
import org.elasticsearch.dsl.exception.ElasticSql2DslException;

import java.util.List;

public interface MethodQueryParser {

List<String> defineMethodNames();

boolean isMatchMethodInvocation(MethodInvocation invocation);

AtomQuery parseAtomMethodQuery(MethodInvocation invocation) throws ElasticSql2DslException;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,11 @@

public abstract class ParameterizedMethodQueryParser extends CheckableMethodQueryParser {

private static final String COMMA = ",";
protected static final String COMMA = ",";

private static final String COLON = ":";
protected static final String COLON = ":";

protected abstract String getExtraParamString(MethodInvocation invocation);
protected abstract String defineExtraParamString(MethodInvocation invocation);

protected abstract AtomQuery parseMethodQueryWithExtraParams(
MethodInvocation invocation, Map<String, String> extraParamMap) throws ElasticSql2DslException;
Expand All @@ -26,7 +26,7 @@ protected AtomQuery parseMethodQueryWithCheck(MethodInvocation invocation) {
}

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

if (StringUtils.isBlank(extraParamString)) {
return Collections.emptyMap();
Expand All @@ -46,6 +46,9 @@ private Map<String, String> buildExtraParamMap(MethodInvocation invocation) {
}

protected Boolean isExtraParamsString(String extraParams) {
if (StringUtils.isBlank(extraParams)) {
return Boolean.FALSE;
}
for (String paramPair : extraParams.split(COMMA)) {
String[] paramPairArr = paramPair.split(COLON);
if (paramPairArr.length != 2) {
Expand Down
Loading

0 comments on commit eb53aa1

Please sign in to comment.