Search in sources :

Example 1 with OpType

use of org.opensearch.action.DocWriteRequest.OpType in project OpenSearch by opensearch-project.

the class BulkItemResponse method fromXContent.

/**
 * Reads a {@link BulkItemResponse} from a {@link XContentParser}.
 *
 * @param parser the {@link XContentParser}
 * @param id the id to assign to the parsed {@link BulkItemResponse}. It is usually the index of
 *           the item in the {@link BulkResponse#getItems} array.
 */
public static BulkItemResponse fromXContent(XContentParser parser, int id) throws IOException {
    ensureExpectedToken(XContentParser.Token.START_OBJECT, parser.currentToken(), parser);
    XContentParser.Token token = parser.nextToken();
    ensureExpectedToken(XContentParser.Token.FIELD_NAME, token, parser);
    String currentFieldName = parser.currentName();
    token = parser.nextToken();
    final OpType opType = OpType.fromString(currentFieldName);
    ensureExpectedToken(XContentParser.Token.START_OBJECT, token, parser);
    DocWriteResponse.Builder builder = null;
    CheckedConsumer<XContentParser, IOException> itemParser = null;
    if (opType == OpType.INDEX || opType == OpType.CREATE) {
        final IndexResponse.Builder indexResponseBuilder = new IndexResponse.Builder();
        builder = indexResponseBuilder;
        itemParser = (indexParser) -> IndexResponse.parseXContentFields(indexParser, indexResponseBuilder);
    } else if (opType == OpType.UPDATE) {
        final UpdateResponse.Builder updateResponseBuilder = new UpdateResponse.Builder();
        builder = updateResponseBuilder;
        itemParser = (updateParser) -> UpdateResponse.parseXContentFields(updateParser, updateResponseBuilder);
    } else if (opType == OpType.DELETE) {
        final DeleteResponse.Builder deleteResponseBuilder = new DeleteResponse.Builder();
        builder = deleteResponseBuilder;
        itemParser = (deleteParser) -> DeleteResponse.parseXContentFields(deleteParser, deleteResponseBuilder);
    } else {
        throwUnknownField(currentFieldName, parser.getTokenLocation());
    }
    RestStatus status = null;
    OpenSearchException exception = null;
    while ((token = parser.nextToken()) != XContentParser.Token.END_OBJECT) {
        if (token == XContentParser.Token.FIELD_NAME) {
            currentFieldName = parser.currentName();
        }
        if (ERROR.equals(currentFieldName)) {
            if (token == XContentParser.Token.START_OBJECT) {
                exception = OpenSearchException.fromXContent(parser);
            }
        } else if (STATUS.equals(currentFieldName)) {
            if (token == XContentParser.Token.VALUE_NUMBER) {
                status = RestStatus.fromCode(parser.intValue());
            }
        } else {
            itemParser.accept(parser);
        }
    }
    ensureExpectedToken(XContentParser.Token.END_OBJECT, token, parser);
    token = parser.nextToken();
    ensureExpectedToken(XContentParser.Token.END_OBJECT, token, parser);
    BulkItemResponse bulkItemResponse;
    if (exception != null) {
        Failure failure = new Failure(builder.getShardId().getIndexName(), builder.getId(), exception, status);
        bulkItemResponse = new BulkItemResponse(id, opType, failure);
    } else {
        bulkItemResponse = new BulkItemResponse(id, opType, builder.build());
    }
    return bulkItemResponse;
}
Also used : SequenceNumbers(org.opensearch.index.seqno.SequenceNumbers) ConstructingObjectParser(org.opensearch.common.xcontent.ConstructingObjectParser) ToXContentFragment(org.opensearch.common.xcontent.ToXContentFragment) IndexResponse(org.opensearch.action.index.IndexResponse) UpdateResponse(org.opensearch.action.update.UpdateResponse) CheckedConsumer(org.opensearch.common.CheckedConsumer) Version(org.opensearch.Version) StreamOutput(org.opensearch.common.io.stream.StreamOutput) OpenSearchException(org.opensearch.OpenSearchException) ParseField(org.opensearch.common.ParseField) Writeable(org.opensearch.common.io.stream.Writeable) ConstructingObjectParser.constructorArg(org.opensearch.common.xcontent.ConstructingObjectParser.constructorArg) Strings(org.opensearch.common.Strings) XContentParser(org.opensearch.common.xcontent.XContentParser) MapperService(org.opensearch.index.mapper.MapperService) LegacyESVersion(org.opensearch.LegacyESVersion) DeleteResponse(org.opensearch.action.delete.DeleteResponse) ConstructingObjectParser.optionalConstructorArg(org.opensearch.common.xcontent.ConstructingObjectParser.optionalConstructorArg) StreamInput(org.opensearch.common.io.stream.StreamInput) ExceptionsHelper(org.opensearch.ExceptionsHelper) StatusToXContentObject(org.opensearch.common.xcontent.StatusToXContentObject) IOException(java.io.IOException) XContentParserUtils.ensureExpectedToken(org.opensearch.common.xcontent.XContentParserUtils.ensureExpectedToken) RestStatus(org.opensearch.rest.RestStatus) XContentBuilder(org.opensearch.common.xcontent.XContentBuilder) ShardId(org.opensearch.index.shard.ShardId) OpType(org.opensearch.action.DocWriteRequest.OpType) DocWriteResponse(org.opensearch.action.DocWriteResponse) XContentParserUtils.throwUnknownField(org.opensearch.common.xcontent.XContentParserUtils.throwUnknownField) DocWriteResponse(org.opensearch.action.DocWriteResponse) XContentBuilder(org.opensearch.common.xcontent.XContentBuilder) IOException(java.io.IOException) UpdateResponse(org.opensearch.action.update.UpdateResponse) DeleteResponse(org.opensearch.action.delete.DeleteResponse) RestStatus(org.opensearch.rest.RestStatus) IndexResponse(org.opensearch.action.index.IndexResponse) OpType(org.opensearch.action.DocWriteRequest.OpType) OpenSearchException(org.opensearch.OpenSearchException) XContentParser(org.opensearch.common.xcontent.XContentParser)

Aggregations

IOException (java.io.IOException)1 ExceptionsHelper (org.opensearch.ExceptionsHelper)1 LegacyESVersion (org.opensearch.LegacyESVersion)1 OpenSearchException (org.opensearch.OpenSearchException)1 Version (org.opensearch.Version)1 OpType (org.opensearch.action.DocWriteRequest.OpType)1 DocWriteResponse (org.opensearch.action.DocWriteResponse)1 DeleteResponse (org.opensearch.action.delete.DeleteResponse)1 IndexResponse (org.opensearch.action.index.IndexResponse)1 UpdateResponse (org.opensearch.action.update.UpdateResponse)1 CheckedConsumer (org.opensearch.common.CheckedConsumer)1 ParseField (org.opensearch.common.ParseField)1 Strings (org.opensearch.common.Strings)1 StreamInput (org.opensearch.common.io.stream.StreamInput)1 StreamOutput (org.opensearch.common.io.stream.StreamOutput)1 Writeable (org.opensearch.common.io.stream.Writeable)1 ConstructingObjectParser (org.opensearch.common.xcontent.ConstructingObjectParser)1 ConstructingObjectParser.constructorArg (org.opensearch.common.xcontent.ConstructingObjectParser.constructorArg)1 ConstructingObjectParser.optionalConstructorArg (org.opensearch.common.xcontent.ConstructingObjectParser.optionalConstructorArg)1 StatusToXContentObject (org.opensearch.common.xcontent.StatusToXContentObject)1