use of org.opensearch.common.xcontent.NamedXContentRegistry in project anomaly-detection by opensearch-project.
the class TestHelpers method randomFeatureQuery.
public static SearchSourceBuilder randomFeatureQuery() throws IOException {
String query = "{\"query\":{\"match\":{\"user\":{\"query\":\"kimchy\",\"operator\":\"OR\",\"prefix_length\":0," + "\"max_expansions\":50,\"fuzzy_transpositions\":true,\"lenient\":false,\"zero_terms_query\":\"NONE\"," + "\"auto_generate_synonyms_phrase_query\":true,\"boost\":1}}}}";
SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
SearchModule searchModule = new SearchModule(Settings.EMPTY, false, Collections.emptyList());
XContentParser parser = XContentType.JSON.xContent().createParser(new NamedXContentRegistry(searchModule.getNamedXContents()), LoggingDeprecationHandler.INSTANCE, query);
searchSourceBuilder.parseXContent(parser);
return searchSourceBuilder;
}
use of org.opensearch.common.xcontent.NamedXContentRegistry in project anomaly-detection by opensearch-project.
the class ParseUtils method getDetector.
/**
* If filterByEnabled is true, get detector and check if the user has permissions to access the detector,
* then execute function; otherwise, get detector and execute function
* @param requestUser user from request
* @param detectorId detector id
* @param listener action listener
* @param function consumer function
* @param client client
* @param clusterService cluster service
* @param xContentRegistry XContent registry
* @param filterByBackendRole filter by backend role or not
*/
public static void getDetector(User requestUser, String detectorId, ActionListener listener, Consumer<AnomalyDetector> function, Client client, ClusterService clusterService, NamedXContentRegistry xContentRegistry, boolean filterByBackendRole) {
if (clusterService.state().metadata().indices().containsKey(AnomalyDetector.ANOMALY_DETECTORS_INDEX)) {
GetRequest request = new GetRequest(AnomalyDetector.ANOMALY_DETECTORS_INDEX).id(detectorId);
client.get(request, ActionListener.wrap(response -> onGetAdResponse(response, requestUser, detectorId, listener, function, xContentRegistry, filterByBackendRole), exception -> {
logger.error("Failed to get anomaly detector: " + detectorId, exception);
listener.onFailure(exception);
}));
} else {
listener.onFailure(new IndexNotFoundException(AnomalyDetector.ANOMALY_DETECTORS_INDEX));
}
}
use of org.opensearch.common.xcontent.NamedXContentRegistry in project OpenSearch by opensearch-project.
the class MetadataIndexTemplateServiceTests method testDefinedTimestampMappingIsAddedForDataStreamTemplates.
public void testDefinedTimestampMappingIsAddedForDataStreamTemplates() throws Exception {
final MetadataIndexTemplateService service = getMetadataIndexTemplateService();
ClusterState state = ClusterState.EMPTY_STATE;
ComponentTemplate ct1 = new ComponentTemplate(new Template(null, new CompressedXContent("{\n" + " \"properties\": {\n" + " \"field1\": {\n" + " \"type\": \"keyword\"\n" + " }\n" + " }\n" + " }"), null), null, null);
state = service.addComponentTemplate(state, true, "ct1", ct1);
{
ComposableIndexTemplate it = new ComposableIndexTemplate(org.opensearch.common.collect.List.of("logs*"), new Template(null, new CompressedXContent("{\n" + " \"properties\": {\n" + " \"field2\": {\n" + " \"type\": \"integer\"\n" + " }\n" + " }\n" + " }"), null), org.opensearch.common.collect.List.of("ct1"), 0L, 1L, null, new ComposableIndexTemplate.DataStreamTemplate());
state = service.addIndexTemplateV2(state, true, "logs-data-stream-template", it);
List<CompressedXContent> mappings = MetadataIndexTemplateService.collectMappings(state, "logs-data-stream-template", DataStream.getDefaultBackingIndexName("logs", 1L));
assertNotNull(mappings);
assertThat(mappings.size(), equalTo(4));
List<Map<String, Object>> parsedMappings = mappings.stream().map(m -> {
try {
return MapperService.parseMapping(new NamedXContentRegistry(org.opensearch.common.collect.List.of()), m.string());
} catch (Exception e) {
logger.error(e);
fail("failed to parse mappings: " + m.string());
return null;
}
}).collect(Collectors.toList());
Map<String, Object> firstParsedMapping = org.opensearch.common.collect.Map.of("_doc", org.opensearch.common.collect.Map.of("properties", org.opensearch.common.collect.Map.of(TIMESTAMP_FIELD.getName(), org.opensearch.common.collect.Map.of("type", "date"))));
assertThat(parsedMappings.get(0), equalTo(firstParsedMapping));
Map<String, Object> secondMapping = org.opensearch.common.collect.Map.of("_doc", org.opensearch.common.collect.Map.of("properties", org.opensearch.common.collect.Map.of("field1", org.opensearch.common.collect.Map.of("type", "keyword"))));
assertThat(parsedMappings.get(1), equalTo(secondMapping));
Map<String, Object> thirdMapping = org.opensearch.common.collect.Map.of("_doc", org.opensearch.common.collect.Map.of("properties", org.opensearch.common.collect.Map.of("field2", org.opensearch.common.collect.Map.of("type", "integer"))));
assertThat(parsedMappings.get(2), equalTo(thirdMapping));
}
{
// indices matched by templates without the data stream field defined don't get the default @timestamp mapping
ComposableIndexTemplate it = new ComposableIndexTemplate(org.opensearch.common.collect.List.of("timeseries*"), new Template(null, new CompressedXContent("{\n" + " \"properties\": {\n" + " \"field2\": {\n" + " \"type\": \"integer\"\n" + " }\n" + " }\n" + " }"), null), org.opensearch.common.collect.List.of("ct1"), 0L, 1L, null, null);
state = service.addIndexTemplateV2(state, true, "timeseries-template", it);
List<CompressedXContent> mappings = MetadataIndexTemplateService.collectMappings(state, "timeseries-template", "timeseries");
assertNotNull(mappings);
assertThat(mappings.size(), equalTo(2));
List<Map<String, Object>> parsedMappings = mappings.stream().map(m -> {
try {
return MapperService.parseMapping(new NamedXContentRegistry(org.opensearch.common.collect.List.of()), m.string());
} catch (Exception e) {
logger.error(e);
fail("failed to parse mappings: " + m.string());
return null;
}
}).collect(Collectors.toList());
Map<String, Object> firstMapping = org.opensearch.common.collect.Map.of("_doc", org.opensearch.common.collect.Map.of("properties", org.opensearch.common.collect.Map.of("field1", org.opensearch.common.collect.Map.of("type", "keyword"))));
assertThat(parsedMappings.get(0), equalTo(firstMapping));
Map<String, Object> secondMapping = org.opensearch.common.collect.Map.of("_doc", org.opensearch.common.collect.Map.of("properties", org.opensearch.common.collect.Map.of("field2", org.opensearch.common.collect.Map.of("type", "integer"))));
assertThat(parsedMappings.get(1), equalTo(secondMapping));
// a default @timestamp mapping will not be added if the matching template doesn't have the data stream field configured, even
// if the index name matches that of a data stream backing index
mappings = MetadataIndexTemplateService.collectMappings(state, "timeseries-template", DataStream.getDefaultBackingIndexName("timeseries", 1L));
assertNotNull(mappings);
assertThat(mappings.size(), equalTo(2));
parsedMappings = mappings.stream().map(m -> {
try {
return MapperService.parseMapping(new NamedXContentRegistry(org.opensearch.common.collect.List.of()), m.string());
} catch (Exception e) {
logger.error(e);
fail("failed to parse mappings: " + m.string());
return null;
}
}).collect(Collectors.toList());
firstMapping = org.opensearch.common.collect.Map.of("_doc", org.opensearch.common.collect.Map.of("properties", org.opensearch.common.collect.Map.of("field1", org.opensearch.common.collect.Map.of("type", "keyword"))));
assertThat(parsedMappings.get(0), equalTo(firstMapping));
secondMapping = org.opensearch.common.collect.Map.of("_doc", org.opensearch.common.collect.Map.of("properties", org.opensearch.common.collect.Map.of("field2", org.opensearch.common.collect.Map.of("type", "integer"))));
assertThat(parsedMappings.get(1), equalTo(secondMapping));
}
}
use of org.opensearch.common.xcontent.NamedXContentRegistry in project OpenSearch by opensearch-project.
the class MetadataIndexTemplateServiceTests method testUserDefinedMappingTakesPrecedenceOverDefault.
public void testUserDefinedMappingTakesPrecedenceOverDefault() throws Exception {
final MetadataIndexTemplateService service = getMetadataIndexTemplateService();
ClusterState state = ClusterState.EMPTY_STATE;
{
// user defines a @timestamp mapping as part of a component template
ComponentTemplate ct1 = new ComponentTemplate(new Template(null, new CompressedXContent("{\n" + " \"properties\": {\n" + " \"@timestamp\": {\n" + " \"type\": \"date_nanos\"\n" + " }\n" + " }\n" + " }"), null), null, null);
state = service.addComponentTemplate(state, true, "ct1", ct1);
ComposableIndexTemplate it = new ComposableIndexTemplate(org.opensearch.common.collect.List.of("logs*"), null, org.opensearch.common.collect.List.of("ct1"), 0L, 1L, null, new ComposableIndexTemplate.DataStreamTemplate());
state = service.addIndexTemplateV2(state, true, "logs-template", it);
List<CompressedXContent> mappings = MetadataIndexTemplateService.collectMappings(state, "logs-template", DataStream.getDefaultBackingIndexName("logs", 1L));
assertNotNull(mappings);
assertThat(mappings.size(), equalTo(3));
List<Map<String, Object>> parsedMappings = mappings.stream().map(m -> {
try {
return MapperService.parseMapping(new NamedXContentRegistry(org.opensearch.common.collect.List.of()), m.string());
} catch (Exception e) {
logger.error(e);
fail("failed to parse mappings: " + m.string());
return null;
}
}).collect(Collectors.toList());
Map<String, Object> firstMapping = org.opensearch.common.collect.Map.of("_doc", org.opensearch.common.collect.Map.of("properties", org.opensearch.common.collect.Map.of(TIMESTAMP_FIELD.getName(), org.opensearch.common.collect.Map.of("type", "date"))));
assertThat(parsedMappings.get(0), equalTo(firstMapping));
Map<String, Object> secondMapping = org.opensearch.common.collect.Map.of("_doc", org.opensearch.common.collect.Map.of("properties", org.opensearch.common.collect.Map.of(TIMESTAMP_FIELD.getName(), org.opensearch.common.collect.Map.of("type", "date_nanos"))));
assertThat(parsedMappings.get(1), equalTo(secondMapping));
}
{
// user defines a @timestamp mapping as part of a composable index template
Template template = new Template(null, new CompressedXContent("{\n" + " \"properties\": {\n" + " \"@timestamp\": {\n" + " \"type\": \"date_nanos\"\n" + " }\n" + " }\n" + " }"), null);
ComposableIndexTemplate it = new ComposableIndexTemplate(org.opensearch.common.collect.List.of("timeseries*"), template, null, 0L, 1L, null, new ComposableIndexTemplate.DataStreamTemplate());
state = service.addIndexTemplateV2(state, true, "timeseries-template", it);
List<CompressedXContent> mappings = MetadataIndexTemplateService.collectMappings(state, "timeseries-template", DataStream.getDefaultBackingIndexName("timeseries-template", 1L));
assertNotNull(mappings);
assertThat(mappings.size(), equalTo(3));
List<Map<String, Object>> parsedMappings = mappings.stream().map(m -> {
try {
return MapperService.parseMapping(new NamedXContentRegistry(org.opensearch.common.collect.List.of()), m.string());
} catch (Exception e) {
logger.error(e);
fail("failed to parse mappings: " + m.string());
return null;
}
}).collect(Collectors.toList());
Map<String, Object> firstMapping = org.opensearch.common.collect.Map.of("_doc", org.opensearch.common.collect.Map.of("properties", org.opensearch.common.collect.Map.of(TIMESTAMP_FIELD.getName(), org.opensearch.common.collect.Map.of("type", "date"))));
assertThat(parsedMappings.get(0), equalTo(firstMapping));
Map<String, Object> secondMapping = org.opensearch.common.collect.Map.of("_doc", org.opensearch.common.collect.Map.of("properties", org.opensearch.common.collect.Map.of(TIMESTAMP_FIELD.getName(), org.opensearch.common.collect.Map.of("type", "date_nanos"))));
assertThat(parsedMappings.get(1), equalTo(secondMapping));
}
}
use of org.opensearch.common.xcontent.NamedXContentRegistry in project OpenSearch by opensearch-project.
the class MetadataIndexTemplateServiceTests method assertTemplatesEqual.
@SuppressWarnings("unchecked")
public static void assertTemplatesEqual(ComposableIndexTemplate actual, ComposableIndexTemplate expected) {
ComposableIndexTemplate actualNoTemplate = new ComposableIndexTemplate(actual.indexPatterns(), null, actual.composedOf(), actual.priority(), actual.version(), actual.metadata(), actual.getDataStreamTemplate());
ComposableIndexTemplate expectedNoTemplate = new ComposableIndexTemplate(expected.indexPatterns(), null, expected.composedOf(), expected.priority(), expected.version(), expected.metadata(), expected.getDataStreamTemplate());
assertThat(actualNoTemplate, equalTo(expectedNoTemplate));
Template actualTemplate = actual.template();
Template expectedTemplate = expected.template();
assertThat("expected both templates to have either a template or no template", Objects.nonNull(actualTemplate), equalTo(Objects.nonNull(expectedTemplate)));
if (actualTemplate != null) {
assertThat(actualTemplate.settings(), equalTo(expectedTemplate.settings()));
assertThat(actualTemplate.aliases(), equalTo(expectedTemplate.aliases()));
assertThat("expected both templates to have either mappings or no mappings", Objects.nonNull(actualTemplate.mappings()), equalTo(Objects.nonNull(expectedTemplate.mappings())));
if (actualTemplate.mappings() != null) {
Map<String, Object> actualMappings;
Map<String, Object> expectedMappings;
try (XContentParser parser = XContentType.JSON.xContent().createParser(new NamedXContentRegistry(Collections.emptyList()), LoggingDeprecationHandler.INSTANCE, actualTemplate.mappings().string())) {
actualMappings = parser.map();
} catch (IOException e) {
throw new AssertionError(e);
}
try (XContentParser parser = XContentType.JSON.xContent().createParser(new NamedXContentRegistry(Collections.emptyList()), LoggingDeprecationHandler.INSTANCE, expectedTemplate.mappings().string())) {
expectedMappings = parser.map();
} catch (IOException e) {
throw new AssertionError(e);
}
if (actualMappings.size() == 1 && actualMappings.containsKey(MapperService.SINGLE_MAPPING_NAME)) {
actualMappings = (Map<String, Object>) actualMappings.get(MapperService.SINGLE_MAPPING_NAME);
}
if (expectedMappings.size() == 1 && expectedMappings.containsKey(MapperService.SINGLE_MAPPING_NAME)) {
expectedMappings = (Map<String, Object>) expectedMappings.get(MapperService.SINGLE_MAPPING_NAME);
}
assertThat(actualMappings, equalTo(expectedMappings));
}
}
}
Aggregations