Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion src/test/java/org/opensearch/knn/index/FaissIT.java
Original file line number Diff line number Diff line change
Expand Up @@ -306,8 +306,12 @@ public void testRadialSearchWithCosineAndFilter_ensureThresholdEnforced_thenSucc
null
);

final float tolerance = 0.0001f;
for (KNNResult result : results.get(0)) {
assertTrue(String.format("Score %.4f below threshold %.3f", result.getScore(), minScore), result.getScore() >= minScore);
assertTrue(
String.format("Score %.4f below threshold %.3f", result.getScore(), minScore),
result.getScore() >= (minScore - tolerance)
);
}

deleteKNNIndex(indexName);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,50 @@ protected void doTestNestedIndex(
doKnnSearchTest(spaceType, schema, IndexingType.SPARSE_NESTED, false);
}

/**
* Test with radial search and filtering both enabled.
*/
@SneakyThrows
protected void doTestNonNestedIndexWithRadialAndFilter(
final VectorDataType dataType,
final String methodParams,
final SpaceType spaceType,
final Consumer<Settings.Builder> additionalSettings
) {
final NonNestedNMappingSchema mapping = new NonNestedNMappingSchema().knnFieldName(KNN_FIELD_NAME)
.dimension(DIMENSIONS)
.dataType(dataType)
.mode(Mode.NOT_CONFIGURED)
.compressionLevel(CompressionLevel.NOT_CONFIGURED)
.methodParamString(methodParams)
.spaceType(spaceType)
.filterFieldName(FILTER_FIELD_NAME)
.idFieldName(ID_FIELD_NAME);

final String mappingStr = mapping.createString();
final Schema schema = new Schema(mappingStr, dataType, Mode.NOT_CONFIGURED, additionalSettings);

doKnnSearchTestWithRadialAndFilter(spaceType, schema, IndexingType.DENSE);
}

@SneakyThrows
private void doKnnSearchTestWithRadialAndFilter(final SpaceType spaceType, final Schema schema, final IndexingType indexingType) {
createKnnHnswIndex(schema.mapping, true, schema.additionalSettings);

final Documents documents = DocumentsGenerator.create(indexingType, schema.vectorDataType, NUM_DOCUMENTS).generate();
final List<String> docStrings = documents.getDocuments();
for (int i = 0; i < docStrings.size(); ++i) {
addKnnDoc(INDEX_NAME, Integer.toString(i), docStrings.get(i));
}

flushIndex(INDEX_NAME);
forceMergeKnnIndex(INDEX_NAME, 1);

doKnnSearchTestAllowRadialWithFilter(documents, schema, indexingType, spaceType);

deleteKNNIndex(INDEX_NAME);
}

@SneakyThrows
protected void doKnnSearchTest(
final SpaceType spaceType,
Expand Down Expand Up @@ -323,6 +367,31 @@ private void doKnnSearchTest(
checkOffheapNotLoaded();
}

@SneakyThrows
private void doKnnSearchTestAllowRadialWithFilter(
final Documents documents,
final Schema schema,
final IndexingType indexingType,
final SpaceType spaceType
) {
final float[] queryVector = SearchTestHelper.generateOneSingleFloatVector(
DIMENSIONS,
MIN_VECTOR_ELEMENT_VALUE,
MAX_VECTOR_ELEMENT_VALUE,
false
);
final float minSimil = documents.prepareAnswerSet(
queryVector,
spaceType.getKnnVectorSimilarityFunction(),
true, // doFiltering = true
true // isRadial = true
);
final List<Documents.Result> results = doQuery(queryVector, false, true, true, Optional.of(minSimil));

documents.validateResponse(results, indexingType, schema.mode());
checkOffheapNotLoaded();
}

@SneakyThrows
private void checkOffheapNotLoaded() {
log.info("Checking whether off-heap was not used.");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,4 +48,8 @@ public void testWhenNoIndexBuilt() {
doTestNonNestedIndex(VectorDataType.FLOAT, EMPTY_PARAMS, true, SpaceType.L2, NO_BUILD_HNSW);
doTestNonNestedIndex(VectorDataType.FLOAT, EMPTY_PARAMS, false, SpaceType.L2, NO_BUILD_HNSW);
}

public void testRadialSearchWithCosineAndFilter_thenThresholdEnforced() {
doTestNonNestedIndexWithRadialAndFilter(VectorDataType.FLOAT, EMPTY_PARAMS, SpaceType.COSINESIMIL, NO_ADDITIONAL_SETTINGS);
}
}
Loading