use of org.opensearch.cluster.metadata.IndexMetadata in project k-NN by opensearch-project.
the class TrainingModelRequestTests method testValidation_invalid_trainingFieldNotKnnVector.
public void testValidation_invalid_trainingFieldNotKnnVector() {
// Check that validation produces exception when field is not knn_vector
// Setup the training request
String modelId = "test-model-id";
KNNMethodContext knnMethodContext = mock(KNNMethodContext.class);
when(knnMethodContext.validate()).thenReturn(null);
when(knnMethodContext.isTrainingRequired()).thenReturn(true);
int dimension = 10;
String trainingIndex = "test-training-index";
String trainingField = "test-training-field";
TrainingModelRequest trainingModelRequest = new TrainingModelRequest(modelId, knnMethodContext, dimension, trainingIndex, trainingField, null, null);
// Mock the model dao to return null so that no exception is produced
ModelDao modelDao = mock(ModelDao.class);
when(modelDao.getMetadata(modelId)).thenReturn(null);
// Return mapping with different type
Map<String, Object> mappingMap = ImmutableMap.of("properties", ImmutableMap.of(trainingField, ImmutableMap.of("type", "int", KNNConstants.DIMENSION, dimension)));
MappingMetadata mappingMetadata = mock(MappingMetadata.class);
when(mappingMetadata.getSourceAsMap()).thenReturn(mappingMap);
IndexMetadata indexMetadata = mock(IndexMetadata.class);
when(indexMetadata.mapping()).thenReturn(mappingMetadata);
Metadata metadata = mock(Metadata.class);
when(metadata.index(trainingIndex)).thenReturn(indexMetadata);
ClusterState clusterState = mock(ClusterState.class);
when(clusterState.metadata()).thenReturn(metadata);
ClusterService clusterService = mock(ClusterService.class);
when(clusterService.state()).thenReturn(clusterState);
// Initialize static components with the mocks
TrainingModelRequest.initialize(modelDao, clusterService);
// Test that validation produces model already exists error message
ActionRequestValidationException exception = trainingModelRequest.validate();
assertNotNull(exception);
List<String> validationErrors = exception.validationErrors();
assertEquals(1, validationErrors.size());
assertTrue(validationErrors.get(0).contains("not of type"));
}
use of org.opensearch.cluster.metadata.IndexMetadata in project k-NN by opensearch-project.
the class TrainingModelRequestTests method getClusterServiceForValidReturns.
/**
* This method produces a cluster service that will mock so that there are no validation exceptions.
*
* @param trainingIndex Name of training index
* @param trainingField Name of training field
* @param dimension Expected dimension
* @return ClusterService
*/
private ClusterService getClusterServiceForValidReturns(String trainingIndex, String trainingField, int dimension) {
Map<String, Object> mappingMap = ImmutableMap.of("properties", ImmutableMap.of(trainingField, ImmutableMap.of("type", KNNVectorFieldMapper.CONTENT_TYPE, KNNConstants.DIMENSION, dimension)));
MappingMetadata mappingMetadata = mock(MappingMetadata.class);
when(mappingMetadata.getSourceAsMap()).thenReturn(mappingMap);
IndexMetadata indexMetadata = mock(IndexMetadata.class);
when(indexMetadata.mapping()).thenReturn(mappingMetadata);
Metadata metadata = mock(Metadata.class);
when(metadata.index(trainingIndex)).thenReturn(indexMetadata);
ClusterState clusterState = mock(ClusterState.class);
when(clusterState.metadata()).thenReturn(metadata);
ClusterService clusterService = mock(ClusterService.class);
when(clusterService.state()).thenReturn(clusterState);
return clusterService;
}
use of org.opensearch.cluster.metadata.IndexMetadata in project anomaly-detection by opensearch-project.
the class UpdateMappingTests method testUpdateMapping.
@SuppressWarnings({ "serial", "unchecked" })
public void testUpdateMapping() throws IOException {
doAnswer(invocation -> {
ActionListener<GetAliasesResponse> listener = (ActionListener<GetAliasesResponse>) invocation.getArgument(1);
ImmutableOpenMap.Builder<String, List<AliasMetadata>> builder = ImmutableOpenMap.builder();
List<AliasMetadata> aliasMetadata = new ArrayList<>();
aliasMetadata.add(AliasMetadata.builder(ADIndex.RESULT.name()).build());
builder.put(resultIndexName, aliasMetadata);
listener.onResponse(new GetAliasesResponse(builder.build()));
return null;
}).when(indicesAdminClient).getAliases(any(GetAliasesRequest.class), any());
IndexMetadata indexMetadata = IndexMetadata.builder(resultIndexName).putAlias(AliasMetadata.builder(ADIndex.RESULT.getIndexName())).settings(settings(Version.CURRENT)).numberOfShards(1).numberOfReplicas(0).putMapping(new MappingMetadata("type", new HashMap<String, Object>() {
{
put(AnomalyDetectionIndices.META, new HashMap<String, Object>() {
{
// version 1 will cause update
put(CommonName.SCHEMA_VERSION_FIELD, 1);
}
});
}
})).build();
ImmutableOpenMap.Builder<String, IndexMetadata> openMapBuilder = ImmutableOpenMap.builder();
openMapBuilder.put(resultIndexName, indexMetadata);
Metadata metadata = Metadata.builder().indices(openMapBuilder.build()).build();
when(clusterState.getMetadata()).thenReturn(metadata);
when(clusterState.metadata()).thenReturn(metadata);
adIndices.update();
verify(indicesAdminClient, times(1)).putMapping(any(), any());
}
use of org.opensearch.cluster.metadata.IndexMetadata in project anomaly-detection by opensearch-project.
the class TestHelpers method createIndexBlockedState.
public static ClusterState createIndexBlockedState(String indexName, Settings hackedSettings, String alias) {
ClusterState blockedClusterState = null;
IndexMetadata.Builder builder = IndexMetadata.builder(indexName);
if (alias != null) {
builder.putAlias(AliasMetadata.builder(alias));
}
IndexMetadata indexMetaData = builder.settings(Settings.builder().put(IndexMetadata.SETTING_INDEX_UUID, UUIDs.randomBase64UUID()).put(IndexMetadata.SETTING_NUMBER_OF_SHARDS, 1).put(IndexMetadata.SETTING_NUMBER_OF_REPLICAS, 0).put(IndexMetadata.SETTING_VERSION_CREATED, Version.CURRENT).put(hackedSettings)).build();
Metadata metaData = Metadata.builder().put(indexMetaData, false).build();
blockedClusterState = ClusterState.builder(new ClusterName("test cluster")).metadata(metaData).blocks(ClusterBlocks.builder().addBlocks(indexMetaData)).build();
return blockedClusterState;
}
use of org.opensearch.cluster.metadata.IndexMetadata in project anomaly-detection by opensearch-project.
the class CustomIndexTests method testInCorrectMapping.
public void testInCorrectMapping() throws IOException {
Map<String, Object> mappings = new HashMap<>();
Map<String, Object> past_mapping = new HashMap<>();
past_mapping.put("type", "nested");
Map<String, Object> past_nested_mapping = new HashMap<>();
past_mapping.put(CommonName.PROPERTIES, past_nested_mapping);
past_nested_mapping.put("data", Collections.singletonMap("type", "double"));
past_nested_mapping.put("feature_id", Collections.singletonMap("type", "keyword"));
mappings.put(AnomalyResult.PAST_VALUES_FIELD, past_mapping);
Map<String, Object> attribution_mapping = new HashMap<>();
past_mapping.put("type", "nested");
Map<String, Object> attribution_nested_mapping = new HashMap<>();
attribution_mapping.put(CommonName.PROPERTIES, attribution_nested_mapping);
attribution_nested_mapping.put("data", Collections.singletonMap("type", "double"));
attribution_nested_mapping.put("feature_id", Collections.singletonMap("type", "keyword"));
mappings.put(AnomalyResult.RELEVANT_ATTRIBUTION_FIELD, attribution_mapping);
IndexMetadata indexMetadata1 = new IndexMetadata.Builder(customIndexName).settings(Settings.builder().put(IndexMetadata.SETTING_VERSION_CREATED, Version.CURRENT).put(IndexMetadata.SETTING_NUMBER_OF_SHARDS, 1).put(IndexMetadata.SETTING_NUMBER_OF_REPLICAS, 0)).putMapping(new MappingMetadata("type1", Collections.singletonMap(CommonName.PROPERTIES, mappings))).build();
when(clusterService.state()).thenReturn(ClusterState.builder(clusterName).metadata(Metadata.builder().put(indexMetadata1, true).build()).build());
assertTrue(!adIndices.isValidResultIndexMapping(customIndexName));
}
Aggregations