Search in sources :

Example 26 with NamedXContentRegistry

use of org.opensearch.common.xcontent.NamedXContentRegistry in project OpenSearch by opensearch-project.

the class AbstractSuggestionBuilderTestCase method init.

/**
 * setup for the whole base test class
 */
@BeforeClass
public static void init() {
    SearchModule searchModule = new SearchModule(Settings.EMPTY, emptyList());
    namedWriteableRegistry = new NamedWriteableRegistry(searchModule.getNamedWriteables());
    xContentRegistry = new NamedXContentRegistry(searchModule.getNamedXContents());
}
Also used : NamedWriteableRegistry(org.opensearch.common.io.stream.NamedWriteableRegistry) SearchModule(org.opensearch.search.SearchModule) NamedXContentRegistry(org.opensearch.common.xcontent.NamedXContentRegistry) BeforeClass(org.junit.BeforeClass)

Example 27 with NamedXContentRegistry

use of org.opensearch.common.xcontent.NamedXContentRegistry in project OpenSearch by opensearch-project.

the class SuggestBuilderTests method init.

/**
 * Setup for the whole base test class.
 */
@BeforeClass
public static void init() {
    SearchModule searchModule = new SearchModule(Settings.EMPTY, emptyList());
    namedWriteableRegistry = new NamedWriteableRegistry(searchModule.getNamedWriteables());
    xContentRegistry = new NamedXContentRegistry(searchModule.getNamedXContents());
}
Also used : NamedWriteableRegistry(org.opensearch.common.io.stream.NamedWriteableRegistry) SearchModule(org.opensearch.search.SearchModule) NamedXContentRegistry(org.opensearch.common.xcontent.NamedXContentRegistry) BeforeClass(org.junit.BeforeClass)

Example 28 with NamedXContentRegistry

use of org.opensearch.common.xcontent.NamedXContentRegistry in project OpenSearch by opensearch-project.

the class SortBuilderTests method init.

@BeforeClass
public static void init() {
    SearchModule searchModule = new SearchModule(Settings.EMPTY, emptyList());
    xContentRegistry = new NamedXContentRegistry(searchModule.getNamedXContents());
}
Also used : SearchModule(org.opensearch.search.SearchModule) NamedXContentRegistry(org.opensearch.common.xcontent.NamedXContentRegistry) BeforeClass(org.junit.BeforeClass)

Example 29 with NamedXContentRegistry

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));
    }
}
Also used : Arrays(java.util.Arrays) OpenSearchSingleNodeTestCase(org.opensearch.test.OpenSearchSingleNodeTestCase) IndexScopedSettings(org.opensearch.common.settings.IndexScopedSettings) TIMESTAMP_FIELD(org.opensearch.index.mapper.DataStreamFieldMapper.Defaults.TIMESTAMP_FIELD) Version(org.opensearch.Version) Strings(org.opensearch.common.Strings) Collections.singletonList(java.util.Collections.singletonList) XContentParser(org.opensearch.common.xcontent.XContentParser) CoreMatchers.instanceOf(org.hamcrest.CoreMatchers.instanceOf) MapperService(org.opensearch.index.mapper.MapperService) Alias(org.opensearch.action.admin.indices.alias.Alias) Settings.builder(org.opensearch.common.settings.Settings.builder) Map(java.util.Map) XContentFactory(org.opensearch.common.xcontent.XContentFactory) ActionListener(org.opensearch.action.ActionListener) TimeValue(org.opensearch.common.unit.TimeValue) CoreMatchers.containsString(org.hamcrest.CoreMatchers.containsString) Index(org.opensearch.index.Index) LoggingDeprecationHandler(org.opensearch.common.xcontent.LoggingDeprecationHandler) IndicesService(org.opensearch.indices.IndicesService) Set(java.util.Set) Settings(org.opensearch.common.settings.Settings) PutRequest(org.opensearch.cluster.metadata.MetadataIndexTemplateService.PutRequest) Collectors(java.util.stream.Collectors) Objects(java.util.Objects) CountDownLatch(java.util.concurrent.CountDownLatch) List(java.util.List) ShardLimitValidatorTests.createTestShardLimitService(org.opensearch.indices.ShardLimitValidatorTests.createTestShardLimitService) Matchers.contains(org.hamcrest.Matchers.contains) Matchers.containsInAnyOrder(org.hamcrest.Matchers.containsInAnyOrder) XContentType(org.opensearch.common.xcontent.XContentType) Matchers.is(org.hamcrest.Matchers.is) IndexTemplateMissingException(org.opensearch.indices.IndexTemplateMissingException) CoreMatchers.equalTo(org.hamcrest.CoreMatchers.equalTo) MapperParsingException(org.opensearch.index.mapper.MapperParsingException) CompressedXContent(org.opensearch.common.compress.CompressedXContent) CoreMatchers.not(org.hamcrest.CoreMatchers.not) HashMap(java.util.HashMap) AtomicReference(java.util.concurrent.atomic.AtomicReference) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) Matchers.matchesRegex(org.hamcrest.Matchers.matchesRegex) ClusterState(org.opensearch.cluster.ClusterState) Environment(org.opensearch.env.Environment) Matchers.empty(org.hamcrest.Matchers.empty) IOException(java.io.IOException) AcknowledgedResponse(org.opensearch.action.support.master.AcknowledgedResponse) TimeUnit(java.util.concurrent.TimeUnit) SystemIndices(org.opensearch.indices.SystemIndices) NamedXContentRegistry(org.opensearch.common.xcontent.NamedXContentRegistry) ClusterService(org.opensearch.cluster.service.ClusterService) InvalidIndexTemplateException(org.opensearch.indices.InvalidIndexTemplateException) CoreMatchers.containsStringIgnoringCase(org.hamcrest.CoreMatchers.containsStringIgnoringCase) Collections(java.util.Collections) ClusterState(org.opensearch.cluster.ClusterState) CoreMatchers.containsString(org.hamcrest.CoreMatchers.containsString) IndexTemplateMissingException(org.opensearch.indices.IndexTemplateMissingException) MapperParsingException(org.opensearch.index.mapper.MapperParsingException) IOException(java.io.IOException) InvalidIndexTemplateException(org.opensearch.indices.InvalidIndexTemplateException) CompressedXContent(org.opensearch.common.compress.CompressedXContent) Collections.singletonList(java.util.Collections.singletonList) List(java.util.List) ArrayList(java.util.ArrayList) NamedXContentRegistry(org.opensearch.common.xcontent.NamedXContentRegistry) Map(java.util.Map) HashMap(java.util.HashMap)

Example 30 with NamedXContentRegistry

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));
    }
}
Also used : ClusterState(org.opensearch.cluster.ClusterState) CoreMatchers.containsString(org.hamcrest.CoreMatchers.containsString) IndexTemplateMissingException(org.opensearch.indices.IndexTemplateMissingException) MapperParsingException(org.opensearch.index.mapper.MapperParsingException) IOException(java.io.IOException) InvalidIndexTemplateException(org.opensearch.indices.InvalidIndexTemplateException) CompressedXContent(org.opensearch.common.compress.CompressedXContent) Collections.singletonList(java.util.Collections.singletonList) List(java.util.List) ArrayList(java.util.ArrayList) NamedXContentRegistry(org.opensearch.common.xcontent.NamedXContentRegistry) Map(java.util.Map) HashMap(java.util.HashMap)

Aggregations

NamedXContentRegistry (org.opensearch.common.xcontent.NamedXContentRegistry)38 SearchModule (org.opensearch.search.SearchModule)19 XContentParser (org.opensearch.common.xcontent.XContentParser)15 NamedWriteableRegistry (org.opensearch.common.io.stream.NamedWriteableRegistry)13 ArrayList (java.util.ArrayList)11 Map (java.util.Map)10 BeforeClass (org.junit.BeforeClass)10 Settings (org.opensearch.common.settings.Settings)10 IOException (java.io.IOException)9 HashMap (java.util.HashMap)9 List (java.util.List)8 ClusterService (org.opensearch.cluster.service.ClusterService)7 HashSet (java.util.HashSet)6 Set (java.util.Set)6 ActionListener (org.opensearch.action.ActionListener)6 MapperService (org.opensearch.index.mapper.MapperService)6 Test (org.junit.Test)5 ClusterState (org.opensearch.cluster.ClusterState)5 XContentBuilder (org.opensearch.common.xcontent.XContentBuilder)5 Arrays (java.util.Arrays)4