Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Backport 2.x] Update knn index default to always do approximate search in test (#2332) #2350

Merged
merged 1 commit into from
Dec 23, 2024
Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -61,4 +61,13 @@ protected static final boolean isRunningAgainstOldCluster() {
protected final Optional<String> getBWCVersion() {
return Optional.ofNullable(System.getProperty(BWC_VERSION, null));
}

@Override
protected Settings getKNNDefaultIndexSettings() {
if (isApproximateThresholdSupported(getBWCVersion())) {
return super.getKNNDefaultIndexSettings();
}
// for bwc will return old default setting without approximate value threshold setting
return getDefaultIndexSettings();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -85,4 +85,13 @@ protected final Optional<String> getBWCVersion() {
return Optional.ofNullable(System.getProperty(BWC_VERSION, null));
}

@Override
protected Settings getKNNDefaultIndexSettings() {
if (isApproximateThresholdSupported(getBWCVersion())) {
return super.getKNNDefaultIndexSettings();
}
// for bwc will return old default setting without approximate value threshold setting
return getDefaultIndexSettings();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public void testKNNDefaultIndexSettings() throws Exception {
waitForClusterHealthGreen(NODES_BWC_CLUSTER);
switch (getClusterType()) {
case OLD:
createKnnIndex(testIndex, getKNNDefaultIndexSettings(), createKnnIndexMapping(TEST_FIELD, DIMENSIONS));
createKnnIndex(testIndex, getDefaultIndexSettings(), createKnnIndexMapping(TEST_FIELD, DIMENSIONS));
int docIdOld = 0;
addKNNDocs(testIndex, TEST_FIELD, DIMENSIONS, docIdOld, NUM_DOCS);
break;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ public void tearDown() throws Exception {
* Create a k-NN index with default settings
*/
protected IndexService createKNNIndex(String indexName) {
return createIndex(indexName, getKNNDefaultIndexSettings());
return createIndex(indexName, getKNNDefaultIndexSettingsBuildsGraphAlways());
}

/**
Expand Down Expand Up @@ -161,13 +161,6 @@ protected void createKnnNestedIndexMapping(String indexName, String fieldPath, I
OpenSearchAssertions.assertAcked(client().admin().indices().putMapping(request).actionGet());
}

/**
* Get default k-NN settings for test cases
*/
protected Settings getKNNDefaultIndexSettings() {
return Settings.builder().put("number_of_shards", 1).put("number_of_replicas", 0).put("index.knn", true).build();
}

/**
* Get default k-NN settings for test cases with build graph always
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,11 @@ public void testCreateIndexWithInvalidSpaceType() throws IOException {
}

public void testUpdateIndexSetting() throws IOException {
Settings settings = Settings.builder().put("index.knn", true).put(KNNSettings.KNN_ALGO_PARAM_EF_SEARCH, 512).build();
Settings settings = Settings.builder()
.put("index.knn", true)
.put(KNNSettings.KNN_ALGO_PARAM_EF_SEARCH, 512)
.put(KNNSettings.INDEX_KNN_ADVANCED_APPROXIMATE_THRESHOLD, 0)
.build();
createKnnIndex(INDEX_NAME, settings, createKnnIndexMapping(FIELD_NAME, 2));
assertEquals("512", getIndexSettingByName(INDEX_NAME, KNNSettings.KNN_ALGO_PARAM_EF_SEARCH));

Expand All @@ -122,7 +126,7 @@ public void testUpdateIndexSetting() throws IOException {

@SuppressWarnings("unchecked")
public void testCacheRebuiltAfterUpdateIndexSettings() throws Exception {
createKnnIndex(INDEX_NAME, buildKNNIndexSettings(ALWAYS_BUILD_GRAPH), createKnnIndexMapping(FIELD_NAME, 2));
createKnnIndex(INDEX_NAME, getKNNDefaultIndexSettings(), createKnnIndexMapping(FIELD_NAME, 2));

Float[] vector = { 6.0f, 6.0f };
addKnnDoc(INDEX_NAME, "1", FIELD_NAME, vector);
Expand Down
4 changes: 2 additions & 2 deletions src/test/java/org/opensearch/knn/index/OpenSearchIT.java
Original file line number Diff line number Diff line change
Expand Up @@ -640,7 +640,7 @@ public void testKNNIndex_whenBuildGraphThresholdIsPresent_thenGetThresholdValue(
public void testKNNIndex_whenBuildThresholdIsNotProvided_thenShouldNotReturnSetting() throws Exception {
final String knnIndexMapping = createKnnIndexMapping(FIELD_NAME, KNNEngine.getMaxDimensionByEngine(KNNEngine.DEFAULT));
final String indexName = "test-index-with-build-graph-settings";
createKnnIndex(indexName, knnIndexMapping);
createKnnIndex(indexName, getDefaultIndexSettings(), knnIndexMapping);
final String buildVectorDataStructureThresholdSetting = getIndexSettingByName(
indexName,
KNNSettings.INDEX_KNN_ADVANCED_APPROXIMATE_THRESHOLD
Expand All @@ -655,7 +655,7 @@ public void testKNNIndex_whenBuildThresholdIsNotProvided_thenShouldNotReturnSett
public void testKNNIndex_whenGetIndexSettingWithDefaultIsCalled_thenReturnDefaultBuildGraphThresholdValue() throws Exception {
final String knnIndexMapping = createKnnIndexMapping(FIELD_NAME, KNNEngine.getMaxDimensionByEngine(KNNEngine.DEFAULT));
final String indexName = "test-index-with-build-vector-graph-settings";
createKnnIndex(indexName, knnIndexMapping);
createKnnIndex(indexName, getDefaultIndexSettings(), knnIndexMapping);
final String buildVectorDataStructureThresholdSetting = getIndexSettingByName(
indexName,
KNNSettings.INDEX_KNN_ADVANCED_APPROXIMATE_THRESHOLD,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import org.opensearch.core.xcontent.XContentBuilder;
import org.opensearch.knn.KNNRestTestCase;
import org.opensearch.knn.NestedKnnDocBuilder;
import org.opensearch.knn.index.KNNSettings;
import org.opensearch.knn.index.VectorDataType;
import org.opensearch.knn.index.engine.KNNEngine;
import org.opensearch.knn.index.mapper.Mode;
Expand Down Expand Up @@ -329,6 +330,7 @@ private void createKnnIndex(
.put("number_of_shards", numOfShards)
.put("number_of_replicas", 0)
.put("index.knn", true)
.put(KNNSettings.INDEX_KNN_ADVANCED_APPROXIMATE_THRESHOLD, 0)
.build();
createKnnIndex(INDEX_NAME, settings, mapping);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import org.opensearch.knn.KNNRestTestCase;
import org.opensearch.knn.KNNResult;
import org.opensearch.knn.common.KNNConstants;
import org.opensearch.knn.index.KNNSettings;
import org.opensearch.knn.index.SpaceType;
import org.apache.http.util.EntityUtils;
import org.opensearch.client.Request;
Expand Down Expand Up @@ -831,7 +832,11 @@ private void createIndexAndAssertScriptScore(
/*
* Create knn index and populate data
*/
Settings settings = Settings.builder().put("number_of_shards", 1).put("number_of_replicas", 0).put("index.knn", enableKnn).build();
Settings.Builder builder = Settings.builder().put("number_of_shards", 1).put("number_of_replicas", 0).put("index.knn", enableKnn);
if (enableKnn) {
builder.put(KNNSettings.INDEX_KNN_ADVANCED_APPROXIMATE_THRESHOLD, 0);
}
Settings settings = builder.build();
createKnnIndex(INDEX_NAME, settings, mapper);
try {
final int numDocsWithField = randomIntBetween(4, 10);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import org.opensearch.common.settings.Settings;
import org.opensearch.knn.KNNRestTestCase;
import org.opensearch.knn.KNNResult;
import org.opensearch.knn.index.KNNSettings;
import org.opensearch.knn.index.engine.KNNMethodContext;
import org.opensearch.knn.index.engine.MethodComponentContext;
import org.opensearch.knn.index.SpaceType;
Expand Down Expand Up @@ -622,7 +623,11 @@ private Response buildIndexAndRunPainlessScript(
/*
* Create knn index and populate data
*/
Settings settings = Settings.builder().put("number_of_shards", 1).put("number_of_replicas", 0).put("index.knn", enableKnn).build();
Settings.Builder builder = Settings.builder().put("number_of_shards", 1).put("number_of_replicas", 0).put("index.knn", enableKnn);
if (enableKnn) {
builder.put(KNNSettings.INDEX_KNN_ADVANCED_APPROXIMATE_THRESHOLD, 0);
}
Settings settings = builder.build();
createKnnIndex(INDEX_NAME, settings, mapper);
try {
for (Map.Entry<String, Float[]> data : documents.entrySet()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@
import org.opensearch.index.query.QueryBuilder;
import org.opensearch.index.query.QueryBuilders;
import org.opensearch.knn.KNNRestTestCase;
import org.opensearch.knn.index.KNNSettings;
import org.opensearch.knn.index.query.KNNQueryBuilder;
import org.opensearch.knn.index.SpaceType;
import org.opensearch.knn.index.query.KNNQueryBuilder;
import org.opensearch.knn.plugin.stats.KNNStats;
Expand Down Expand Up @@ -292,7 +294,12 @@ public void testScriptStats_multipleShards() throws Exception {
// Create an index with a single vector
createKnnIndex(
INDEX_NAME,
Settings.builder().put("number_of_shards", 2).put("number_of_replicas", 0).put("index.knn", true).build(),
Settings.builder()
.put("number_of_shards", 2)
.put("number_of_replicas", 0)
.put("index.knn", true)
.put(KNNSettings.INDEX_KNN_ADVANCED_APPROXIMATE_THRESHOLD, 0)
.build(),
createKnnIndexMapping(FIELD_NAME, 2)
);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
package org.opensearch.knn.plugin.action;

import org.opensearch.knn.KNNRestTestCase;
import org.opensearch.knn.index.KNNSettings;
import org.opensearch.knn.index.SpaceType;
import org.opensearch.knn.index.query.KNNQueryBuilder;
import org.opensearch.knn.plugin.KNNPlugin;
Expand Down Expand Up @@ -266,7 +267,12 @@ public void testScriptStats_multipleShards() throws Exception {
// Create an index with a single vector
createKnnIndex(
INDEX_NAME,
Settings.builder().put("number_of_shards", 2).put("number_of_replicas", 0).put("index.knn", true).build(),
Settings.builder()
.put("number_of_shards", 2)
.put("number_of_replicas", 0)
.put("index.knn", true)
.put(KNNSettings.INDEX_KNN_ADVANCED_APPROXIMATE_THRESHOLD, 0)
.build(),
createKnnIndexMapping(FIELD_NAME, 2)
);

Expand Down
3 changes: 3 additions & 0 deletions src/test/java/org/opensearch/knn/recall/RecallTestsIT.java
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@
import static org.opensearch.knn.common.KNNConstants.PARAMETERS;
import static org.opensearch.knn.common.KNNConstants.TYPE;
import static org.opensearch.knn.common.KNNConstants.TYPE_KNN_VECTOR;
import static org.opensearch.knn.index.KNNSettings.INDEX_KNN_ADVANCED_APPROXIMATE_THRESHOLD;
import static org.opensearch.knn.index.KNNSettings.KNN_ALGO_PARAM_EF_SEARCH;
import static org.opensearch.knn.index.KNNSettings.KNN_ALGO_PARAM_INDEX_THREAD_QTY;
import static org.opensearch.knn.index.KNNSettings.KNN_MEMORY_CIRCUIT_BREAKER_ENABLED;
Expand Down Expand Up @@ -152,6 +153,7 @@ public void testRecall_whenNmslibHnswFP32_thenRecallAbove75percent() {
.put("number_of_shards", SHARD_COUNT)
.put("number_of_replicas", REPLICA_COUNT)
.put("index.knn", true)
.put(INDEX_KNN_ADVANCED_APPROXIMATE_THRESHOLD, 0)
.put(KNN_ALGO_PARAM_EF_SEARCH, HNSW_EF_SEARCH)
.build(),
builder.toString()
Expand Down Expand Up @@ -531,6 +533,7 @@ private Settings getSettings() {
.put("number_of_shards", SHARD_COUNT)
.put("number_of_replicas", REPLICA_COUNT)
.put("index.knn", true)
.put(INDEX_KNN_ADVANCED_APPROXIMATE_THRESHOLD, 0)
.build();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -797,7 +797,14 @@ protected void updateClusterSettings(String settingKey, Object value) throws Exc
* Return default index settings for index creation
*/
protected Settings getKNNDefaultIndexSettings() {
return Settings.builder().put("number_of_shards", 1).put("number_of_replicas", 0).put("index.knn", true).build();
return buildKNNIndexSettings(0);
}

/**
* Return default index settings for index creation
*/
protected Settings getDefaultIndexSettings() {
return Settings.builder().put("number_of_shards", 1).put("number_of_replicas", 0).put(KNN_INDEX, true).build();
}

protected Settings getKNNSegmentReplicatedIndexSettings() {
Expand Down
Loading