-
Notifications
You must be signed in to change notification settings - Fork 132
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
pass filterbitset as null and add integ tests.
Signed-off-by: Wei Wang <[email protected]>
- Loading branch information
1 parent
105c39a
commit 50c96f3
Showing
3 changed files
with
99 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
82 changes: 82 additions & 0 deletions
82
src/test/java/org/opensearch/knn/integ/FilteredSearchANNSearchIT.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
/* | ||
* Copyright OpenSearch Contributors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package org.opensearch.knn.integ; | ||
|
||
import com.google.common.collect.ImmutableMap; | ||
import lombok.SneakyThrows; | ||
import lombok.extern.log4j.Log4j2; | ||
import org.apache.hc.core5.http.io.entity.EntityUtils; | ||
import org.opensearch.client.Response; | ||
import org.opensearch.common.settings.Settings; | ||
import org.opensearch.knn.KNNJsonIndexMappingsBuilder; | ||
import org.opensearch.knn.KNNJsonQueryBuilder; | ||
import org.opensearch.knn.KNNRestTestCase; | ||
import org.opensearch.knn.index.KNNSettings; | ||
import org.opensearch.knn.index.engine.KNNEngine; | ||
import java.util.List; | ||
|
||
import static org.opensearch.knn.common.KNNConstants.METHOD_HNSW; | ||
|
||
@Log4j2 | ||
public class FilteredSearchANNSearchIT extends KNNRestTestCase { | ||
@SneakyThrows | ||
public void testFilteredSearchWithFaissHnsw_whenFiltersMatchAllDocs_thenReturnCorrectResults() { | ||
String filterFieldName = "color"; | ||
final int expectResultSize = randomIntBetween(1,3); | ||
final String filterValue = "red"; | ||
createKnnIndex(INDEX_NAME, FIELD_NAME, 3, KNNEngine.FAISS); | ||
|
||
// ingest 4 vector docs into the index with the same field {"color": "red"} | ||
for (int i = 0; i < 4; i++) { | ||
addKnnDocWithAttributes( | ||
String.valueOf(i), | ||
new float[] { i, i, i }, | ||
ImmutableMap.of(filterFieldName, filterValue) | ||
); | ||
} | ||
|
||
refreshIndex(INDEX_NAME); | ||
forceMergeKnnIndex(INDEX_NAME); | ||
|
||
updateIndexSettings( | ||
INDEX_NAME, | ||
Settings.builder().put(KNNSettings.ADVANCED_FILTERED_EXACT_SEARCH_THRESHOLD, 0) | ||
); | ||
|
||
Float[] queryVector = { 3f, 3f, 3f }; | ||
// All docs in one segment will match the filters value | ||
String query = KNNJsonQueryBuilder.builder() | ||
.fieldName(FIELD_NAME) | ||
.vector(queryVector) | ||
.k(expectResultSize) | ||
.filterFieldName(filterFieldName) | ||
.filterValue(filterValue) | ||
.build() | ||
.getQueryString(); | ||
Response response = searchKNNIndex(INDEX_NAME, query, expectResultSize); | ||
String entity = EntityUtils.toString(response.getEntity()); | ||
List<String> docIds = parseIds(entity); | ||
assertEquals(expectResultSize, docIds.size()); | ||
assertEquals(expectResultSize, parseTotalSearchHits(entity)); | ||
} | ||
|
||
private void createKnnIndex(final String indexName, final String fieldName, final int dimension, final KNNEngine knnEngine) | ||
throws Exception { | ||
KNNJsonIndexMappingsBuilder.Method method = KNNJsonIndexMappingsBuilder.Method.builder() | ||
.methodName(METHOD_HNSW) | ||
.engine(knnEngine.getName()) | ||
.build(); | ||
|
||
String knnIndexMapping = KNNJsonIndexMappingsBuilder.builder() | ||
.fieldName(fieldName) | ||
.dimension(dimension) | ||
.method(method) | ||
.build() | ||
.getIndexMapping(); | ||
|
||
createKnnIndex(indexName, knnIndexMapping); | ||
} | ||
} |