@Override public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException { final Stream<ToXContent> stream = Arrays.stream(new ToXContent[] { docs, store, indexing, get, search, merge, refresh, flush, warmer, queryCache, fieldData, completion, segments, translog, requestCache, recoveryStats}) .filter(Objects::nonNull); for (ToXContent toXContent : ((Iterable<ToXContent>)stream::iterator)) { toXContent.toXContent(builder, params); } return builder; } }
/** * 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); } }
protected void write(T obj, StreamOutput streamOutput) throws IOException { try (XContentBuilder builder = XContentFactory.contentBuilder(xContentType, streamOutput)) { builder.startObject(); obj.toXContent(builder, SNAPSHOT_ONLY_FORMAT_PARAMS); builder.endObject(); } } }
/** * Return a {@link String} that is the json representation of the provided {@link ToXContent}. * Wraps the output into an anonymous object if needed. Allows to control whether the outputted * json needs to be pretty printed and human readable. * */ public static String toString(ToXContent toXContent, boolean pretty, boolean human) { try { XContentBuilder builder = createBuilder(pretty, human); if (toXContent.isFragment()) { builder.startObject(); } toXContent.toXContent(builder, ToXContent.EMPTY_PARAMS); if (toXContent.isFragment()) { builder.endObject(); } return toString(builder); } catch (IOException e) { try { XContentBuilder builder = createBuilder(pretty, human); builder.startObject(); builder.field("error", "error building toString out of XContent: " + e.getMessage()); builder.field("stack_trace", ExceptionsHelper.stackTrace(e)); builder.endObject(); return toString(builder); } catch (IOException e2) { throw new ElasticsearchException("cannot generate error message for deserialization", e); } } }
/** * Sets query contexts for completion * @param queryContexts named query contexts * see {@link org.elasticsearch.search.suggest.completion.context.CategoryQueryContext} * and {@link org.elasticsearch.search.suggest.completion.context.GeoQueryContext} */ public CompletionSuggestionBuilder contexts(Map<String, List<? extends ToXContent>> queryContexts) { Objects.requireNonNull(queryContexts, "contexts must not be null"); try { XContentBuilder contentBuilder = XContentFactory.contentBuilder(CONTEXT_BYTES_XCONTENT_TYPE); contentBuilder.startObject(); for (Map.Entry<String, List<? extends ToXContent>> contextEntry : queryContexts.entrySet()) { contentBuilder.startArray(contextEntry.getKey()); for (ToXContent queryContext : contextEntry.getValue()) { queryContext.toXContent(contentBuilder, EMPTY_PARAMS); } contentBuilder.endArray(); } contentBuilder.endObject(); return contexts(contentBuilder); } catch (IOException e) { throw new IllegalArgumentException(e); } }
/** * Return a {@link String} that is the json representation of the provided {@link ToXContent}. * Wraps the output into an anonymous object. */ public static String toString(ToXContent toXContent) { try { XContentBuilder builder = JsonXContent.contentBuilder(); if (toXContent.isFragment()) { builder.startObject(); } toXContent.toXContent(builder, ToXContent.EMPTY_PARAMS); if (toXContent.isFragment()) { builder.endObject(); } return builder.string(); } catch (IOException e) { return "Error building toString out of XContent: " + ExceptionsHelper.stackTrace(e); } }
/** * Create a {@link CompressedXContent} out of a {@link ToXContent} instance. */ public CompressedXContent(ToXContent xcontent, XContentType type, ToXContent.Params params) throws IOException { BytesStreamOutput bStream = new BytesStreamOutput(); OutputStream compressedStream = CompressorFactory.COMPRESSOR.streamOutput(bStream); CRC32 crc32 = new CRC32(); OutputStream checkedStream = new CheckedOutputStream(compressedStream, crc32); try (XContentBuilder builder = XContentFactory.contentBuilder(type, checkedStream)) { builder.startObject(); xcontent.toXContent(builder, params); builder.endObject(); } this.bytes = BytesReference.toBytes(bStream.bytes()); this.crc32 = (int) crc32.getValue(); assertConsistent(); }
/** * 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); } }
/** * Automatically transform the {@link ToXContent}-compatible, nodes-level {@code response} into a a {@link BytesRestResponse}. * <p> * This looks like: * <code> * { * "_nodes" : { ... }, * "cluster_name" : "...", * ... * } * </code> * * @param builder XContent builder. * @param params XContent parameters. * @param response The nodes-level (plural) response. * @return Never {@code null}. * @throws IOException if building the response causes an issue */ public static <NodesResponse extends BaseNodesResponse & ToXContent> BytesRestResponse nodesResponse(final XContentBuilder builder, final Params params, final NodesResponse response) throws IOException { builder.startObject(); RestActions.buildNodesHeader(builder, params, response); builder.field("cluster_name", response.getClusterName().value()); response.toXContent(builder, params); builder.endObject(); return new BytesRestResponse(RestStatus.OK, builder); }
/** * 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); } }
custom.toXContent(builder, params);
/** * 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 builder.bytes(); } } }
@Override protected XContentBuilder toXContent(Request request, Response response, XContentBuilder builder) throws IOException { return response.toXContent(builder, ToXContent.EMPTY_PARAMS); } }
/** * Return a {@link String} that is the json representation of the provided {@link ToXContent}. * Wraps the output into an anonymous object if needed. Allows to control whether the outputted * json needs to be pretty printed and human readable. * */ public static String toString(ToXContent toXContent, boolean pretty, boolean human) { try { XContentBuilder builder = createBuilder(pretty, human); if (toXContent.isFragment()) { builder.startObject(); } toXContent.toXContent(builder, ToXContent.EMPTY_PARAMS); if (toXContent.isFragment()) { builder.endObject(); } return toString(builder); } catch (IOException e) { try { XContentBuilder builder = createBuilder(pretty, human); builder.startObject(); builder.field("error", "error building toString out of XContent: " + e.getMessage()); builder.field("stack_trace", ExceptionsHelper.stackTrace(e)); builder.endObject(); return toString(builder); } catch (IOException e2) { throw new ElasticsearchException("cannot generate error message for deserialization", e); } } }
@Override public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException { final Stream<ToXContent> stream = Arrays.stream(new ToXContent[] { docs, store, indexing, get, search, merge, refresh, flush, warmer, queryCache, fieldData, completion, segments, translog, requestCache, recoveryStats}) .filter(Objects::nonNull); for (ToXContent toXContent : ((Iterable<ToXContent>)stream::iterator)) { toXContent.toXContent(builder, params); } return builder; } }
/** * Return a {@link String} that is the json representation of the provided {@link ToXContent}. * Wraps the output into an anonymous object if needed. Allows to control whether the outputted * json needs to be pretty printed and human readable. * */ public static String toString(ToXContent toXContent, boolean pretty, boolean human) { try { XContentBuilder builder = createBuilder(pretty, human); if (toXContent.isFragment()) { builder.startObject(); } toXContent.toXContent(builder, ToXContent.EMPTY_PARAMS); if (toXContent.isFragment()) { builder.endObject(); } return toString(builder); } catch (IOException e) { try { XContentBuilder builder = createBuilder(pretty, human); builder.startObject(); builder.field("error", "error building toString out of XContent: " + e.getMessage()); builder.field("stack_trace", ExceptionsHelper.stackTrace(e)); builder.endObject(); return toString(builder); } catch (IOException e2) { throw new ElasticsearchException("cannot generate error message for deserialization", e); } } }
@Override public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException { final Stream<ToXContent> stream = Arrays.stream(new ToXContent[] { docs, store, indexing, get, search, merge, refresh, flush, warmer, queryCache, fieldData, completion, segments, translog, requestCache, recoveryStats}) .filter(Objects::nonNull); for (ToXContent toXContent : ((Iterable<ToXContent>)stream::iterator)) { toXContent.toXContent(builder, params); } return builder; } }
@Override public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException { final Stream<ToXContent> stream = Arrays.stream(new ToXContent[] { docs, store, indexing, get, search, merge, refresh, flush, warmer, queryCache, fieldData, completion, segments, translog, requestCache, recoveryStats}) .filter(Objects::nonNull); for (ToXContent toXContent : ((Iterable<ToXContent>)stream::iterator)) { toXContent.toXContent(builder, params); } return builder; } }
protected void write(T obj, StreamOutput streamOutput) throws IOException { try (XContentBuilder builder = XContentFactory.contentBuilder(xContentType, streamOutput)) { builder.startObject(); obj.toXContent(builder, SNAPSHOT_ONLY_FORMAT_PARAMS); builder.endObject(); } } }
public XContentBuilder field(String name, ToXContent xContent) throws IOException { field(name); xContent.toXContent(this, ToXContent.EMPTY_PARAMS); return this; }