DocWriteRequest.OpType opType = action.opType(); try (XContentBuilder metadata = XContentBuilder.builder(bulkContentType.xContent())) { metadata.startObject(); try (XContentBuilder builder = XContentBuilder.builder(bulkContentType.xContent())) { builder.copyCurrentStructure(parser); source = BytesReference.bytes(builder).toBytesRef();
@Override public String toString() { try (XContentBuilder builder = XContentBuilder.builder(XContentType.JSON.xContent())) { builder.startObject(); toXContent(builder, new MapParams(Collections.singletonMap("flat_settings", "true"))); builder.endObject(); return Strings.toString(builder); } catch (IOException e) { throw new UncheckedIOException(e); } }
public static byte[] writeMultiLineFormat(MultiSearchRequest multiSearchRequest, XContent xContent) throws IOException { ByteArrayOutputStream output = new ByteArrayOutputStream(); for (SearchRequest request : multiSearchRequest.requests()) { try (XContentBuilder xContentBuilder = XContentBuilder.builder(xContent)) { writeSearchRequestParams(request, xContentBuilder); BytesReference.bytes(xContentBuilder).writeTo(output); } output.write(xContent.streamSeparator()); try (XContentBuilder xContentBuilder = XContentBuilder.builder(xContent)) { if (request.source() != null) { request.source().toXContent(xContentBuilder, ToXContent.EMPTY_PARAMS); } else { xContentBuilder.startObject(); xContentBuilder.endObject(); } BytesReference.bytes(xContentBuilder).writeTo(output); } output.write(xContent.streamSeparator()); } return output.toByteArray(); }
private static BytesReference parseSourceBytes(XContentParser parser) throws IOException { try (XContentBuilder builder = XContentBuilder.builder(parser.contentType().xContent())) { // the original document gets slightly modified: whitespaces or // pretty printing are not preserved, // it all depends on the current builder settings builder.copyCurrentStructure(parser); return BytesReference.bytes(builder); } }
builder.append(System.lineSeparator()); builder.append("curl -XPUT 'http://localhost:9200/_all/_settings?preserve_existing=true' -d '"); try (XContentBuilder xContentBuilder = XContentBuilder.builder(XContentType.JSON.xContent())) { xContentBuilder.prettyPrint(); xContentBuilder.startObject();
/** * Returns the bytes that represent the XContent output of the provided {@link ToXContent} object, using the provided * {@link XContentType}. Wraps the output into a new anonymous object according to the value returned * by the {@link ToXContent#isFragment()} method returns. */ public static BytesReference toXContent(ToXContent toXContent, XContentType xContentType, Params params, boolean humanReadable) throws IOException { try (XContentBuilder builder = XContentBuilder.builder(xContentType.xContent())) { builder.humanReadable(humanReadable); if (toXContent.isFragment()) { builder.startObject(); } toXContent.toXContent(builder, params); if (toXContent.isFragment()) { builder.endObject(); } return BytesReference.bytes(builder); } }
private static Script extractConditional(Map<String, Object> config) throws IOException { Object scriptSource = config.remove("if"); if (scriptSource != null) { try (XContentBuilder builder = XContentBuilder.builder(JsonXContent.jsonXContent) .map(normalizeScript(scriptSource)); InputStream stream = BytesReference.bytes(builder).streamInput(); XContentParser parser = XContentType.JSON.xContent().createParser(NamedXContentRegistry.EMPTY, LoggingDeprecationHandler.INSTANCE, stream)) { return Script.parse(parser); } } return null; }
private static String arrayToParsableString(List<String> array) { try { XContentBuilder builder = XContentBuilder.builder(XContentType.JSON.xContent()); builder.startArray(); for (String element : array) { builder.value(element); } builder.endArray(); return Strings.toString(builder); } catch (IOException ex) { throw new ElasticsearchException(ex); } }
try (XContentBuilder builder = XContentBuilder.builder(parser.contentType().xContent())) {
public static XContentBuilder contentBuilder() throws IOException { return XContentBuilder.builder(jsonXContent); } private static final JsonFactory jsonFactory;
/** * * @param parser the parser for the XContent that contains the serialized GetPipelineResponse. * @return an instance of GetPipelineResponse read from the parser * @throws IOException If the parsing fails */ public static GetPipelineResponse fromXContent(XContentParser parser) throws IOException { ensureExpectedToken(XContentParser.Token.START_OBJECT, parser.nextToken(), parser::getTokenLocation); List<PipelineConfiguration> pipelines = new ArrayList<>(); while(parser.nextToken().equals(Token.FIELD_NAME)) { String pipelineId = parser.currentName(); parser.nextToken(); XContentBuilder contentBuilder = XContentBuilder.builder(parser.contentType().xContent()); contentBuilder.generator().copyCurrentStructure(parser); PipelineConfiguration pipeline = new PipelineConfiguration( pipelineId, BytesReference.bytes(contentBuilder), contentBuilder.contentType() ); pipelines.add(pipeline); } ensureExpectedToken(XContentParser.Token.END_OBJECT, parser.currentToken(), parser::getTokenLocation); return new GetPipelineResponse(pipelines); }
@Override public String toString() { try (XContentBuilder builder = XContentBuilder.builder(XContentType.JSON.xContent())) { builder.startObject(); toXContent(builder, new MapParams(Collections.singletonMap("flat_settings", "true"))); builder.endObject(); return Strings.toString(builder); } catch (IOException e) { throw new UncheckedIOException(e); } }
protected XContentBuilder buildSource(XContent content, Map<String, Object> map) { try { // Enforce the content type to be CBOR as it is more efficient for large byte arrays return XContentBuilder.builder(XContentType.CBOR.xContent()).map(map); } catch (IOException e) { logger.error("failed to build source", e); throw new IllegalStateException("Failed to build source", e); } }
private XContentBuilder buildQuery(Map query) { try { if (query == null) { return null; } return XContentBuilder.builder(XContentType.CBOR.xContent()).map(query); } catch (IOException e) { throw new ElasticsearchParseException("Unable to build the lookup query"); } }
private static BytesReference parseSourceBytes(XContentParser parser) throws IOException { try (XContentBuilder builder = XContentBuilder.builder(parser.contentType().xContent())) { // the original document gets slightly modified: whitespaces or // pretty printing are not preserved, // it all depends on the current builder settings builder.copyCurrentStructure(parser); return builder.bytes(); } }
private static BytesReference parseSourceBytes(XContentParser parser) throws IOException { try (XContentBuilder builder = XContentBuilder.builder(parser.contentType().xContent())) { // the original document gets slightly modified: whitespaces or // pretty printing are not preserved, // it all depends on the current builder settings builder.copyCurrentStructure(parser); return BytesReference.bytes(builder); } }
private static BytesReference parseSourceBytes(XContentParser parser) throws IOException { try (XContentBuilder builder = XContentBuilder.builder(parser.contentType().xContent())) { // the original document gets slightly modified: whitespaces or // pretty printing are not preserved, // it all depends on the current builder settings builder.copyCurrentStructure(parser); return BytesReference.bytes(builder); } }
private static Script extractConditional(Map<String, Object> config) throws IOException { Object scriptSource = config.remove("if"); if (scriptSource != null) { try (XContentBuilder builder = XContentBuilder.builder(JsonXContent.jsonXContent) .map(normalizeScript(scriptSource)); InputStream stream = BytesReference.bytes(builder).streamInput(); XContentParser parser = XContentType.JSON.xContent().createParser(NamedXContentRegistry.EMPTY, LoggingDeprecationHandler.INSTANCE, stream)) { return Script.parse(parser); } } return null; }
private static String arrayToParsableString(String[] array) { try { XContentBuilder builder = XContentBuilder.builder(XContentType.JSON.xContent()); builder.startArray(); for (String element : array) { builder.value(element); } builder.endArray(); return builder.string(); } catch (IOException ex) { throw new ElasticsearchException(ex); } }
private static String arrayToParsableString(List<String> array) { try { XContentBuilder builder = XContentBuilder.builder(XContentType.JSON.xContent()); builder.startArray(); for (String element : array) { builder.value(element); } builder.endArray(); return Strings.toString(builder); } catch (IOException ex) { throw new ElasticsearchException(ex); } }