Skip to content

Commit

Permalink
Adding integ test for index close/open scenario (#693)
Browse files Browse the repository at this point in the history
* Adding integ test for index close/open scenario

Signed-off-by: Martin Gaievski <[email protected]>
  • Loading branch information
martin-gaievski authored Dec 23, 2022
1 parent fcd2d55 commit 54a433e
Showing 1 changed file with 32 additions and 0 deletions.
32 changes: 32 additions & 0 deletions src/test/java/org/opensearch/knn/index/LuceneEngineIT.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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<Float[]> knnResultsBeforeIndexClosure = queryResults(searchVector, k);

closeIndex(INDEX_NAME);
openIndex(INDEX_NAME);

ensureGreen(INDEX_NAME);

final List<Float[]> knnResultsAfterIndexClosure = queryResults(searchVector, k);

assertArrayEquals(knnResultsBeforeIndexClosure.toArray(), knnResultsAfterIndexClosure.toArray());
}

private void addKnnDocWithAttributes(String docId, float[] vector, Map<String, String> fieldValues) throws IOException {
Request request = new Request("POST", "/" + INDEX_NAME + "/_doc/" + docId + "?refresh=true");

Expand Down Expand Up @@ -406,4 +429,13 @@ private void validateQueries(SpaceType spaceType, String fieldName) throws Excep
}
}
}

private List<Float[]> 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<KNNResult> knnResults = parseSearchResponse(responseBody, FIELD_NAME);
assertNotNull(knnResults);
return knnResults.stream().map(KNNResult::getVector).collect(Collectors.toUnmodifiableList());
}
}

0 comments on commit 54a433e

Please sign in to comment.