Search in sources :

Example 6 with XContentType

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

the class CrudIT method testBulkProcessorIntegration.

public void testBulkProcessorIntegration() throws IOException {
    int nbItems = randomIntBetween(10, 100);
    boolean[] errors = new boolean[nbItems];
    XContentType xContentType = randomFrom(XContentType.JSON, XContentType.SMILE);
    AtomicReference<BulkResponse> responseRef = new AtomicReference<>();
    AtomicReference<BulkRequest> requestRef = new AtomicReference<>();
    AtomicReference<Throwable> error = new AtomicReference<>();
    BulkProcessor.Listener listener = new BulkProcessor.Listener() {

        @Override
        public void beforeBulk(long executionId, BulkRequest request) {
        }

        @Override
        public void afterBulk(long executionId, BulkRequest request, BulkResponse response) {
            responseRef.set(response);
            requestRef.set(request);
        }

        @Override
        public void afterBulk(long executionId, BulkRequest request, Throwable failure) {
            error.set(failure);
        }
    };
    try (BulkProcessor processor = BulkProcessor.builder((request, bulkListener) -> highLevelClient().bulkAsync(request, RequestOptions.DEFAULT, bulkListener), listener).setConcurrentRequests(0).setBulkSize(new ByteSizeValue(5, ByteSizeUnit.GB)).setBulkActions(nbItems + 1).build()) {
        for (int i = 0; i < nbItems; i++) {
            String id = String.valueOf(i);
            boolean erroneous = randomBoolean();
            errors[i] = erroneous;
            DocWriteRequest.OpType opType = randomFrom(DocWriteRequest.OpType.values());
            if (opType == DocWriteRequest.OpType.DELETE) {
                if (erroneous == false) {
                    assertEquals(RestStatus.CREATED, highLevelClient().index(new IndexRequest("index").id(id).source("field", -1), RequestOptions.DEFAULT).status());
                }
                DeleteRequest deleteRequest = new DeleteRequest("index", id);
                processor.add(deleteRequest);
            } else {
                if (opType == DocWriteRequest.OpType.INDEX) {
                    IndexRequest indexRequest = new IndexRequest("index").id(id).source(xContentType, "id", i);
                    if (erroneous) {
                        indexRequest.setIfSeqNo(12L);
                        indexRequest.setIfPrimaryTerm(12L);
                    }
                    processor.add(indexRequest);
                } else if (opType == DocWriteRequest.OpType.CREATE) {
                    IndexRequest createRequest = new IndexRequest("index").id(id).source(xContentType, "id", i).create(true);
                    if (erroneous) {
                        assertEquals(RestStatus.CREATED, highLevelClient().index(createRequest, RequestOptions.DEFAULT).status());
                    }
                    processor.add(createRequest);
                } else if (opType == DocWriteRequest.OpType.UPDATE) {
                    UpdateRequest updateRequest = new UpdateRequest("index", id).doc(new IndexRequest().source(xContentType, "id", i));
                    if (erroneous == false) {
                        assertEquals(RestStatus.CREATED, highLevelClient().index(new IndexRequest("index").id(id).source("field", -1), RequestOptions.DEFAULT).status());
                    }
                    processor.add(updateRequest);
                }
            }
        }
        assertNull(responseRef.get());
        assertNull(requestRef.get());
    }
    BulkResponse bulkResponse = responseRef.get();
    BulkRequest bulkRequest = requestRef.get();
    assertEquals(RestStatus.OK, bulkResponse.status());
    assertTrue(bulkResponse.getTook().getMillis() > 0);
    assertEquals(nbItems, bulkResponse.getItems().length);
    assertNull(error.get());
    validateBulkResponses(nbItems, errors, bulkResponse, bulkRequest);
}
Also used : UpdateRequest(org.opensearch.action.update.UpdateRequest) ByteSizeValue(org.opensearch.common.unit.ByteSizeValue) BulkResponse(org.opensearch.action.bulk.BulkResponse) AtomicReference(java.util.concurrent.atomic.AtomicReference) Matchers.containsString(org.hamcrest.Matchers.containsString) GetIndexRequest(org.opensearch.client.indices.GetIndexRequest) IndexRequest(org.opensearch.action.index.IndexRequest) XContentType(org.opensearch.common.xcontent.XContentType) BulkRequest(org.opensearch.action.bulk.BulkRequest) BulkProcessor(org.opensearch.action.bulk.BulkProcessor) DocWriteRequest(org.opensearch.action.DocWriteRequest) DeleteRequest(org.opensearch.action.delete.DeleteRequest)

Example 7 with XContentType

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

the class RequestConvertersTests method testPutScript.

public void testPutScript() throws Exception {
    PutStoredScriptRequest putStoredScriptRequest = new PutStoredScriptRequest();
    String id = randomAlphaOfLengthBetween(5, 10);
    putStoredScriptRequest.id(id);
    XContentType xContentType = randomFrom(XContentType.values());
    try (XContentBuilder builder = XContentBuilder.builder(xContentType.xContent())) {
        builder.startObject();
        builder.startObject("script").field("lang", "painless").field("source", "Math.log(_score * 2) + params.multiplier").endObject();
        builder.endObject();
        putStoredScriptRequest.content(BytesReference.bytes(builder), xContentType);
    }
    Map<String, String> expectedParams = new HashMap<>();
    setRandomMasterTimeout(putStoredScriptRequest, expectedParams);
    setRandomTimeout(putStoredScriptRequest::timeout, AcknowledgedRequest.DEFAULT_ACK_TIMEOUT, expectedParams);
    if (randomBoolean()) {
        String context = randomAlphaOfLengthBetween(5, 10);
        putStoredScriptRequest.context(context);
        expectedParams.put("context", context);
    }
    Request request = RequestConverters.putScript(putStoredScriptRequest);
    assertThat(request.getEndpoint(), equalTo("/_scripts/" + id));
    assertThat(request.getParameters(), equalTo(expectedParams));
    assertNotNull(request.getEntity());
    assertToXContentBody(putStoredScriptRequest, request.getEntity());
}
Also used : XContentType(org.opensearch.common.xcontent.XContentType) HashMap(java.util.HashMap) PutStoredScriptRequest(org.opensearch.action.admin.cluster.storedscripts.PutStoredScriptRequest) RandomSearchRequestGenerator.randomSearchRequest(org.opensearch.search.RandomSearchRequestGenerator.randomSearchRequest) MasterNodeRequest(org.opensearch.action.support.master.MasterNodeRequest) WriteRequest(org.opensearch.action.support.WriteRequest) AbstractBulkByScrollRequest(org.opensearch.index.reindex.AbstractBulkByScrollRequest) RatedRequest(org.opensearch.index.rankeval.RatedRequest) DeleteRequest(org.opensearch.action.delete.DeleteRequest) TermVectorsRequest(org.opensearch.client.core.TermVectorsRequest) AcknowledgedRequest(org.opensearch.action.support.master.AcknowledgedRequest) FieldCapabilitiesRequest(org.opensearch.action.fieldcaps.FieldCapabilitiesRequest) UpdateRequest(org.opensearch.action.update.UpdateRequest) GetSourceRequest(org.opensearch.client.core.GetSourceRequest) MultiSearchRequest(org.opensearch.action.search.MultiSearchRequest) DocWriteRequest(org.opensearch.action.DocWriteRequest) SearchScrollRequest(org.opensearch.action.search.SearchScrollRequest) ExplainRequest(org.opensearch.action.explain.ExplainRequest) SearchRequest(org.opensearch.action.search.SearchRequest) PutStoredScriptRequest(org.opensearch.action.admin.cluster.storedscripts.PutStoredScriptRequest) DeleteByQueryRequest(org.opensearch.index.reindex.DeleteByQueryRequest) MultiGetRequest(org.opensearch.action.get.MultiGetRequest) BulkRequest(org.opensearch.action.bulk.BulkRequest) GetStoredScriptRequest(org.opensearch.action.admin.cluster.storedscripts.GetStoredScriptRequest) ReplicationRequest(org.opensearch.action.support.replication.ReplicationRequest) AnalyzeRequest(org.opensearch.client.indices.AnalyzeRequest) CountRequest(org.opensearch.client.core.CountRequest) BulkShardRequest(org.opensearch.action.bulk.BulkShardRequest) SearchTemplateRequest(org.opensearch.script.mustache.SearchTemplateRequest) RankEvalRequest(org.opensearch.index.rankeval.RankEvalRequest) DeleteStoredScriptRequest(org.opensearch.action.admin.cluster.storedscripts.DeleteStoredScriptRequest) GetRequest(org.opensearch.action.get.GetRequest) MultiSearchTemplateRequest(org.opensearch.script.mustache.MultiSearchTemplateRequest) UpdateByQueryRequest(org.opensearch.index.reindex.UpdateByQueryRequest) MultiTermVectorsRequest(org.opensearch.client.core.MultiTermVectorsRequest) ClearScrollRequest(org.opensearch.action.search.ClearScrollRequest) IndexRequest(org.opensearch.action.index.IndexRequest) ReindexRequest(org.opensearch.index.reindex.ReindexRequest) XContentBuilder(org.opensearch.common.xcontent.XContentBuilder)

Example 8 with XContentType

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

the class RequestConvertersTests method testCreateContentType.

public void testCreateContentType() {
    final XContentType xContentType = randomFrom(XContentType.values());
    assertEquals(xContentType.mediaTypeWithoutParameters(), RequestConverters.createContentType(xContentType).getMimeType());
}
Also used : XContentType(org.opensearch.common.xcontent.XContentType)

Example 9 with XContentType

use of org.opensearch.common.xcontent.XContentType 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 10 with XContentType

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

the class JsonXContentGenerator method writeRawField.

@Override
public void writeRawField(String name, InputStream content) throws IOException {
    if (content.markSupported() == false) {
        // needed for the XContentFactory.xContentType call
        content = new BufferedInputStream(content);
    }
    XContentType contentType = XContentFactory.xContentType(content);
    if (contentType == null) {
        throw new IllegalArgumentException("Can't write raw bytes whose xcontent-type can't be guessed");
    }
    writeRawField(name, content, contentType);
}
Also used : XContentType(org.opensearch.common.xcontent.XContentType) BufferedInputStream(java.io.BufferedInputStream)

Aggregations

XContentType (org.opensearch.common.xcontent.XContentType)148 BytesReference (org.opensearch.common.bytes.BytesReference)99 XContentParser (org.opensearch.common.xcontent.XContentParser)85 XContentBuilder (org.opensearch.common.xcontent.XContentBuilder)44 IOException (java.io.IOException)31 HashMap (java.util.HashMap)31 Map (java.util.Map)30 ToXContent (org.opensearch.common.xcontent.ToXContent)22 List (java.util.List)18 IndexRequest (org.opensearch.action.index.IndexRequest)16 ArrayList (java.util.ArrayList)15 Predicate (java.util.function.Predicate)14 OpenSearchTestCase (org.opensearch.test.OpenSearchTestCase)14 XContentTestUtils.insertRandomFields (org.opensearch.test.XContentTestUtils.insertRandomFields)12 Matchers.containsString (org.hamcrest.Matchers.containsString)11 DocWriteRequest (org.opensearch.action.DocWriteRequest)11 XContentHelper.toXContent (org.opensearch.common.xcontent.XContentHelper.toXContent)11 Collections (java.util.Collections)10 DeleteRequest (org.opensearch.action.delete.DeleteRequest)10 OpenSearchAssertions.assertToXContentEquivalent (org.opensearch.test.hamcrest.OpenSearchAssertions.assertToXContentEquivalent)10