Skip to content

Commit bc8d9ad

Browse files
authored
Update VectorNestedIT to include more kNN search scenarios (#131776)
1 parent 5917aaa commit bc8d9ad

File tree

1 file changed

+88
-0
lines changed

1 file changed

+88
-0
lines changed

server/src/internalClusterTest/java/org/elasticsearch/search/nested/VectorNestedIT.java

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,11 @@
1717
import org.elasticsearch.test.ESIntegTestCase;
1818

1919
import java.util.List;
20+
import java.util.Map;
2021

2122
import static org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertAcked;
23+
import static org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertHitCount;
24+
import static org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertNoFailuresAndResponse;
2225
import static org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertResponse;
2326
import static org.elasticsearch.xcontent.XContentFactory.jsonBuilder;
2427
import static org.hamcrest.Matchers.equalTo;
@@ -75,4 +78,89 @@ public void testSimpleNested() throws Exception {
7578
);
7679
}
7780

81+
public void testNestedKNnnSearch() {
82+
testNestedWithTwoSegments(false);
83+
}
84+
85+
public void testNestedKNnnSearchWithMultipleSegments() {
86+
testNestedWithTwoSegments(true);
87+
}
88+
89+
private void testNestedWithTwoSegments(boolean flush) {
90+
assertAcked(prepareCreate("test").setMapping("""
91+
{
92+
"properties": {
93+
"name": {
94+
"type": "keyword"
95+
},
96+
"nested": {
97+
"type": "nested",
98+
"properties": {
99+
"paragraph_id": {
100+
"type": "keyword"
101+
},
102+
"vector": {
103+
"type": "dense_vector",
104+
"dims": 5,
105+
"similarity": "l2_norm",
106+
"index_options": {
107+
"type": "hnsw"
108+
}
109+
}
110+
}
111+
}
112+
}
113+
}
114+
""").setSettings(Settings.builder().put(indexSettings()).put("index.number_of_shards", 1)));
115+
ensureGreen();
116+
117+
prepareIndex("test").setId("1")
118+
.setSource(
119+
"name",
120+
"dog",
121+
"nested",
122+
new Object[] {
123+
Map.of("paragraph_id", 0, "vector", new float[] { 230.0f, 300.33f, -34.8988f, 15.555f, -200.0f }),
124+
Map.of("paragraph_id", 1, "vector", new float[] { 240.0f, 300f, -3f, 1f, -20f }) }
125+
)
126+
.get();
127+
128+
prepareIndex("test").setId("2")
129+
.setSource(
130+
"name",
131+
"cat",
132+
"nested",
133+
new Object[] {
134+
Map.of("paragraph_id", 0, "vector", new float[] { -0.5f, 100.0f, -13f, 14.8f, -156.0f }),
135+
Map.of("paragraph_id", 1, "vector", new float[] { 0f, 100.0f, 0f, 14.8f, -156.0f }),
136+
Map.of("paragraph_id", 2, "vector", new float[] { 0f, 1.0f, 0f, 1.8f, -15.0f }) }
137+
)
138+
.get();
139+
140+
if (flush) {
141+
refresh("test");
142+
}
143+
144+
prepareIndex("test").setId("3")
145+
.setSource(
146+
"name",
147+
"rat",
148+
"nested",
149+
new Object[] { Map.of("paragraph_id", 0, "vector", new float[] { 0.5f, 111.3f, -13.0f, 14.8f, -156.0f }) }
150+
)
151+
.get();
152+
153+
waitForRelocation(ClusterHealthStatus.GREEN);
154+
refresh();
155+
156+
var knn = new KnnSearchBuilder("nested.vector", new float[] { -0.5f, 90.0f, -10f, 14.8f, -156.0f }, 2, 3, null, null);
157+
var request = prepareSearch("test").addFetchField("name").setKnnSearch(List.of(knn));
158+
assertNoFailuresAndResponse(request, response -> {
159+
assertHitCount(response, 2);
160+
assertEquals("2", response.getHits().getHits()[0].getId());
161+
assertEquals("cat", response.getHits().getHits()[0].field("name").getValue());
162+
assertEquals("3", response.getHits().getHits()[1].getId());
163+
assertEquals("rat", response.getHits().getHits()[1].field("name").getValue());
164+
});
165+
}
78166
}

0 commit comments

Comments
 (0)