Skip to content

Commit

Permalink
Update custom mappings
Browse files Browse the repository at this point in the history
Signed-off-by: Vijayan Balasubramanian <[email protected]>
  • Loading branch information
VijayanB committed Dec 16, 2024
1 parent 265ce5e commit 691810a
Show file tree
Hide file tree
Showing 8 changed files with 38 additions and 14 deletions.
9 changes: 1 addition & 8 deletions src/test/java/org/opensearch/knn/KNNSingleNodeTestCase.java
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(0), 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
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,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 @@ -323,6 +324,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.hc.core5.http.io.entity.EntityUtils;
import org.opensearch.client.Request;
Expand Down Expand Up @@ -840,7 +841,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 @@ -24,6 +24,7 @@
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.plugin.stats.KNNStats;
Expand Down Expand Up @@ -292,7 +293,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();
}
}

0 comments on commit 691810a

Please sign in to comment.