1010import com .carrotsearch .randomizedtesting .annotations .ThreadLeakFilters ;
1111
1212import org .elasticsearch .client .Request ;
13+ import org .elasticsearch .client .ResponseException ;
1314import org .elasticsearch .test .TestClustersThreadFilter ;
1415import org .elasticsearch .test .cluster .ElasticsearchCluster ;
1516import org .elasticsearch .test .rest .ESRestTestCase ;
2829import java .util .List ;
2930import java .util .Map ;
3031
32+ import static org .elasticsearch .rest .RestStatus .BAD_REQUEST ;
3133import static org .elasticsearch .xpack .esql .qa .rest .RestEsqlTestCase .requestObjectBuilder ;
3234import static org .elasticsearch .xpack .esql .qa .rest .RestEsqlTestCase .runEsqlSync ;
3335import static org .hamcrest .Matchers .is ;
36+ import static org .hamcrest .core .StringContains .containsString ;
3437
3538@ ThreadLeakFilters (filters = TestClustersThreadFilter .class )
3639public class KnnSemanticTextIT extends ESRestTestCase {
@@ -51,10 +54,11 @@ protected String getTestRestCluster() {
5154
5255 @ Before
5356 public void checkCapability () {
54- assumeTrue ("semantic text capability not available" , EsqlCapabilities .Cap .KNN_FUNCTION_V4 .isEnabled ());
57+ assumeTrue ("knn with semantic text not available" , EsqlCapabilities .Cap .KNN_FUNCTION_V4 .isEnabled ());
5558 }
5659
57- public void testKnnQuery () throws IOException {
60+ @ SuppressWarnings ("unchecked" )
61+ public void testKnnQueryWithSemanticText () throws IOException {
5862 String knnQuery = """
5963 FROM semantic-test METADATA _score
6064 | WHERE knn(semantic, [0, 1, 2], 10)
@@ -64,9 +68,29 @@ public void testKnnQuery() throws IOException {
6468 """ ;
6569
6670 Map <String , Object > response = runEsqlQuery (knnQuery );
67- @ SuppressWarnings ("unchecked" )
6871 List <Map <String , Object >> columns = (List <Map <String , Object >>) response .get ("columns" );
6972 assertThat (columns .size (), is (3 ));
73+ List <List <Object >> rows = (List <List <Object >>) response .get ("values" );
74+ assertThat (rows .size (), is (3 ));
75+ for (int row = 0 ; row < rows .size (); row ++) {
76+ List <Object > rowData = rows .get (row );
77+ Integer id = (Integer ) rowData .get (0 );
78+ assertThat (id , is (3 - row ));
79+ }
80+ }
81+
82+ public void testKnnQueryOnTextField () throws IOException {
83+ String knnQuery = """
84+ FROM semantic-test METADATA _score
85+ | WHERE knn(text, [0, 1, 2], 10)
86+ | KEEP id, _score, semantic
87+ | SORT _score DESC
88+ | LIMIT 10
89+ """ ;
90+
91+ ResponseException re = expectThrows (ResponseException .class , () -> runEsqlQuery (knnQuery ));
92+ assertThat (re .getResponse ().getStatusLine ().getStatusCode (), is (BAD_REQUEST .getStatus ()));
93+ assertThat (re .getMessage (), containsString ("[knn] queries are only supported on [dense_vector] fields" ));
7094 }
7195
7296 @ Before
@@ -82,6 +106,10 @@ public void setupIndex() throws IOException {
82106 "semantic": {
83107 "type": "semantic_text",
84108 "inference_id": "test_dense_inference"
109+ },
110+ "text": {
111+ "type": "text",
112+ "copy_to": "semantic"
85113 }
86114 }
87115 },
@@ -99,11 +127,11 @@ public void setupIndex() throws IOException {
99127 // 4 documents with a null in the middle, leading to 3 ESQL pages and 3 Arrow batches
100128 request .setJsonEntity ("""
101129 {"index": {"_id": "1"}}
102- {"id": 1, "semantic ": "sample text one "}
130+ {"id": 1, "text ": "sample text"}
103131 {"index": {"_id": "2"}}
104- {"id": 2, "semantic ": "sample text two "}
132+ {"id": 2, "text ": "another sample text"}
105133 {"index": {"_id": "3"}}
106- {"id": 3, "semantic ": "sample text three "}
134+ {"id": 3, "text ": "yet another sample text"}
107135 """ );
108136 assertEquals (200 , client ().performRequest (request ).getStatusLine ().getStatusCode ());
109137 }
0 commit comments