Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

修复索引名中含有中划线时,sql会报错的问题 #382

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ public SqlResponse havenaskFetchWithSql(
FetchSourceContext fetchSourceContext,
QrsClient qrsClient
) throws IOException {
QrsSqlRequest qrsFetchPhaseSqlRequest = getQrsFetchPhaseSqlRequest(idList, tableName, fetchSourceContext);
QrsSqlRequest qrsFetchPhaseSqlRequest = getQrsFetchPhaseSqlRequest(idList, tableName);
QrsSqlResponse qrsFetchPhaseSqlResponse = qrsClient.executeSql(qrsFetchPhaseSqlRequest);
SqlResponse fetchPhaseSqlResponse = SqlResponse.parse(qrsFetchPhaseSqlResponse.getResult());
if (logger.isDebugEnabled()) {
Expand All @@ -117,9 +117,9 @@ public SqlResponse havenaskFetchWithSql(
return fetchPhaseSqlResponse;
}

private static QrsSqlRequest getQrsFetchPhaseSqlRequest(List<String> idList, String tableName, FetchSourceContext fetchSourceContext) {
public static QrsSqlRequest getQrsFetchPhaseSqlRequest(List<String> idList, String tableName) {
StringBuilder sqlQuery = new StringBuilder();
sqlQuery.append("select _id, _source from ").append(tableName).append("_summary_ where _id in(");
sqlQuery.append("select _id, _source from ").append('`').append(tableName).append("_summary_` where _id in(");
for (int i = 0; i < idList.size(); i++) {
sqlQuery.append("'").append(idList.get(i)).append("'");
if (i < idList.size() - 1) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ public String transferSearchRequest2HavenaskSql(String table, SearchSourceBuilde
throw new IOException("unsupported DSL: " + dsl);
}
}
sqlQuery.append("select").append(selectParams).append(" from ").append(table);
sqlQuery.append("select").append(selectParams).append(" from ").append('`').append(table).append('`');
sqlQuery.append(where).append(orderBy);

sqlQuery.append(" limit ").append(size).append(" offset ").append(from);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import org.havenask.client.ha.SqlResponse;
import org.havenask.common.lucene.search.TopDocsAndMaxScore;
import org.havenask.engine.rpc.QrsClient;
import org.havenask.engine.rpc.QrsSqlRequest;
import org.havenask.engine.search.fetch.HavenaskFetchSubPhase.HitContent;
import org.havenask.engine.search.HavenaskSearchFetchProcessor;
import org.havenask.search.SearchHit;
Expand All @@ -38,6 +39,13 @@
public class HavenaskSearchFetchProcessorTests extends HavenaskTestCase {
private QrsClient qrsClient = mock(QrsClient.class);

public void testGetQrsFetchPhaseSqlRequest() throws IOException {
List<String> idList = List.of("1", "2", "3");
String tableName = "table";
QrsSqlRequest sqlRequest = HavenaskSearchFetchProcessor.getQrsFetchPhaseSqlRequest(idList, tableName);
assertEquals(sqlRequest.getSql(), "select _id, _source from `table_summary_` where _id in('1','2','3') limit 3");
}

public void testBuildQuerySearchResult() throws IOException {
String sqlResponseStr = "{\"total_time\":8.126,\"has_soft_failure\":false,\"covered_percent\":1.0,"
+ "\"row_count\":4,\"format_type\":\"full_json\",\"search_info\":{},\"rpc_info\":\"\","
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ public void testMatchAllDocsQuery() throws IOException {

HavenaskSearchQueryProcessor havenaskSearchQueryProcessor = new HavenaskSearchQueryProcessor(qrsClient);
String sql = havenaskSearchQueryProcessor.transferSearchRequest2HavenaskSql("table", builder, null);
assertEquals("select _id from table limit 10 offset 0", sql);
assertEquals("select _id from `table` limit 10 offset 0", sql);
}

public void testProximaQuery() throws IOException {
Expand All @@ -65,7 +65,7 @@ public void testProximaQuery() throws IOException {
HavenaskSearchQueryProcessor havenaskSearchQueryProcessor = new HavenaskSearchQueryProcessor(qrsClient);
String sql = havenaskSearchQueryProcessor.transferSearchRequest2HavenaskSql("table", builder, indexMapping);
assertEquals(
"select _id, (1/(1+vector_score('field'))) as _score from table where "
"select _id, (1/(1+vector_score('field'))) as _score from `table` where "
+ "MATCHINDEX('field', '1.0,2.0&n=20') order by _score desc limit 10 offset 0",
sql
);
Expand All @@ -88,7 +88,7 @@ public void testMatchQuery() throws IOException {
builder.query(QueryBuilders.matchQuery("field", "value"));
HavenaskSearchQueryProcessor havenaskSearchQueryProcessor = new HavenaskSearchQueryProcessor(qrsClient);
String sql = havenaskSearchQueryProcessor.transferSearchRequest2HavenaskSql("table", builder, null);
assertEquals("select _id from table where MATCHINDEX('field', 'value') limit 10 offset 0", sql);
assertEquals("select _id from `table` where MATCHINDEX('field', 'value') limit 10 offset 0", sql);
}

public void testLimit() throws IOException {
Expand All @@ -98,7 +98,7 @@ public void testLimit() throws IOException {
builder.size(10);
HavenaskSearchQueryProcessor havenaskSearchQueryProcessor = new HavenaskSearchQueryProcessor(qrsClient);
String sql = havenaskSearchQueryProcessor.transferSearchRequest2HavenaskSql("table", builder, null);
assertEquals("select _id from table limit 10 offset 10", sql);
assertEquals("select _id from `table` limit 10 offset 10", sql);
}

public void testNoFrom() throws IOException {
Expand All @@ -107,7 +107,7 @@ public void testNoFrom() throws IOException {
builder.size(10);
HavenaskSearchQueryProcessor havenaskSearchQueryProcessor = new HavenaskSearchQueryProcessor(qrsClient);
String sql = havenaskSearchQueryProcessor.transferSearchRequest2HavenaskSql("table", builder, null);
assertEquals(sql, "select _id from table limit 10 offset 0");
assertEquals(sql, "select _id from `table` limit 10 offset 0");
}

public void testNoSize() throws IOException {
Expand All @@ -116,7 +116,7 @@ public void testNoSize() throws IOException {
builder.from(10);
HavenaskSearchQueryProcessor havenaskSearchQueryProcessor = new HavenaskSearchQueryProcessor(qrsClient);
String sql = havenaskSearchQueryProcessor.transferSearchRequest2HavenaskSql("table", builder, null);
assertEquals(sql, "select _id from table limit 10 offset 10");
assertEquals(sql, "select _id from `table` limit 10 offset 10");
}

// test knn dsl
Expand All @@ -127,7 +127,7 @@ public void testKnnDsl() throws IOException {
HavenaskSearchQueryProcessor havenaskSearchQueryProcessor = new HavenaskSearchQueryProcessor(qrsClient);
String l2NormSql = havenaskSearchQueryProcessor.transferSearchRequest2HavenaskSql("table", l2NormBuilder, indexMapping);
assertEquals(
"select _id, ((1/(1+vector_score('field1')))) as _score from table "
"select _id, ((1/(1+vector_score('field1')))) as _score from `table` "
+ "where MATCHINDEX('field1', '1.0,2.0&n=20') order by _score desc limit 10 offset 0",
l2NormSql
);
Expand All @@ -137,7 +137,7 @@ public void testKnnDsl() throws IOException {
dotProductBuilder.knnSearch(List.of(new KnnSearchBuilder("field2", new float[] { 0.6f, 0.8f }, 20, 20, null)));
String dotProductSql = havenaskSearchQueryProcessor.transferSearchRequest2HavenaskSql("table", dotProductBuilder, indexMapping);
assertEquals(
"select _id, (((1+vector_score('field2'))/2)) as _score from table "
"select _id, (((1+vector_score('field2'))/2)) as _score from `table` "
+ "where MATCHINDEX('field2', '0.6,0.8&n=20') order by _score desc limit 10 offset 0",
dotProductSql
);
Expand All @@ -156,7 +156,7 @@ public void testMultiKnnDsl() throws IOException {
HavenaskSearchQueryProcessor havenaskSearchQueryProcessor = new HavenaskSearchQueryProcessor(qrsClient);
String sql = havenaskSearchQueryProcessor.transferSearchRequest2HavenaskSql("table", builder, indexMapping);
assertEquals(
"select _id, ((1/(1+vector_score('field1'))) + ((1+vector_score('field2'))/2)) as _score from table "
"select _id, ((1/(1+vector_score('field1'))) + ((1+vector_score('field2'))/2)) as _score from `table` "
+ "where MATCHINDEX('field1', '1.0,2.0&n=20') or MATCHINDEX('field2', '0.6,0.8&n=10') "
+ "order by _score desc limit 10 offset 0",
sql
Expand Down