Search in sources :

Example 1 with XContent

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

the class RequestConverters method multiSearch.

static Request multiSearch(MultiSearchRequest multiSearchRequest) throws IOException {
    Request request = new Request(HttpPost.METHOD_NAME, "/_msearch");
    Params params = new Params();
    params.putParam(RestSearchAction.TYPED_KEYS_PARAM, "true");
    if (multiSearchRequest.maxConcurrentSearchRequests() != MultiSearchRequest.MAX_CONCURRENT_SEARCH_REQUESTS_DEFAULT) {
        params.putParam("max_concurrent_searches", Integer.toString(multiSearchRequest.maxConcurrentSearchRequests()));
    }
    XContent xContent = REQUEST_BODY_CONTENT_TYPE.xContent();
    byte[] source = MultiSearchRequest.writeMultiLineFormat(multiSearchRequest, xContent);
    request.addParameters(params.asMap());
    request.setEntity(new NByteArrayEntity(source, createContentType(xContent.type())));
    return request;
}
Also used : NByteArrayEntity(org.apache.http.nio.entity.NByteArrayEntity) ToXContent(org.opensearch.common.xcontent.ToXContent) XContent(org.opensearch.common.xcontent.XContent) BulkRequest(org.opensearch.action.bulk.BulkRequest) WriteRequest(org.opensearch.action.support.WriteRequest) AbstractBulkByScrollRequest(org.opensearch.index.reindex.AbstractBulkByScrollRequest) GetStoredScriptRequest(org.opensearch.action.admin.cluster.storedscripts.GetStoredScriptRequest) AnalyzeRequest(org.opensearch.client.indices.AnalyzeRequest) DeleteRequest(org.opensearch.action.delete.DeleteRequest) CountRequest(org.opensearch.client.core.CountRequest) TermVectorsRequest(org.opensearch.client.core.TermVectorsRequest) FieldCapabilitiesRequest(org.opensearch.action.fieldcaps.FieldCapabilitiesRequest) UpdateRequest(org.opensearch.action.update.UpdateRequest) SearchTemplateRequest(org.opensearch.script.mustache.SearchTemplateRequest) RankEvalRequest(org.opensearch.index.rankeval.RankEvalRequest) 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) DeleteStoredScriptRequest(org.opensearch.action.admin.cluster.storedscripts.DeleteStoredScriptRequest) GetRequest(org.opensearch.action.get.GetRequest) MultiSearchTemplateRequest(org.opensearch.script.mustache.MultiSearchTemplateRequest) PutStoredScriptRequest(org.opensearch.action.admin.cluster.storedscripts.PutStoredScriptRequest) UpdateByQueryRequest(org.opensearch.index.reindex.UpdateByQueryRequest) MultiTermVectorsRequest(org.opensearch.client.core.MultiTermVectorsRequest) ClearScrollRequest(org.opensearch.action.search.ClearScrollRequest) DeleteByQueryRequest(org.opensearch.index.reindex.DeleteByQueryRequest) IndexRequest(org.opensearch.action.index.IndexRequest) ClusterHealthRequest(org.opensearch.action.admin.cluster.health.ClusterHealthRequest) ReindexRequest(org.opensearch.index.reindex.ReindexRequest) MultiGetRequest(org.opensearch.action.get.MultiGetRequest)

Example 2 with XContent

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

the class RequestConverters method multiSearchTemplate.

static Request multiSearchTemplate(MultiSearchTemplateRequest multiSearchTemplateRequest) throws IOException {
    Request request = new Request(HttpPost.METHOD_NAME, "/_msearch/template");
    Params params = new Params();
    params.putParam(RestSearchAction.TYPED_KEYS_PARAM, "true");
    if (multiSearchTemplateRequest.maxConcurrentSearchRequests() != MultiSearchRequest.MAX_CONCURRENT_SEARCH_REQUESTS_DEFAULT) {
        params.putParam("max_concurrent_searches", Integer.toString(multiSearchTemplateRequest.maxConcurrentSearchRequests()));
    }
    request.addParameters(params.asMap());
    XContent xContent = REQUEST_BODY_CONTENT_TYPE.xContent();
    byte[] source = MultiSearchTemplateRequest.writeMultiLineFormat(multiSearchTemplateRequest, xContent);
    request.setEntity(new NByteArrayEntity(source, createContentType(xContent.type())));
    return request;
}
Also used : NByteArrayEntity(org.apache.http.nio.entity.NByteArrayEntity) ToXContent(org.opensearch.common.xcontent.ToXContent) XContent(org.opensearch.common.xcontent.XContent) BulkRequest(org.opensearch.action.bulk.BulkRequest) WriteRequest(org.opensearch.action.support.WriteRequest) AbstractBulkByScrollRequest(org.opensearch.index.reindex.AbstractBulkByScrollRequest) GetStoredScriptRequest(org.opensearch.action.admin.cluster.storedscripts.GetStoredScriptRequest) AnalyzeRequest(org.opensearch.client.indices.AnalyzeRequest) DeleteRequest(org.opensearch.action.delete.DeleteRequest) CountRequest(org.opensearch.client.core.CountRequest) TermVectorsRequest(org.opensearch.client.core.TermVectorsRequest) FieldCapabilitiesRequest(org.opensearch.action.fieldcaps.FieldCapabilitiesRequest) UpdateRequest(org.opensearch.action.update.UpdateRequest) SearchTemplateRequest(org.opensearch.script.mustache.SearchTemplateRequest) RankEvalRequest(org.opensearch.index.rankeval.RankEvalRequest) 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) DeleteStoredScriptRequest(org.opensearch.action.admin.cluster.storedscripts.DeleteStoredScriptRequest) GetRequest(org.opensearch.action.get.GetRequest) MultiSearchTemplateRequest(org.opensearch.script.mustache.MultiSearchTemplateRequest) PutStoredScriptRequest(org.opensearch.action.admin.cluster.storedscripts.PutStoredScriptRequest) UpdateByQueryRequest(org.opensearch.index.reindex.UpdateByQueryRequest) MultiTermVectorsRequest(org.opensearch.client.core.MultiTermVectorsRequest) ClearScrollRequest(org.opensearch.action.search.ClearScrollRequest) DeleteByQueryRequest(org.opensearch.index.reindex.DeleteByQueryRequest) IndexRequest(org.opensearch.action.index.IndexRequest) ClusterHealthRequest(org.opensearch.action.admin.cluster.health.ClusterHealthRequest) ReindexRequest(org.opensearch.index.reindex.ReindexRequest) MultiGetRequest(org.opensearch.action.get.MultiGetRequest)

Example 3 with XContent

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

the class PainlessExecuteRequestTests method testFromXContent.

// Testing XContent serialization manually here, because the xContentType field in ContextSetup determines
// how the request needs to parse and the xcontent serialization framework randomizes that. The XContentType
// is not known and accessable when the test request instance is created in the xcontent serialization framework.
// Changing that is a big change. Writing a custom xcontent test here is the best option for now, because as far
// as I know this request class is the only case where this is a problem.
public final void testFromXContent() throws Exception {
    for (int i = 0; i < 20; i++) {
        PainlessExecuteAction.Request testInstance = createTestInstance();
        ContextSetup contextSetup = testInstance.getContextSetup();
        XContent xContent = randomFrom(XContentType.values()).xContent();
        if (contextSetup != null && contextSetup.getXContentType() != null) {
            xContent = contextSetup.getXContentType().xContent();
        }
        try (XContentBuilder builder = XContentBuilder.builder(xContent)) {
            builder.value(testInstance);
            StreamInput instanceInput = BytesReference.bytes(builder).streamInput();
            try (XContentParser parser = xContent.createParser(xContentRegistry(), LoggingDeprecationHandler.INSTANCE, instanceInput)) {
                PainlessExecuteAction.Request result = PainlessExecuteAction.Request.parse(parser);
                assertThat(result, equalTo(testInstance));
            }
        }
    }
}
Also used : ContextSetup(org.opensearch.painless.action.PainlessExecuteAction.Request.ContextSetup) XContent(org.opensearch.common.xcontent.XContent) StreamInput(org.opensearch.common.io.stream.StreamInput) XContentBuilder(org.opensearch.common.xcontent.XContentBuilder) XContentParser(org.opensearch.common.xcontent.XContentParser)

Example 4 with XContent

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

the class BulkRequestParser method parse.

/**
 * Parse the provided {@code data} assuming the provided default values. Index requests
 * will be passed to the {@code indexRequestConsumer}, update requests to the
 * {@code updateRequestConsumer} and delete requests to the {@code deleteRequestConsumer}.
 */
public void parse(BytesReference data, @Nullable String defaultIndex, @Nullable String defaultRouting, @Nullable FetchSourceContext defaultFetchSourceContext, @Nullable String defaultPipeline, @Nullable Boolean defaultRequireAlias, boolean allowExplicitIndex, XContentType xContentType, BiConsumer<IndexRequest, String> indexRequestConsumer, Consumer<UpdateRequest> updateRequestConsumer, Consumer<DeleteRequest> deleteRequestConsumer) throws IOException {
    XContent xContent = xContentType.xContent();
    int line = 0;
    int from = 0;
    byte marker = xContent.streamSeparator();
    // Bulk requests can contain a lot of repeated strings for the index, pipeline and routing parameters. This map is used to
    // deduplicate duplicate strings parsed for these parameters. While it does not prevent instantiating the duplicate strings, it
    // reduces their lifetime to the lifetime of this parse call instead of the lifetime of the full bulk request.
    final Map<String, String> stringDeduplicator = new HashMap<>();
    while (true) {
        int nextMarker = findNextMarker(marker, from, data);
        if (nextMarker == -1) {
            break;
        }
        line++;
        // now parse the action
        try (XContentParser parser = createParser(data, xContent, from, nextMarker)) {
            // move pointers
            from = nextMarker + 1;
            // Move to START_OBJECT
            XContentParser.Token token = parser.nextToken();
            if (token == null) {
                continue;
            }
            if (token != XContentParser.Token.START_OBJECT) {
                throw new IllegalArgumentException("Malformed action/metadata line [" + line + "], expected " + XContentParser.Token.START_OBJECT + " but found [" + token + "]");
            }
            // Move to FIELD_NAME, that's the action
            token = parser.nextToken();
            if (token != XContentParser.Token.FIELD_NAME) {
                throw new IllegalArgumentException("Malformed action/metadata line [" + line + "], expected " + XContentParser.Token.FIELD_NAME + " but found [" + token + "]");
            }
            String action = parser.currentName();
            String index = defaultIndex;
            String type = null;
            String id = null;
            String routing = defaultRouting;
            FetchSourceContext fetchSourceContext = defaultFetchSourceContext;
            String opType = null;
            long version = Versions.MATCH_ANY;
            VersionType versionType = VersionType.INTERNAL;
            long ifSeqNo = SequenceNumbers.UNASSIGNED_SEQ_NO;
            long ifPrimaryTerm = UNASSIGNED_PRIMARY_TERM;
            int retryOnConflict = 0;
            String pipeline = defaultPipeline;
            boolean requireAlias = defaultRequireAlias != null && defaultRequireAlias;
            // at this stage, next token can either be END_OBJECT (and use default index and type, with auto generated id)
            // or START_OBJECT which will have another set of parameters
            token = parser.nextToken();
            if (token == XContentParser.Token.START_OBJECT) {
                String currentFieldName = null;
                while ((token = parser.nextToken()) != XContentParser.Token.END_OBJECT) {
                    if (token == XContentParser.Token.FIELD_NAME) {
                        currentFieldName = parser.currentName();
                    } else if (token.isValue()) {
                        if (INDEX.match(currentFieldName, parser.getDeprecationHandler())) {
                            if (allowExplicitIndex == false) {
                                throw new IllegalArgumentException("explicit index in bulk is not allowed");
                            }
                            index = stringDeduplicator.computeIfAbsent(parser.text(), Function.identity());
                        } else if (TYPE.match(currentFieldName, parser.getDeprecationHandler())) {
                            if (errorOnType) {
                                throw new IllegalArgumentException("Action/metadata line [" + line + "] contains an unknown parameter [" + currentFieldName + "]");
                            }
                            type = stringDeduplicator.computeIfAbsent(parser.text(), Function.identity());
                        } else if (ID.match(currentFieldName, parser.getDeprecationHandler())) {
                            id = parser.text();
                        } else if (ROUTING.match(currentFieldName, parser.getDeprecationHandler())) {
                            routing = stringDeduplicator.computeIfAbsent(parser.text(), Function.identity());
                        } else if (OP_TYPE.match(currentFieldName, parser.getDeprecationHandler())) {
                            opType = parser.text();
                        } else if (VERSION.match(currentFieldName, parser.getDeprecationHandler())) {
                            version = parser.longValue();
                        } else if (VERSION_TYPE.match(currentFieldName, parser.getDeprecationHandler())) {
                            versionType = VersionType.fromString(parser.text());
                        } else if (IF_SEQ_NO.match(currentFieldName, parser.getDeprecationHandler())) {
                            ifSeqNo = parser.longValue();
                        } else if (IF_PRIMARY_TERM.match(currentFieldName, parser.getDeprecationHandler())) {
                            ifPrimaryTerm = parser.longValue();
                        } else if (RETRY_ON_CONFLICT.match(currentFieldName, parser.getDeprecationHandler())) {
                            retryOnConflict = parser.intValue();
                        } else if (PIPELINE.match(currentFieldName, parser.getDeprecationHandler())) {
                            pipeline = stringDeduplicator.computeIfAbsent(parser.text(), Function.identity());
                        } else if (SOURCE.match(currentFieldName, parser.getDeprecationHandler())) {
                            fetchSourceContext = FetchSourceContext.fromXContent(parser);
                        } else if (REQUIRE_ALIAS.match(currentFieldName, parser.getDeprecationHandler())) {
                            requireAlias = parser.booleanValue();
                        } else {
                            throw new IllegalArgumentException("Action/metadata line [" + line + "] contains an unknown parameter [" + currentFieldName + "]");
                        }
                    } else if (token == XContentParser.Token.START_ARRAY) {
                        throw new IllegalArgumentException("Malformed action/metadata line [" + line + "], expected a simple value for field [" + currentFieldName + "] but found [" + token + "]");
                    } else if (token == XContentParser.Token.START_OBJECT && SOURCE.match(currentFieldName, parser.getDeprecationHandler())) {
                        fetchSourceContext = FetchSourceContext.fromXContent(parser);
                    } else if (token != XContentParser.Token.VALUE_NULL) {
                        throw new IllegalArgumentException("Malformed action/metadata line [" + line + "], expected a simple value for field [" + currentFieldName + "] but found [" + token + "]");
                    }
                }
            } else if (token != XContentParser.Token.END_OBJECT) {
                throw new IllegalArgumentException("Malformed action/metadata line [" + line + "], expected " + XContentParser.Token.START_OBJECT + " or " + XContentParser.Token.END_OBJECT + " but found [" + token + "]");
            }
            if ("delete".equals(action)) {
                deleteRequestConsumer.accept(new DeleteRequest(index).id(id).routing(routing).version(version).versionType(versionType).setIfSeqNo(ifSeqNo).setIfPrimaryTerm(ifPrimaryTerm));
            } else {
                nextMarker = findNextMarker(marker, from, data);
                if (nextMarker == -1) {
                    break;
                }
                line++;
                // of index request.
                if ("index".equals(action)) {
                    if (opType == null) {
                        indexRequestConsumer.accept(new IndexRequest(index).id(id).routing(routing).version(version).versionType(versionType).setPipeline(pipeline).setIfSeqNo(ifSeqNo).setIfPrimaryTerm(ifPrimaryTerm).source(sliceTrimmingCarriageReturn(data, from, nextMarker, xContentType), xContentType).setRequireAlias(requireAlias), type);
                    } else {
                        indexRequestConsumer.accept(new IndexRequest(index).id(id).routing(routing).version(version).versionType(versionType).create("create".equals(opType)).setPipeline(pipeline).setIfSeqNo(ifSeqNo).setIfPrimaryTerm(ifPrimaryTerm).source(sliceTrimmingCarriageReturn(data, from, nextMarker, xContentType), xContentType).setRequireAlias(requireAlias), type);
                    }
                } else if ("create".equals(action)) {
                    indexRequestConsumer.accept(new IndexRequest(index).id(id).routing(routing).version(version).versionType(versionType).create(true).setPipeline(pipeline).setIfSeqNo(ifSeqNo).setIfPrimaryTerm(ifPrimaryTerm).source(sliceTrimmingCarriageReturn(data, from, nextMarker, xContentType), xContentType).setRequireAlias(requireAlias), type);
                } else if ("update".equals(action)) {
                    if (version != Versions.MATCH_ANY || versionType != VersionType.INTERNAL) {
                        throw new IllegalArgumentException("Update requests do not support versioning. " + "Please use `if_seq_no` and `if_primary_term` instead");
                    }
                    UpdateRequest updateRequest = new UpdateRequest().index(index).id(id).routing(routing).retryOnConflict(retryOnConflict).setIfSeqNo(ifSeqNo).setIfPrimaryTerm(ifPrimaryTerm).setRequireAlias(requireAlias).routing(routing);
                    try (XContentParser sliceParser = createParser(sliceTrimmingCarriageReturn(data, from, nextMarker, xContentType), xContent)) {
                        updateRequest.fromXContent(sliceParser);
                    }
                    if (fetchSourceContext != null) {
                        updateRequest.fetchSource(fetchSourceContext);
                    }
                    IndexRequest upsertRequest = updateRequest.upsertRequest();
                    if (upsertRequest != null) {
                        upsertRequest.setPipeline(defaultPipeline);
                    }
                    updateRequestConsumer.accept(updateRequest);
                }
                // move pointers
                from = nextMarker + 1;
            }
        }
    }
}
Also used : HashMap(java.util.HashMap) UpdateRequest(org.opensearch.action.update.UpdateRequest) IndexRequest(org.opensearch.action.index.IndexRequest) VersionType(org.opensearch.index.VersionType) FetchSourceContext(org.opensearch.search.fetch.subphase.FetchSourceContext) XContent(org.opensearch.common.xcontent.XContent) DeleteRequest(org.opensearch.action.delete.DeleteRequest) XContentParser(org.opensearch.common.xcontent.XContentParser)

Example 5 with XContent

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

the class DefaultShardOperationFailedExceptionTests method testFromXContent.

public void testFromXContent() throws IOException {
    XContent xContent = randomFrom(XContentType.values()).xContent();
    XContentBuilder builder = XContentBuilder.builder(xContent).startObject().field("shard", 1).field("index", "test").field("status", "INTERNAL_SERVER_ERROR").startObject("reason").field("type", "exception").field("reason", "foo").endObject().endObject();
    builder = shuffleXContent(builder);
    DefaultShardOperationFailedException parsed;
    try (XContentParser parser = createParser(xContent, BytesReference.bytes(builder))) {
        assertEquals(XContentParser.Token.START_OBJECT, parser.nextToken());
        parsed = DefaultShardOperationFailedException.fromXContent(parser);
        assertEquals(XContentParser.Token.END_OBJECT, parser.currentToken());
        assertNull(parser.nextToken());
    }
    assertNotNull(parsed);
    assertEquals(parsed.shardId(), 1);
    assertEquals(parsed.index(), "test");
    assertEquals(parsed.status(), RestStatus.INTERNAL_SERVER_ERROR);
    assertEquals(parsed.getCause().getMessage(), "OpenSearch exception [type=exception, reason=foo]");
}
Also used : XContent(org.opensearch.common.xcontent.XContent) XContentBuilder(org.opensearch.common.xcontent.XContentBuilder) XContentParser(org.opensearch.common.xcontent.XContentParser)

Aggregations

XContent (org.opensearch.common.xcontent.XContent)23 XContentParser (org.opensearch.common.xcontent.XContentParser)19 ToXContent (org.opensearch.common.xcontent.ToXContent)16 BytesReference (org.opensearch.common.bytes.BytesReference)12 XContentBuilder (org.opensearch.common.xcontent.XContentBuilder)10 IOException (java.io.IOException)7 RoutingMissingException (org.opensearch.action.RoutingMissingException)4 ParsingException (org.opensearch.common.ParsingException)4 XContentType (org.opensearch.common.xcontent.XContentType)4 DeleteRequest (org.opensearch.action.delete.DeleteRequest)3 IndexRequest (org.opensearch.action.index.IndexRequest)3 UpdateRequest (org.opensearch.action.update.UpdateRequest)3 ArrayList (java.util.ArrayList)2 List (java.util.List)2 Set (java.util.Set)2 NByteArrayEntity (org.apache.http.nio.entity.NByteArrayEntity)2 DocWriteRequest (org.opensearch.action.DocWriteRequest)2 ClusterHealthRequest (org.opensearch.action.admin.cluster.health.ClusterHealthRequest)2 DeleteStoredScriptRequest (org.opensearch.action.admin.cluster.storedscripts.DeleteStoredScriptRequest)2 GetStoredScriptRequest (org.opensearch.action.admin.cluster.storedscripts.GetStoredScriptRequest)2