Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit f0016fa

Browse files
author
石源
committed
support multi_match query NLPchina#638
1 parent 5d2d732 commit f0016fa

File tree

5 files changed

+115
-6
lines changed

5 files changed

+115
-6
lines changed

pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
<modelVersion>4.0.0</modelVersion>
44
<groupId>org.nlpcn</groupId>
55
<artifactId>elasticsearch-sql</artifactId>
6-
<version>6.2.3.0</version>
6+
<version>6.2.3.1</version>
77
<packaging>jar</packaging>
88
<description>Query elasticsearch using SQL</description>
99
<name>elasticsearch-sql</name>

src/main/java/org/nlpcn/es4sql/domain/Paramer.java

Lines changed: 58 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,17 @@
11
package org.nlpcn.es4sql.domain;
22

3+
import java.util.HashMap;
34
import java.util.List;
5+
import java.util.Map;
46

57
import com.alibaba.druid.sql.ast.SQLExpr;
68
import com.alibaba.druid.sql.ast.expr.SQLBinaryOpExpr;
79
import com.alibaba.druid.sql.ast.expr.SQLCharExpr;
810
import com.alibaba.druid.sql.ast.expr.SQLMethodInvokeExpr;
911
import com.alibaba.druid.sql.ast.expr.SQLNumericLiteralExpr;
12+
import org.elasticsearch.common.Strings;
1013
import org.elasticsearch.common.xcontent.ToXContent;
11-
import org.elasticsearch.index.query.MatchPhraseQueryBuilder;
12-
import org.elasticsearch.index.query.MatchQueryBuilder;
13-
import org.elasticsearch.index.query.QueryStringQueryBuilder;
14-
import org.elasticsearch.index.query.WildcardQueryBuilder;
14+
import org.elasticsearch.index.query.*;
1515
import org.nlpcn.es4sql.Util;
1616
import org.nlpcn.es4sql.exception.SqlParseException;
1717

@@ -23,6 +23,11 @@ public class Paramer {
2323
public String value;
2424
public Integer slop;
2525

26+
public Map<String, Float> fieldsBoosts = new HashMap<>();
27+
public String type;
28+
public Float tieBreaker;
29+
public Operator operator;
30+
2631
public static Paramer parseParamer(SQLMethodInvokeExpr method) throws SqlParseException {
2732
Paramer instance = new Paramer();
2833
List<SQLExpr> parameters = method.getParameters();
@@ -50,6 +55,27 @@ public static Paramer parseParamer(SQLMethodInvokeExpr method) throws SqlParseEx
5055
case "slop":
5156
instance.slop = Integer.parseInt(Util.expr2Object(sqlExpr.getRight()).toString());
5257
break;
58+
59+
case "fields":
60+
int index;
61+
for (String f : Strings.split(Util.expr2Object(sqlExpr.getRight()).toString(), ",")) {
62+
index = f.lastIndexOf('^');
63+
if (-1 < index) {
64+
instance.fieldsBoosts.put(f.substring(0, index), Float.parseFloat(f.substring(index + 1)));
65+
} else {
66+
instance.fieldsBoosts.put(f, 1.0F);
67+
}
68+
}
69+
break;
70+
case "type":
71+
instance.type = Util.expr2Object(sqlExpr.getRight()).toString();
72+
break;
73+
case "tie_breaker":
74+
instance.tieBreaker = Float.parseFloat(Util.expr2Object(sqlExpr.getRight()).toString());
75+
break;
76+
case "operator":
77+
instance.operator = Operator.fromString(Util.expr2Object(sqlExpr.getRight()).toString());
78+
break;
5379
default:
5480
break;
5581
}
@@ -108,4 +134,32 @@ public static ToXContent fullParamer(QueryStringQueryBuilder query, Paramer para
108134

109135
return query;
110136
}
137+
138+
public static ToXContent fullParamer(MultiMatchQueryBuilder query, Paramer paramer) {
139+
if (paramer.analysis != null) {
140+
query.analyzer(paramer.analysis);
141+
}
142+
143+
if (paramer.boost != null) {
144+
query.boost(paramer.boost);
145+
}
146+
147+
if (paramer.slop != null) {
148+
query.slop(paramer.slop);
149+
}
150+
151+
if (paramer.type != null) {
152+
query.type(paramer.type);
153+
}
154+
155+
if (paramer.tieBreaker != null) {
156+
query.tieBreaker(paramer.tieBreaker);
157+
}
158+
159+
if (paramer.operator != null) {
160+
query.operator(paramer.operator);
161+
}
162+
163+
return query;
164+
}
111165
}

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

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,14 @@ private ToXContent make(Condition cond, String name, SQLMethodInvokeExpr value)
107107
MatchPhraseQueryBuilder matchPhraseQuery = QueryBuilders.matchPhraseQuery(name, paramer.value);
108108
bqb = Paramer.fullParamer(matchPhraseQuery, paramer);
109109
break;
110+
111+
case "multimatchquery":
112+
case "multi_match":
113+
case "multimatch":
114+
paramer = Paramer.parseParamer(value);
115+
MultiMatchQueryBuilder multiMatchQuery = QueryBuilders.multiMatchQuery(paramer.value).fields(paramer.fieldsBoosts);
116+
bqb = Paramer.fullParamer(multiMatchQuery, paramer);
117+
break;
110118
default:
111119
throw new SqlParseException("it did not support this query method " + value.getMethodName());
112120

src/test/java/org/nlpcn/es4sql/ExplainTest.java

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,14 @@ public void orderByOnNestedFieldTest() throws Exception {
7777
assertThat(result.replaceAll("\\s+", ""), equalTo("{\"from\":0,\"size\":200,\"sort\":[{\"message.info\":{\"order\":\"asc\",\"nested\":{\"path\":\"message\"}}}]}"));
7878
}
7979

80-
private String explain(String sql) throws SQLFeatureNotSupportedException, SqlParseException, InvocationTargetException, NoSuchMethodException, IllegalAccessException, IOException {
80+
@Test
81+
public void multiMatchQuery() throws IOException, SqlParseException, SQLFeatureNotSupportedException {
82+
String expectedOutput = Files.toString(new File("src/test/resources/expectedOutput/multi_match_query.json"), StandardCharsets.UTF_8).replaceAll("\r", "");
83+
String result = explain(String.format("SELECT * FROM %s WHERE q=multimatch(query='this is a test',fields='subject^3,message',analyzer='standard',type='best_fields',boost=1.0,slop=0,tie_breaker=0.3,operator='and')", TEST_INDEX_ACCOUNT));
84+
assertThat(result.replaceAll("\\s+", ""), equalTo(expectedOutput.replaceAll("\\s+", "")));
85+
}
86+
87+
private String explain(String sql) throws SQLFeatureNotSupportedException, SqlParseException {
8188
SearchDao searchDao = MainTestSuite.getSearchDao();
8289
SqlElasticRequestBuilder requestBuilder = searchDao.explain(sql).explain();
8390
return requestBuilder.explain();
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
{
2+
"from": 0,
3+
"size": 200,
4+
"query": {
5+
"bool": {
6+
"filter": [
7+
{
8+
"bool": {
9+
"must": [
10+
{
11+
"multi_match": {
12+
"query": "this is a test",
13+
"fields": [
14+
"message^1.0",
15+
"subject^3.0"
16+
],
17+
"type": "best_fields",
18+
"operator": "AND",
19+
"analyzer": "standard",
20+
"slop": 0,
21+
"prefix_length": 0,
22+
"max_expansions": 50,
23+
"tie_breaker": 0.3,
24+
"zero_terms_query": "NONE",
25+
"auto_generate_synonyms_phrase_query": true,
26+
"fuzzy_transpositions": true,
27+
"boost": 1.0
28+
}
29+
}
30+
],
31+
"adjust_pure_negative": true,
32+
"boost": 1.0
33+
}
34+
}
35+
],
36+
"adjust_pure_negative": true,
37+
"boost": 1.0
38+
}
39+
}
40+
}

0 commit comments

Comments
 (0)