From 54a433ee3681a2f48acee5c1aacf9a3058f85641 Mon Sep 17 00:00:00 2001 From: Martin Gaievski Date: Thu, 22 Dec 2022 16:43:34 -0800 Subject: [PATCH] Adding integ test for index close/open scenario (#693) * Adding integ test for index close/open scenario Signed-off-by: Martin Gaievski --- .../opensearch/knn/index/LuceneEngineIT.java | 32 +++++++++++++++++++ 1 file changed, 32 insertions(+) diff --git a/src/test/java/org/opensearch/knn/index/LuceneEngineIT.java b/src/test/java/org/opensearch/knn/index/LuceneEngineIT.java index d0e3bae20..14e45eaa9 100644 --- a/src/test/java/org/opensearch/knn/index/LuceneEngineIT.java +++ b/src/test/java/org/opensearch/knn/index/LuceneEngineIT.java @@ -8,6 +8,7 @@ import com.google.common.collect.ImmutableList; import com.google.common.collect.ImmutableMap; import com.google.common.primitives.Floats; +import org.apache.commons.lang.math.RandomUtils; import org.apache.hc.core5.http.io.entity.EntityUtils; import org.apache.lucene.index.VectorSimilarityFunction; import org.junit.After; @@ -338,6 +339,28 @@ public void testQuery_filterWithNonLuceneEngine() throws Exception { ); } + public void testIndexReopening() throws Exception { + createKnnIndexMappingWithLuceneEngine(DIMENSION, SpaceType.L2); + + for (int j = 0; j < TEST_INDEX_VECTORS.length; j++) { + addKnnDoc(INDEX_NAME, Integer.toString(j + 1), FIELD_NAME, TEST_INDEX_VECTORS[j]); + } + + final float[] searchVector = TEST_QUERY_VECTORS[0]; + final int k = 1 + RandomUtils.nextInt(TEST_INDEX_VECTORS.length); + + final List knnResultsBeforeIndexClosure = queryResults(searchVector, k); + + closeIndex(INDEX_NAME); + openIndex(INDEX_NAME); + + ensureGreen(INDEX_NAME); + + final List knnResultsAfterIndexClosure = queryResults(searchVector, k); + + assertArrayEquals(knnResultsBeforeIndexClosure.toArray(), knnResultsAfterIndexClosure.toArray()); + } + private void addKnnDocWithAttributes(String docId, float[] vector, Map fieldValues) throws IOException { Request request = new Request("POST", "/" + INDEX_NAME + "/_doc/" + docId + "?refresh=true"); @@ -406,4 +429,13 @@ private void validateQueries(SpaceType spaceType, String fieldName) throws Excep } } } + + private List queryResults(final float[] searchVector, final int k) throws Exception { + final String responseBody = EntityUtils.toString( + searchKNNIndex(INDEX_NAME, new KNNQueryBuilder(FIELD_NAME, searchVector, k), k).getEntity() + ); + final List knnResults = parseSearchResponse(responseBody, FIELD_NAME); + assertNotNull(knnResults); + return knnResults.stream().map(KNNResult::getVector).collect(Collectors.toUnmodifiableList()); + } }