Java Code Examples for org.elasticsearch.common.xcontent.ToXContent#toXContent()
The following examples show how to use
org.elasticsearch.common.xcontent.ToXContent#toXContent() .
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: ElasticSearchServiceMapper.java From vertx-elasticsearch-service with Apache License 2.0 | 6 votes |
protected static JsonObject readResponse(ToXContent toXContent) { try { final XContentBuilder builder = XContentFactory.jsonBuilder(); if (toXContent.isFragment()) { builder.startObject(); toXContent.toXContent(builder, SearchResponse.EMPTY_PARAMS); builder.endObject(); } else { toXContent.toXContent(builder, SearchResponse.EMPTY_PARAMS); } return new JsonObject(builder.string()); } catch (IOException e) { throw new RuntimeException(e); } }
Example 2
Source File: CompressedXContent.java From crate with Apache License 2.0 | 6 votes |
/** * 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(); try (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(); }
Example 3
Source File: AbstractApiAction.java From deprecated-security-advanced-modules with Apache License 2.0 | 5 votes |
protected static XContentBuilder convertToJson(RestChannel channel, ToXContent toxContent) { try { XContentBuilder builder = channel.newBuilder(); toxContent.toXContent(builder, ToXContent.EMPTY_PARAMS); return builder; } catch (IOException e) { throw ExceptionsHelper.convertToElastic(e); } }
Example 4
Source File: CompressedXContent.java From Elasticsearch with Apache License 2.0 | 5 votes |
/** * 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.defaultCompressor().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 = bStream.bytes().toBytes(); this.crc32 = (int) crc32.getValue(); assertConsistent(); }
Example 5
Source File: Strings.java From Elasticsearch with Apache License 2.0 | 5 votes |
/** * Return a {@link String} that is the json representation of the provided * {@link ToXContent}. */ public static String toString(ToXContent toXContent) { try { XContentBuilder builder = JsonXContent.contentBuilder(); toXContent.toXContent(builder, ToXContent.EMPTY_PARAMS); return builder.string(); } catch (IOException e) { throw new AssertionError("Cannot happen", e); } }
Example 6
Source File: TestUtils.java From openshift-elasticsearch-plugin with Apache License 2.0 | 5 votes |
public static void assertYaml(String message, String exp, ToXContent content) throws Exception { XContentBuilder builder = XContentFactory.yamlBuilder(); builder.startObject(); content.toXContent(builder, ToXContent.EMPTY_PARAMS); builder.endObject(); Map<String, Object> act = XContentHelper.convertToMap(builder.bytes(), true, XContentType.YAML).v2(); assertEquals(message, exp, new Yaml().dump(new TreeMap<>(act))); }
Example 7
Source File: AbstractRestActionTest.java From elasticsearch-inout-plugin with Apache License 2.0 | 5 votes |
/** * Convert an XContent object to a Java map * @param toXContent * @return * @throws IOException */ public static Map<String, Object> toMap(ToXContent toXContent) throws IOException { XContentBuilder builder = XContentFactory.contentBuilder(XContentType.JSON); toXContent.toXContent(builder, ToXContent.EMPTY_PARAMS); return XContentFactory.xContent(XContentType.JSON).createParser( builder.string()).mapOrderedAndClose(); }
Example 8
Source File: ToXContentAsString.java From elasticshell with Apache License 2.0 | 5 votes |
public String asString(ToXContent toXContent) throws IOException { XContentBuilder builder = XContentFactory.jsonBuilder().prettyPrint(); try { toXContent.toXContent(builder, ToXContent.EMPTY_PARAMS); } catch (IOException e) { //hack: the first object in the builder might need to be opened, depending on the ToXContent implementation //Let's just try again, hopefully it'll work builder.startObject(); toXContent.toXContent(builder, ToXContent.EMPTY_PARAMS); builder.endObject(); } return builder.string(); }
Example 9
Source File: XContentTestUtils.java From crate with Apache License 2.0 | 5 votes |
public static Map<String, Object> convertToMap(ToXContent part) throws IOException { XContentBuilder builder = XContentFactory.jsonBuilder(); builder.startObject(); part.toXContent(builder, EMPTY_PARAMS); builder.endObject(); return XContentHelper.convertToMap(BytesReference.bytes(builder), false, builder.contentType()).v2(); }
Example 10
Source File: ObjectMapper.java From Elasticsearch with Apache License 2.0 | 4 votes |
public void toXContent(XContentBuilder builder, Params params, ToXContent custom) throws IOException { builder.startObject(simpleName()); if (nested.isNested()) { builder.field("type", NESTED_CONTENT_TYPE); if (nested.isIncludeInParent()) { builder.field("include_in_parent", true); } if (nested.isIncludeInRoot()) { builder.field("include_in_root", true); } } else if (mappers.isEmpty() && custom == null) { // only write the object content type if there are no properties, otherwise, it is automatically detected builder.field("type", CONTENT_TYPE); } if (dynamic != null) { builder.field("dynamic", dynamic.name().toLowerCase(Locale.ROOT)); } if (enabled != Defaults.ENABLED) { builder.field("enabled", enabled); } if (pathType != Defaults.PATH_TYPE) { builder.field("path", pathType.name().toLowerCase(Locale.ROOT)); } if (includeInAll != null) { builder.field("include_in_all", includeInAll); } if (custom != null) { custom.toXContent(builder, params); } doXContent(builder, params); // sort the mappers so we get consistent serialization format Mapper[] sortedMappers = Iterables.toArray(mappers.values(), Mapper.class); Arrays.sort(sortedMappers, new Comparator<Mapper>() { @Override public int compare(Mapper o1, Mapper o2) { return o1.name().compareTo(o2.name()); } }); int count = 0; for (Mapper mapper : sortedMappers) { if (!(mapper instanceof MetadataFieldMapper)) { if (count++ == 0) { builder.startObject("properties"); } mapper.toXContent(builder, params); } } if (count > 0) { builder.endObject(); } builder.endObject(); }
Example 11
Source File: OpenShiftRestResponseTest.java From openshift-elasticsearch-plugin with Apache License 2.0 | 4 votes |
private OpenShiftRestResponse whenCreatingResponseResponse(ToXContent content) throws Exception { RestResponse response = new BytesRestResponse(RestStatus.CREATED, content.toXContent(XContentBuilder.builder(XCONTENT), ToXContent.EMPTY_PARAMS)); return new OpenShiftRestResponse(response, CONTEXT, DEFAULT_USER_PROFILE_PREFIX); }
Example 12
Source File: ObjectMapper.java From crate with Apache License 2.0 | 4 votes |
public void toXContent(XContentBuilder builder, Params params, ToXContent custom) throws IOException { builder.startObject(simpleName()); if (mappers.isEmpty() && custom == null) { // only write the object content type if there are no properties, otherwise, it is automatically detected builder.field("type", CONTENT_TYPE); } if (position != null) { builder.field("position", position); } if (dynamic != null) { builder.field("dynamic", dynamic.name().toLowerCase(Locale.ROOT)); } if (enabled != Defaults.ENABLED) { builder.field("enabled", enabled); } if (custom != null) { custom.toXContent(builder, params); } doXContent(builder, params); // sort the mappers so we get consistent serialization format Mapper[] sortedMappers = mappers.values().stream().toArray(size -> new Mapper[size]); Arrays.sort(sortedMappers, new Comparator<Mapper>() { @Override public int compare(Mapper o1, Mapper o2) { return o1.name().compareTo(o2.name()); } }); int count = 0; for (Mapper mapper : sortedMappers) { if (!(mapper instanceof MetadataFieldMapper)) { if (count++ == 0) { builder.startObject("properties"); } mapper.toXContent(builder, params); } } if (count > 0) { builder.endObject(); } builder.endObject(); }