diff --git a/elastic-fed/modules/havenask-engine/src/main/java/org/havenask/engine/search/HavenaskSearchFetchProcessor.java b/elastic-fed/modules/havenask-engine/src/main/java/org/havenask/engine/search/HavenaskSearchFetchProcessor.java index 1c528420..58eb1eca 100644 --- a/elastic-fed/modules/havenask-engine/src/main/java/org/havenask/engine/search/HavenaskSearchFetchProcessor.java +++ b/elastic-fed/modules/havenask-engine/src/main/java/org/havenask/engine/search/HavenaskSearchFetchProcessor.java @@ -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()) { @@ -117,9 +117,9 @@ public SqlResponse havenaskFetchWithSql( return fetchPhaseSqlResponse; } - private static QrsSqlRequest getQrsFetchPhaseSqlRequest(List idList, String tableName, FetchSourceContext fetchSourceContext) { + public static QrsSqlRequest getQrsFetchPhaseSqlRequest(List 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) { diff --git a/elastic-fed/modules/havenask-engine/src/main/java/org/havenask/engine/search/HavenaskSearchQueryProcessor.java b/elastic-fed/modules/havenask-engine/src/main/java/org/havenask/engine/search/HavenaskSearchQueryProcessor.java index 069e2821..31c7e1ba 100644 --- a/elastic-fed/modules/havenask-engine/src/main/java/org/havenask/engine/search/HavenaskSearchQueryProcessor.java +++ b/elastic-fed/modules/havenask-engine/src/main/java/org/havenask/engine/search/HavenaskSearchQueryProcessor.java @@ -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); diff --git a/elastic-fed/modules/havenask-engine/src/test/java/org/havenask/engine/search/fetch/HavenaskSearchFetchProcessorTests.java b/elastic-fed/modules/havenask-engine/src/test/java/org/havenask/engine/search/fetch/HavenaskSearchFetchProcessorTests.java index 1fa9c6fb..ba999777 100644 --- a/elastic-fed/modules/havenask-engine/src/test/java/org/havenask/engine/search/fetch/HavenaskSearchFetchProcessorTests.java +++ b/elastic-fed/modules/havenask-engine/src/test/java/org/havenask/engine/search/fetch/HavenaskSearchFetchProcessorTests.java @@ -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; @@ -38,6 +39,13 @@ public class HavenaskSearchFetchProcessorTests extends HavenaskTestCase { private QrsClient qrsClient = mock(QrsClient.class); + public void testGetQrsFetchPhaseSqlRequest() throws IOException { + List 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\":\"\"," diff --git a/elastic-fed/modules/havenask-engine/src/test/java/org/havenask/engine/search/fetch/HavenaskSearchQueryProcessorTests.java b/elastic-fed/modules/havenask-engine/src/test/java/org/havenask/engine/search/fetch/HavenaskSearchQueryProcessorTests.java index c98c75e3..db010a36 100644 --- a/elastic-fed/modules/havenask-engine/src/test/java/org/havenask/engine/search/fetch/HavenaskSearchQueryProcessorTests.java +++ b/elastic-fed/modules/havenask-engine/src/test/java/org/havenask/engine/search/fetch/HavenaskSearchQueryProcessorTests.java @@ -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 { @@ -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 ); @@ -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 { @@ -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 { @@ -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 { @@ -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 @@ -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 ); @@ -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 ); @@ -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