Class XContentBuilder
- java.lang.Object
-
- org.elasticsearch.common.xcontent.XContentBuilder
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static interfaceXContentBuilder.HumanReadableTransformerInterface for transforming complex objects into their "raw" equivalents for human-readable fieldsstatic interfaceXContentBuilder.Writer
-
Constructor Summary
Constructors Constructor Description XContentBuilder(XContent xContent, OutputStream bos)Constructs a new builder using the provided XContent and an OutputStream.XContentBuilder(XContent xContent, OutputStream bos, Set<String> includes)Constructs a new builder using the provided XContent, an OutputStream and some filters.XContentBuilder(XContent xContent, OutputStream os, Set<String> includes, Set<String> excludes)Creates a new builder using the provided XContent, output stream and some inclusive and/or exclusive filters.
-
Method Summary
-
-
-
Constructor Detail
-
XContentBuilder
public XContentBuilder(XContent xContent, OutputStream bos) throws IOException
Constructs a new builder using the provided XContent and an OutputStream. Make sure to callclose()when the builder is done with.- Throws:
IOException
-
XContentBuilder
public XContentBuilder(XContent xContent, OutputStream bos, Set<String> includes) throws IOException
Constructs a new builder using the provided XContent, an OutputStream and some filters. If filters are specified, only those values matching a filter will be written to the output stream. Make sure to callclose()when the builder is done with.- Throws:
IOException
-
XContentBuilder
public XContentBuilder(XContent xContent, OutputStream os, Set<String> includes, Set<String> excludes) throws IOException
Creates a new builder using the provided XContent, output stream and some inclusive and/or exclusive filters. When both exclusive and inclusive filters are provided, the underlying builder will first use exclusion filters to remove fields and then will check the remaining fields against the inclusive filters.Make sure to call
close()when the builder is done with.- Parameters:
os- the output streamincludes- the inclusive filters: only fields and objects that match the inclusive filters will be written to the output.excludes- the exclusive filters: only fields and objects that don't match the exclusive filters will be written to the output.- Throws:
IOException
-
-
Method Detail
-
builder
public static XContentBuilder builder(XContent xContent) throws IOException
Create a newXContentBuilderusing the givenXContentcontent.The builder uses an internal
ByteArrayOutputStreamoutput stream to build the content.- Parameters:
xContent- theXContent- Returns:
- a new
XContentBuilder - Throws:
IOException- if anIOExceptionoccurs while building the content
-
builder
public static XContentBuilder builder(XContent xContent, Set<String> includes, Set<String> excludes) throws IOException
Create a newXContentBuilderusing the givenXContentcontent and some inclusive and/or exclusive filters.The builder uses an internal
ByteArrayOutputStreamoutput stream to build the content. When both exclusive and inclusive filters are provided, the underlying builder will first use exclusion filters to remove fields and then will check the remaining fields against the inclusive filters.- Parameters:
xContent- theXContentincludes- the inclusive filters: only fields and objects that match the inclusive filters will be written to the output.excludes- the exclusive filters: only fields and objects that don't match the exclusive filters will be written to the output.- Throws:
IOException- if anIOExceptionoccurs while building the content
-
contentType
public XContentType contentType()
-
getOutputStream
public OutputStream getOutputStream()
- Returns:
- the output stream to which the built object is being written. Note that is dangerous to modify the stream.
-
prettyPrint
public XContentBuilder prettyPrint()
-
isPrettyPrint
public boolean isPrettyPrint()
-
lfAtEnd
public XContentBuilder lfAtEnd()
Indicate that the currentXContentBuildermust write a line feed ("\n") at the end of the built object.This only applies for JSON XContent type. It has no effect for other types.
-
humanReadable
public XContentBuilder humanReadable(boolean humanReadable)
Set the "human readable" flag. Once set, some types of values are written in a format easier to read for a human.
-
humanReadable
public boolean humanReadable()
- Returns:
- the value of the "human readable" flag. When the value is equal to true, some types of values are written in a format easier to read for a human.
-
startObject
public XContentBuilder startObject() throws IOException
- Throws:
IOException
-
startObject
public XContentBuilder startObject(String name) throws IOException
- Throws:
IOException
-
endObject
public XContentBuilder endObject() throws IOException
- Throws:
IOException
-
startArray
public XContentBuilder startArray() throws IOException
- Throws:
IOException
-
startArray
public XContentBuilder startArray(String name) throws IOException
- Throws:
IOException
-
endArray
public XContentBuilder endArray() throws IOException
- Throws:
IOException
-
field
public XContentBuilder field(String name) throws IOException
- Throws:
IOException
-
nullField
public XContentBuilder nullField(String name) throws IOException
- Throws:
IOException
-
nullValue
public XContentBuilder nullValue() throws IOException
- Throws:
IOException
-
field
public XContentBuilder field(String name, Boolean value) throws IOException
- Throws:
IOException
-
field
public XContentBuilder field(String name, boolean value) throws IOException
- Throws:
IOException
-
array
public XContentBuilder array(String name, boolean[] values) throws IOException
- Throws:
IOException
-
value
public XContentBuilder value(Boolean value) throws IOException
- Throws:
IOException
-
value
public XContentBuilder value(boolean value) throws IOException
- Throws:
IOException
-
field
public XContentBuilder field(String name, Byte value) throws IOException
- Throws:
IOException
-
field
public XContentBuilder field(String name, byte value) throws IOException
- Throws:
IOException
-
value
public XContentBuilder value(Byte value) throws IOException
- Throws:
IOException
-
value
public XContentBuilder value(byte value) throws IOException
- Throws:
IOException
-
field
public XContentBuilder field(String name, Double value) throws IOException
- Throws:
IOException
-
field
public XContentBuilder field(String name, double value) throws IOException
- Throws:
IOException
-
array
public XContentBuilder array(String name, double[] values) throws IOException
- Throws:
IOException
-
value
public XContentBuilder value(Double value) throws IOException
- Throws:
IOException
-
value
public XContentBuilder value(double value) throws IOException
- Throws:
IOException
-
field
public XContentBuilder field(String name, Float value) throws IOException
- Throws:
IOException
-
field
public XContentBuilder field(String name, float value) throws IOException
- Throws:
IOException
-
array
public XContentBuilder array(String name, float[] values) throws IOException
- Throws:
IOException
-
value
public XContentBuilder value(Float value) throws IOException
- Throws:
IOException
-
value
public XContentBuilder value(float value) throws IOException
- Throws:
IOException
-
field
public XContentBuilder field(String name, Integer value) throws IOException
- Throws:
IOException
-
field
public XContentBuilder field(String name, int value) throws IOException
- Throws:
IOException
-
array
public XContentBuilder array(String name, int[] values) throws IOException
- Throws:
IOException
-
value
public XContentBuilder value(Integer value) throws IOException
- Throws:
IOException
-
value
public XContentBuilder value(int value) throws IOException
- Throws:
IOException
-
field
public XContentBuilder field(String name, Long value) throws IOException
- Throws:
IOException
-
field
public XContentBuilder field(String name, long value) throws IOException
- Throws:
IOException
-
array
public XContentBuilder array(String name, long[] values) throws IOException
- Throws:
IOException
-
value
public XContentBuilder value(Long value) throws IOException
- Throws:
IOException
-
value
public XContentBuilder value(long value) throws IOException
- Throws:
IOException
-
field
public XContentBuilder field(String name, Short value) throws IOException
- Throws:
IOException
-
field
public XContentBuilder field(String name, short value) throws IOException
- Throws:
IOException
-
array
public XContentBuilder array(String name, short[] values) throws IOException
- Throws:
IOException
-
value
public XContentBuilder value(Short value) throws IOException
- Throws:
IOException
-
value
public XContentBuilder value(short value) throws IOException
- Throws:
IOException
-
field
public XContentBuilder field(String name, BigInteger value) throws IOException
- Throws:
IOException
-
array
public XContentBuilder array(String name, BigInteger[] values) throws IOException
- Throws:
IOException
-
value
public XContentBuilder value(BigInteger value) throws IOException
- Throws:
IOException
-
field
public XContentBuilder field(String name, BigDecimal value) throws IOException
- Throws:
IOException
-
array
public XContentBuilder array(String name, BigDecimal[] values) throws IOException
- Throws:
IOException
-
value
public XContentBuilder value(BigDecimal value) throws IOException
- Throws:
IOException
-
field
public XContentBuilder field(String name, String value) throws IOException
- Throws:
IOException
-
array
public XContentBuilder array(String name, String... values) throws IOException
- Throws:
IOException
-
value
public XContentBuilder value(String value) throws IOException
- Throws:
IOException
-
field
public XContentBuilder field(String name, byte[] value) throws IOException
- Throws:
IOException
-
value
public XContentBuilder value(byte[] value) throws IOException
- Throws:
IOException
-
field
public XContentBuilder field(String name, byte[] value, int offset, int length) throws IOException
- Throws:
IOException
-
value
public XContentBuilder value(byte[] value, int offset, int length) throws IOException
- Throws:
IOException
-
utf8Value
public XContentBuilder utf8Value(byte[] bytes, int offset, int length) throws IOException
Writes the binary content of the given byte array as UTF-8 bytes. UseXContentParser.charBuffer()to read the value back- Throws:
IOException
-
timeField
public XContentBuilder timeField(String name, Object timeValue) throws IOException
Write a time-based field and value, if the passed timeValue is null a null value is written, otherwise a date transformers lookup is performed.- Throws:
IllegalArgumentException- if there is no transformers for the type of objectIOException
-
timeField
public XContentBuilder timeField(String name, String readableName, long value) throws IOException
If thehumanReadableflag is set, writes both a formatted and unformatted version of the time value using the date transformer for theLongclass.- Throws:
IOException
-
timeValue
public XContentBuilder timeValue(Object timeValue) throws IOException
Write a time-based value, if the value is null a null value is written, otherwise a date transformers lookup is performed.- Throws:
IllegalArgumentException- if there is no transformers for the type of objectIOException
-
latlon
public XContentBuilder latlon(String name, double lat, double lon) throws IOException
- Throws:
IOException
-
latlon
public XContentBuilder latlon(double lat, double lon) throws IOException
- Throws:
IOException
-
value
public XContentBuilder value(Path value) throws IOException
- Throws:
IOException
-
field
public XContentBuilder field(String name, Object value) throws IOException
- Throws:
IOException
-
array
public XContentBuilder array(String name, Object... values) throws IOException
- Throws:
IOException
-
value
public XContentBuilder value(Object value) throws IOException
- Throws:
IOException
-
field
public XContentBuilder field(String name, ToXContent value) throws IOException
- Throws:
IOException
-
field
public XContentBuilder field(String name, ToXContent value, ToXContent.Params params) throws IOException
- Throws:
IOException
-
field
public XContentBuilder field(String name, Map<String,Object> values) throws IOException
- Throws:
IOException
-
map
public XContentBuilder map(Map<String,?> values) throws IOException
- Throws:
IOException
-
mapContents
public XContentBuilder mapContents(Map<String,?> values) throws IOException
writes a map without the start object and end object headers- Throws:
IOException
-
field
public XContentBuilder field(String name, Iterable<?> values) throws IOException
- Throws:
IOException
-
humanReadableField
public XContentBuilder humanReadableField(String rawFieldName, String readableFieldName, Object value) throws IOException
- Throws:
IOException
-
percentageField
public XContentBuilder percentageField(String rawFieldName, String readableFieldName, double percentage) throws IOException
- Throws:
IOException
-
rawField
@Deprecated public XContentBuilder rawField(String name, InputStream value) throws IOException
Deprecated.userawField(String, InputStream, XContentType)to avoid content type auto-detectionWrites a raw field with the value taken from the bytes in the stream- Throws:
IOException
-
rawField
public XContentBuilder rawField(String name, InputStream value, XContentType contentType) throws IOException
Writes a raw field with the value taken from the bytes in the stream- Throws:
IOException
-
rawValue
public XContentBuilder rawValue(InputStream stream, XContentType contentType) throws IOException
Writes a value with the source coming directly from the bytes in the stream- Throws:
IOException
-
copyCurrentStructure
public XContentBuilder copyCurrentStructure(XContentParser parser) throws IOException
- Throws:
IOException
-
flush
public void flush() throws IOException- Specified by:
flushin interfaceFlushable- Throws:
IOException
-
close
public void close()
- Specified by:
closein interfaceAutoCloseable- Specified by:
closein interfaceCloseable
-
generator
public XContentGenerator generator()
-
-