Source:
protected IndexItemResponse insert(ShardUpsertRequest request, ShardUpsertRequest.Item item, IndexShard indexShard, boolean isRetry, @Nullable ReturnValueGen returnGen, @Nullable InsertSourceGen insertSourceGen) throws Exception { assert insertSourceGen != null : "InsertSourceGen must not be null"; final BytesReference rawSource; Map<String, Object> source = null; try { if (insertSourceGen instanceof FromRawInsertSource) { rawSource = insertSourceGen.generateSourceAndCheckConstraintsAsBytesReference(item.insertValues()); } else { source = insertSourceGen.generateSourceAndCheckConstraints(item.insertValues()); rawSource = insertSourceGen.toBytesReference(source); } } catch (IOException e) { throw ExceptionsHelper.convertToElastic(e); } item.source(rawSource); final long version = request.duplicateKeyAction() == DuplicateKeyAction.OVERWRITE ? Versions.MATCH_ANY : Versions.MATCH_DELETED; final long seqNo = SequenceNumbers.UNASSIGNED_SEQ_NO; final long primaryTerm = SequenceNumbers.UNASSIGNED_PRIMARY_TERM; Engine.IndexResult indexResult = index(item, indexShard, isRetry, seqNo, primaryTerm, version); Object[] returnvalues = null; if (returnGen != null) { if (source == null) { source = JsonXContent.jsonXContent.createParser( NamedXContentRegistry.EMPTY, DeprecationHandler.THROW_UNSUPPORTED_OPERATION, BytesReference.toBytes(rawSource)).map(); } returnvalues = returnGen.generateReturnValues( new Doc(-1, indexShard.shardId().getIndexName(), item.id(), indexResult.getVersion(), indexResult.getSeqNo(), primaryTerm, source, () -> "" ) ); } return new IndexItemResponse(indexResult.getTranslogLocation(), returnvalues); }

Target:
protected IndexItemResponse insert(ShardUpsertRequest request, ShardUpsertRequest.Item item, IndexShard indexShard, boolean isRetry, @Nullable ReturnValueGen returnGen, @Nullable InsertSourceGen insertSourceGen) throws Exception { assert insertSourceGen != null : "InsertSourceGen must not be null"; BytesReference rawSource; Map<String, Object> source = null; try { if (insertSourceGen instanceof FromRawInsertSource) { rawSource = insertSourceGen.generateSourceAndCheckConstraintsAsBytesReference(item.insertValues()); } else { source = insertSourceGen.generateSourceAndCheckConstraints(item.insertValues()); rawSource = BytesReference.bytes(XContentFactory.jsonBuilder().map(source)); } } catch (IOException e) { throw ExceptionsHelper.convertToElastic(e); } item.source(rawSource); long version = request.duplicateKeyAction() == DuplicateKeyAction.OVERWRITE ? Versions.MATCH_ANY : Versions.MATCH_DELETED; long seqNo = SequenceNumbers.UNASSIGNED_SEQ_NO; long primaryTerm = SequenceNumbers.UNASSIGNED_PRIMARY_TERM; Engine.IndexResult indexResult = index(item, indexShard, isRetry, seqNo, primaryTerm, version); Object[] returnvalues = null; if (returnGen != null) { if (source == null) { source = JsonXContent.jsonXContent.createParser( NamedXContentRegistry.EMPTY, DeprecationHandler.THROW_UNSUPPORTED_OPERATION, BytesReference.toBytes(rawSource)).map(); } returnvalues = returnGen.generateReturnValues( new Doc( -1, indexShard.shardId().getIndexName(), item.id(), indexResult.getVersion(), indexResult.getSeqNo(), primaryTerm, source, () -> "" ) ); } return new IndexItemResponse(indexResult.getTranslogLocation(), returnvalues); }