@Override public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException { boolean includeDefaults = params.paramAsBoolean("include_defaults", false); if (!includeDefaults) { // simulate the generation to make sure we don't add unnecessary content if all is default // if all are defaults, no need to write it at all - generating is twice is ok though BytesStreamOutput bytesStreamOutput = new BytesStreamOutput(0); XContentBuilder b = new XContentBuilder(builder.contentType().xContent(), bytesStreamOutput); b.startObject().flush(); long pos = bytesStreamOutput.position(); innerToXContent(b, false); b.flush(); if (pos == bytesStreamOutput.position()) { return builder; } } builder.startObject(CONTENT_TYPE); innerToXContent(builder, includeDefaults); builder.endObject(); return builder; }
private static GeoPoint parseGeoPointString(String value) { try { XContentBuilder content = JsonXContent.contentBuilder(); content.value(value); content.flush(); content.close(); try (XContentParser parser = JsonXContent.jsonXContent.createParser(NamedXContentRegistry.EMPTY, THROW_UNSUPPORTED_OPERATION, ((ByteArrayOutputStream) content.getOutputStream()).toByteArray())) { parser.nextToken(); return GeoUtils.parseGeoPoint(parser); } } catch (IOException e) { throw new IllegalArgumentException("Invalid value for geopoint: " + e.getMessage()); } }
@Override public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException { boolean includeDefaults = params.paramAsBoolean("include_defaults", false); if (!includeDefaults) { // simulate the generation to make sure we don't add unnecessary content if all is default // if all are defaults, no need to write it at all - generating is twice is ok though BytesStreamOutput bytesStreamOutput = new BytesStreamOutput(0); XContentBuilder b = new XContentBuilder(builder.contentType().xContent(), bytesStreamOutput); b.startObject().flush(); long pos = bytesStreamOutput.position(); innerToXContent(b, false); b.flush(); if (pos == bytesStreamOutput.position()) { return builder; } } builder.startObject(CONTENT_TYPE); innerToXContent(builder, includeDefaults); builder.endObject(); return builder; }
@Override public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException { boolean includeDefaults = params.paramAsBoolean("include_defaults", false); if (!includeDefaults) { // simulate the generation to make sure we don't add unnecessary content if all is default // if all are defaults, no need to write it at all - generating is twice is ok though BytesStreamOutput bytesStreamOutput = new BytesStreamOutput(0); XContentBuilder b = new XContentBuilder(builder.contentType().xContent(), bytesStreamOutput); b.startObject().flush(); long pos = bytesStreamOutput.position(); innerToXContent(b, false); b.flush(); if (pos == bytesStreamOutput.position()) { return builder; } } builder.startObject(CONTENT_TYPE); innerToXContent(builder, includeDefaults); builder.endObject(); return builder; }
@Override public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException { boolean includeDefaults = params.paramAsBoolean("include_defaults", false); if (!includeDefaults) { // simulate the generation to make sure we don't add unnecessary content if all is default // if all are defaults, no need to write it at all - generating is twice is ok though BytesStreamOutput bytesStreamOutput = new BytesStreamOutput(0); XContentBuilder b = new XContentBuilder(builder.contentType().xContent(), bytesStreamOutput); b.startObject().flush(); long pos = bytesStreamOutput.position(); innerToXContent(b, false); b.flush(); if (pos == bytesStreamOutput.position()) { return builder; } } builder.startObject(CONTENT_TYPE); innerToXContent(builder, includeDefaults); builder.endObject(); return builder; }
@Override public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException { boolean includeDefaults = params.paramAsBoolean("include_defaults", false); if (!includeDefaults) { // simulate the generation to make sure we don't add unnecessary content if all is default // if all are defaults, no need to write it at all - generating is twice is ok though BytesStreamOutput bytesStreamOutput = new BytesStreamOutput(0); XContentBuilder b = new XContentBuilder(builder.contentType().xContent(), bytesStreamOutput); b.startObject().flush(); long pos = bytesStreamOutput.position(); innerToXContent(b, false); b.flush(); if (pos == bytesStreamOutput.position()) { return builder; } } builder.startObject(CONTENT_TYPE); innerToXContent(builder, includeDefaults); builder.endObject(); return builder; }
static void createQueryBuilderField(Version indexVersion, BinaryFieldMapper qbField, QueryBuilder queryBuilder, ParseContext context) throws IOException { if (indexVersion.onOrAfter(Version.V_6_0_0_beta2)) { try (ByteArrayOutputStream stream = new ByteArrayOutputStream()) { try (OutputStreamStreamOutput out = new OutputStreamStreamOutput(stream)) { out.setVersion(indexVersion); out.writeNamedWriteable(queryBuilder); byte[] queryBuilderAsBytes = stream.toByteArray(); qbField.parse(context.createExternalValueContext(queryBuilderAsBytes)); } } } else { try (XContentBuilder builder = XContentFactory.contentBuilder(QUERY_BUILDER_CONTENT_TYPE)) { queryBuilder.toXContent(builder, new MapParams(Collections.emptyMap())); builder.flush(); byte[] queryBuilderAsBytes = BytesReference.toBytes(BytesReference.bytes(builder)); context.doc().add(new Field(qbField.name(), queryBuilderAsBytes, qbField.fieldType())); } } }
static void createQueryBuilderField(Version indexVersion, BinaryFieldMapper qbField, QueryBuilder queryBuilder, ParseContext context) throws IOException { if (indexVersion.onOrAfter(Version.V_6_0_0_beta2)) { try (ByteArrayOutputStream stream = new ByteArrayOutputStream()) { try (OutputStreamStreamOutput out = new OutputStreamStreamOutput(stream)) { out.setVersion(indexVersion); out.writeNamedWriteable(queryBuilder); byte[] queryBuilderAsBytes = stream.toByteArray(); qbField.parse(context.createExternalValueContext(queryBuilderAsBytes)); } } } else { try (XContentBuilder builder = XContentFactory.contentBuilder(QUERY_BUILDER_CONTENT_TYPE)) { queryBuilder.toXContent(builder, new MapParams(Collections.emptyMap())); builder.flush(); byte[] queryBuilderAsBytes = BytesReference.toBytes(BytesReference.bytes(builder)); context.doc().add(new Field(qbField.name(), queryBuilderAsBytes, qbField.fieldType())); } } }
XContentBuilder builder = new XContentBuilder(XContentFactory.xContent(XContentType.JSON), os); exportFields.toXContent(builder, ToXContent.EMPTY_PARAMS); builder.flush(); BytesReference bytes = os.bytes(); out.write(bytes.array(), bytes.arrayOffset(), bytes.length());
try (XContentBuilder builder = XContentFactory.jsonBuilder()) { builder.copyCurrentStructure(parser); builder.flush(); documents.add(BytesReference.bytes(builder)); try (XContentBuilder builder = XContentFactory.jsonBuilder()) { builder.copyCurrentStructure(parser); builder.flush(); documents.add(BytesReference.bytes(builder));
try (XContentBuilder builder = XContentFactory.jsonBuilder()) { builder.copyCurrentStructure(parser); builder.flush(); documents.add(BytesReference.bytes(builder)); try (XContentBuilder builder = XContentFactory.jsonBuilder()) { builder.copyCurrentStructure(parser); builder.flush(); documents.add(BytesReference.bytes(builder));