Search in sources :

Example 1 with CompressedXContent

use of org.opensearch.common.compress.CompressedXContent in project OpenSearch by opensearch-project.

the class IndicesClientIT method testSimulateIndexTemplate.

public void testSimulateIndexTemplate() throws Exception {
    String templateName = "my-template";
    Settings settings = Settings.builder().put("index.number_of_shards", 1).build();
    CompressedXContent mappings = new CompressedXContent("{\"properties\":{\"host_name\":{\"type\":\"keyword\"}}}");
    AliasMetadata alias = AliasMetadata.builder("alias").writeIndex(true).build();
    Template template = new Template(settings, mappings, org.opensearch.common.collect.Map.of("alias", alias));
    List<String> pattern = org.opensearch.common.collect.List.of("pattern");
    ComposableIndexTemplate indexTemplate = new ComposableIndexTemplate(pattern, template, Collections.emptyList(), 1L, 1L, new HashMap<>(), null);
    PutComposableIndexTemplateRequest putComposableIndexTemplateRequest = new PutComposableIndexTemplateRequest().name(templateName).create(true).indexTemplate(indexTemplate);
    AcknowledgedResponse response = execute(putComposableIndexTemplateRequest, highLevelClient().indices()::putIndexTemplate, highLevelClient().indices()::putIndexTemplateAsync);
    assertThat(response.isAcknowledged(), equalTo(true));
    SimulateIndexTemplateRequest simulateIndexTemplateRequest = new SimulateIndexTemplateRequest("pattern");
    AliasMetadata simulationAlias = AliasMetadata.builder("simulation-alias").writeIndex(true).build();
    ComposableIndexTemplate simulationTemplate = new ComposableIndexTemplate(pattern, new Template(null, null, org.opensearch.common.collect.Map.of("simulation-alias", simulationAlias)), Collections.emptyList(), 2L, 1L, new HashMap<>(), null);
    PutComposableIndexTemplateRequest newIndexTemplateReq = new PutComposableIndexTemplateRequest().name("used-for-simulation").create(true).indexTemplate(indexTemplate);
    newIndexTemplateReq.indexTemplate(simulationTemplate);
    simulateIndexTemplateRequest.indexTemplateV2Request(newIndexTemplateReq);
    SimulateIndexTemplateResponse simulateResponse = execute(simulateIndexTemplateRequest, highLevelClient().indices()::simulateIndexTemplate, highLevelClient().indices()::simulateIndexTemplateAsync);
    Map<String, AliasMetadata> aliases = simulateResponse.resolvedTemplate().aliases();
    assertThat(aliases, is(notNullValue()));
    assertThat("the template we provided for the simulation has a higher priority than the one in the system", aliases.get("simulation-alias"), is(notNullValue()));
    assertThat(aliases.get("simulation-alias").getAlias(), is("simulation-alias"));
    assertThat("existing template overlaps the higher priority template we provided for the simulation", simulateResponse.overlappingTemplates().get("my-template").get(0), is("pattern"));
}
Also used : ComposableIndexTemplate(org.opensearch.cluster.metadata.ComposableIndexTemplate) AliasMetadata(org.opensearch.cluster.metadata.AliasMetadata) AcknowledgedResponse(org.opensearch.action.support.master.AcknowledgedResponse) Matchers.containsString(org.hamcrest.Matchers.containsString) PutComposableIndexTemplateRequest(org.opensearch.client.indices.PutComposableIndexTemplateRequest) ComposableIndexTemplate(org.opensearch.cluster.metadata.ComposableIndexTemplate) Template(org.opensearch.cluster.metadata.Template) SimulateIndexTemplateRequest(org.opensearch.client.indices.SimulateIndexTemplateRequest) CompressedXContent(org.opensearch.common.compress.CompressedXContent) SimulateIndexTemplateResponse(org.opensearch.client.indices.SimulateIndexTemplateResponse) Settings(org.opensearch.common.settings.Settings) IndexSettings(org.opensearch.index.IndexSettings)

Example 2 with CompressedXContent

use of org.opensearch.common.compress.CompressedXContent in project OpenSearch by opensearch-project.

the class IndicesClientIT method testIndexTemplates.

public void testIndexTemplates() throws Exception {
    String templateName = "my-template";
    Settings settings = Settings.builder().put("index.number_of_shards", 1).build();
    CompressedXContent mappings = new CompressedXContent("{\"properties\":{\"host_name\":{\"type\":\"keyword\"}}}");
    AliasMetadata alias = AliasMetadata.builder("alias").writeIndex(true).build();
    Template template = new Template(settings, mappings, Collections.singletonMap("alias", alias));
    List<String> pattern = Collections.singletonList("pattern");
    ComposableIndexTemplate indexTemplate = new ComposableIndexTemplate(pattern, template, Collections.emptyList(), 1L, 1L, new HashMap<>(), null);
    PutComposableIndexTemplateRequest putComposableIndexTemplateRequest = new PutComposableIndexTemplateRequest().name(templateName).create(true).indexTemplate(indexTemplate);
    AcknowledgedResponse response = execute(putComposableIndexTemplateRequest, highLevelClient().indices()::putIndexTemplate, highLevelClient().indices()::putIndexTemplateAsync);
    assertThat(response.isAcknowledged(), equalTo(true));
    ComposableIndexTemplateExistRequest composableIndexTemplateExistRequest = new ComposableIndexTemplateExistRequest(templateName);
    boolean exist = execute(composableIndexTemplateExistRequest, highLevelClient().indices()::existsIndexTemplate, highLevelClient().indices()::existsIndexTemplateAsync);
    assertTrue(exist);
    GetComposableIndexTemplateRequest getComposableIndexTemplateRequest = new GetComposableIndexTemplateRequest(templateName);
    GetComposableIndexTemplatesResponse getResponse = execute(getComposableIndexTemplateRequest, highLevelClient().indices()::getIndexTemplate, highLevelClient().indices()::getIndexTemplateAsync);
    assertThat(getResponse.getIndexTemplates().size(), equalTo(1));
    assertThat(getResponse.getIndexTemplates().containsKey(templateName), equalTo(true));
    assertThat(getResponse.getIndexTemplates().get(templateName), equalTo(indexTemplate));
    DeleteComposableIndexTemplateRequest deleteComposableIndexTemplateRequest = new DeleteComposableIndexTemplateRequest(templateName);
    response = execute(deleteComposableIndexTemplateRequest, highLevelClient().indices()::deleteIndexTemplate, highLevelClient().indices()::deleteIndexTemplateAsync);
    assertThat(response.isAcknowledged(), equalTo(true));
    OpenSearchStatusException statusException = expectThrows(OpenSearchStatusException.class, () -> execute(getComposableIndexTemplateRequest, highLevelClient().indices()::getIndexTemplate, highLevelClient().indices()::getIndexTemplateAsync));
    assertThat(statusException.status(), equalTo(RestStatus.NOT_FOUND));
    exist = execute(composableIndexTemplateExistRequest, highLevelClient().indices()::existsIndexTemplate, highLevelClient().indices()::existsIndexTemplateAsync);
    assertFalse(exist);
}
Also used : ComposableIndexTemplate(org.opensearch.cluster.metadata.ComposableIndexTemplate) AliasMetadata(org.opensearch.cluster.metadata.AliasMetadata) AcknowledgedResponse(org.opensearch.action.support.master.AcknowledgedResponse) Matchers.containsString(org.hamcrest.Matchers.containsString) PutComposableIndexTemplateRequest(org.opensearch.client.indices.PutComposableIndexTemplateRequest) ComposableIndexTemplateExistRequest(org.opensearch.client.indices.ComposableIndexTemplateExistRequest) ComposableIndexTemplate(org.opensearch.cluster.metadata.ComposableIndexTemplate) Template(org.opensearch.cluster.metadata.Template) CompressedXContent(org.opensearch.common.compress.CompressedXContent) GetComposableIndexTemplatesResponse(org.opensearch.client.indices.GetComposableIndexTemplatesResponse) DeleteComposableIndexTemplateRequest(org.opensearch.client.indices.DeleteComposableIndexTemplateRequest) Settings(org.opensearch.common.settings.Settings) IndexSettings(org.opensearch.index.IndexSettings) GetComposableIndexTemplateRequest(org.opensearch.client.indices.GetComposableIndexTemplateRequest) OpenSearchStatusException(org.opensearch.OpenSearchStatusException)

Example 3 with CompressedXContent

use of org.opensearch.common.compress.CompressedXContent in project OpenSearch by opensearch-project.

the class GetIndexTemplatesResponseTests method testParsingFromEsResponse.

public void testParsingFromEsResponse() throws IOException {
    for (int runs = 0; runs < 20; runs++) {
        org.opensearch.action.admin.indices.template.get.GetIndexTemplatesResponse esResponse = new org.opensearch.action.admin.indices.template.get.GetIndexTemplatesResponse(new ArrayList<>());
        XContentType xContentType = randomFrom(XContentType.values());
        int numTemplates = randomIntBetween(0, 32);
        for (int i = 0; i < numTemplates; i++) {
            org.opensearch.cluster.metadata.IndexTemplateMetadata.Builder esIMD = new org.opensearch.cluster.metadata.IndexTemplateMetadata.Builder(String.format(Locale.ROOT, "%02d ", i) + randomAlphaOfLength(4));
            esIMD.patterns(Arrays.asList(generateRandomStringArray(32, 4, false, false)));
            esIMD.settings(randomIndexSettings());
            esIMD.putMapping("_doc", new CompressedXContent(BytesReference.bytes(randomMapping("_doc", xContentType))));
            int numAliases = randomIntBetween(0, 8);
            for (int j = 0; j < numAliases; j++) {
                esIMD.putAlias(randomAliasMetadata(String.format(Locale.ROOT, "%02d ", j) + randomAlphaOfLength(4)));
            }
            esIMD.order(randomIntBetween(0, Integer.MAX_VALUE));
            esIMD.version(randomIntBetween(0, Integer.MAX_VALUE));
            esResponse.getIndexTemplates().add(esIMD.build());
        }
        XContentBuilder xContentBuilder = XContentBuilder.builder(xContentType.xContent());
        esResponse.toXContent(xContentBuilder, ToXContent.EMPTY_PARAMS);
        try (XContentParser parser = XContentHelper.createParser(NamedXContentRegistry.EMPTY, DeprecationHandler.THROW_UNSUPPORTED_OPERATION, BytesReference.bytes(xContentBuilder), xContentType)) {
            GetIndexTemplatesResponse response = GetIndexTemplatesResponse.fromXContent(parser);
            assertThat(response.getIndexTemplates().size(), equalTo(numTemplates));
            response.getIndexTemplates().sort(Comparator.comparing(IndexTemplateMetadata::name));
            for (int i = 0; i < numTemplates; i++) {
                org.opensearch.cluster.metadata.IndexTemplateMetadata esIMD = esResponse.getIndexTemplates().get(i);
                IndexTemplateMetadata result = response.getIndexTemplates().get(i);
                assertThat(result.patterns(), equalTo(esIMD.patterns()));
                assertThat(result.settings(), equalTo(esIMD.settings()));
                assertThat(result.order(), equalTo(esIMD.order()));
                assertThat(result.version(), equalTo(esIMD.version()));
                assertThat(esIMD.mappings().size(), equalTo(1));
                BytesReference mappingSource = esIMD.mappings().valuesIt().next().uncompressed();
                Map<String, Object> expectedMapping = XContentHelper.convertToMap(mappingSource, true, xContentBuilder.contentType()).v2();
                assertThat(result.mappings().sourceAsMap(), equalTo(expectedMapping.get("_doc")));
                assertThat(result.aliases().size(), equalTo(esIMD.aliases().size()));
                List<AliasMetadata> expectedAliases = Arrays.stream(esIMD.aliases().values().toArray(AliasMetadata.class)).sorted(Comparator.comparing(AliasMetadata::alias)).collect(Collectors.toList());
                List<AliasMetadata> actualAliases = Arrays.stream(result.aliases().values().toArray(AliasMetadata.class)).sorted(Comparator.comparing(AliasMetadata::alias)).collect(Collectors.toList());
                for (int j = 0; j < result.aliases().size(); j++) {
                    assertThat(actualAliases.get(j), equalTo(expectedAliases.get(j)));
                }
            }
        }
    }
}
Also used : XContentBuilder(org.opensearch.common.xcontent.XContentBuilder) XContentType(org.opensearch.common.xcontent.XContentType) CompressedXContent(org.opensearch.common.compress.CompressedXContent) BytesReference(org.opensearch.common.bytes.BytesReference) AliasMetadata(org.opensearch.cluster.metadata.AliasMetadata) XContentBuilder(org.opensearch.common.xcontent.XContentBuilder) XContentParser(org.opensearch.common.xcontent.XContentParser)

Example 4 with CompressedXContent

use of org.opensearch.common.compress.CompressedXContent in project OpenSearch by opensearch-project.

the class PercolatorFieldMapperTests method testAllowNoAdditionalSettings.

public void testAllowNoAdditionalSettings() throws Exception {
    addQueryFieldMappings();
    IndexService indexService = createIndex("test1", Settings.EMPTY);
    MapperService mapperService = indexService.mapperService();
    String percolatorMapper = Strings.toString(XContentFactory.jsonBuilder().startObject().startObject("doc").startObject("properties").startObject(fieldName).field("type", "percolator").field("index", "no").endObject().endObject().endObject().endObject());
    MapperParsingException e = expectThrows(MapperParsingException.class, () -> mapperService.merge("doc", new CompressedXContent(percolatorMapper), MapperService.MergeReason.MAPPING_UPDATE));
    assertThat(e.getMessage(), containsString("Mapping definition for [" + fieldName + "] has unsupported parameters:  [index : no]"));
}
Also used : MapperParsingException(org.opensearch.index.mapper.MapperParsingException) IndexService(org.opensearch.index.IndexService) CompressedXContent(org.opensearch.common.compress.CompressedXContent) Matchers.containsString(org.hamcrest.Matchers.containsString) MapperService(org.opensearch.index.mapper.MapperService)

Example 5 with CompressedXContent

use of org.opensearch.common.compress.CompressedXContent in project OpenSearch by opensearch-project.

the class PercolatorFieldMapperTests method testNestedPercolatorField.

// percolator field can be nested under an object field, but only one query can be specified per document
public void testNestedPercolatorField() throws Exception {
    String typeName = "doc";
    String percolatorMapper = Strings.toString(XContentFactory.jsonBuilder().startObject().startObject(typeName).startObject("properties").startObject("object_field").field("type", "object").startObject("properties").startObject("query_field").field("type", "percolator").endObject().endObject().endObject().endObject().endObject().endObject());
    mapperService.merge(typeName, new CompressedXContent(percolatorMapper), MapperService.MergeReason.MAPPING_UPDATE);
    QueryBuilder queryBuilder = matchQuery("field", "value");
    ParsedDocument doc = mapperService.documentMapper(typeName).parse(new SourceToParse("test", typeName, "1", BytesReference.bytes(jsonBuilder().startObject().startObject("object_field").field("query_field", queryBuilder).endObject().endObject()), XContentType.JSON));
    // also includes all other meta fields
    assertThat(doc.rootDoc().getFields().size(), equalTo(12));
    IndexableField queryBuilderField = doc.rootDoc().getField("object_field.query_field.query_builder_field");
    assertTrue(queryBuilderField.fieldType().omitNorms());
    IndexableField extractionResultField = doc.rootDoc().getField("object_field.query_field.extraction_result");
    assertTrue(extractionResultField.fieldType().omitNorms());
    BytesRef queryBuilderAsBytes = queryBuilderField.binaryValue();
    assertQueryBuilder(queryBuilderAsBytes, queryBuilder);
    doc = mapperService.documentMapper(typeName).parse(new SourceToParse("test", typeName, "1", BytesReference.bytes(jsonBuilder().startObject().startArray("object_field").startObject().field("query_field", queryBuilder).endObject().endArray().endObject()), XContentType.JSON));
    // also includes all other meta fields
    assertThat(doc.rootDoc().getFields().size(), equalTo(12));
    queryBuilderAsBytes = doc.rootDoc().getField("object_field.query_field.query_builder_field").binaryValue();
    assertQueryBuilder(queryBuilderAsBytes, queryBuilder);
    MapperParsingException e = expectThrows(MapperParsingException.class, () -> {
        mapperService.documentMapper(typeName).parse(new SourceToParse("test", typeName, "1", BytesReference.bytes(jsonBuilder().startObject().startArray("object_field").startObject().field("query_field", queryBuilder).endObject().startObject().field("query_field", queryBuilder).endObject().endArray().endObject()), XContentType.JSON));
    });
    assertThat(e.getCause(), instanceOf(IllegalArgumentException.class));
    assertThat(e.getCause().getMessage(), equalTo("a document can only contain one percolator query"));
}
Also used : IndexableField(org.apache.lucene.index.IndexableField) MapperParsingException(org.opensearch.index.mapper.MapperParsingException) ParsedDocument(org.opensearch.index.mapper.ParsedDocument) CompressedXContent(org.opensearch.common.compress.CompressedXContent) SourceToParse(org.opensearch.index.mapper.SourceToParse) Matchers.containsString(org.hamcrest.Matchers.containsString) BoostingQueryBuilder(org.opensearch.index.query.BoostingQueryBuilder) BoolQueryBuilder(org.opensearch.index.query.BoolQueryBuilder) HasChildQueryBuilder(org.opensearch.join.query.HasChildQueryBuilder) ConstantScoreQueryBuilder(org.opensearch.index.query.ConstantScoreQueryBuilder) FunctionScoreQueryBuilder(org.opensearch.index.query.functionscore.FunctionScoreQueryBuilder) QueryBuilder(org.opensearch.index.query.QueryBuilder) ScriptQueryBuilder(org.opensearch.index.query.ScriptQueryBuilder) DisMaxQueryBuilder(org.opensearch.index.query.DisMaxQueryBuilder) RangeQueryBuilder(org.opensearch.index.query.RangeQueryBuilder) HasParentQueryBuilder(org.opensearch.join.query.HasParentQueryBuilder) MatchAllQueryBuilder(org.opensearch.index.query.MatchAllQueryBuilder) BytesRef(org.apache.lucene.util.BytesRef)

Aggregations

CompressedXContent (org.opensearch.common.compress.CompressedXContent)174 Matchers.containsString (org.hamcrest.Matchers.containsString)71 XContentBuilder (org.opensearch.common.xcontent.XContentBuilder)35 Settings (org.opensearch.common.settings.Settings)34 IndexService (org.opensearch.index.IndexService)29 IOException (java.io.IOException)22 IndexableField (org.apache.lucene.index.IndexableField)21 DocumentMapper (org.opensearch.index.mapper.DocumentMapper)20 Map (java.util.Map)19 HashMap (java.util.HashMap)18 MapperParsingException (org.opensearch.index.mapper.MapperParsingException)18 SourceToParse (org.opensearch.index.mapper.SourceToParse)17 ClusterState (org.opensearch.cluster.ClusterState)15 IndexSettings (org.opensearch.index.IndexSettings)14 ParsedDocument (org.opensearch.index.mapper.ParsedDocument)14 MapperService (org.opensearch.index.mapper.MapperService)13 ArrayList (java.util.ArrayList)12 BytesReference (org.opensearch.common.bytes.BytesReference)12 IndexScopedSettings (org.opensearch.common.settings.IndexScopedSettings)12 Collections (java.util.Collections)11