A content processing library for Elasticsearch that provides abstractions for parsing and generating various content formats including JSON, YAML, CBOR, and Smile.
npx @tessl/cli install tessl/maven-org-elasticsearch--elasticsearch-x-content@8.18.0Elasticsearch X-Content is a comprehensive content processing library that provides abstractions for parsing and generating various content formats including JSON, YAML, CBOR, and Smile. It offers a generic abstraction layer for content handling with support for pull parsing, filtering, and streaming operations across different serialization formats.
<dependency>
<groupId>org.elasticsearch</groupId>
<artifactId>elasticsearch-x-content</artifactId>
<version>8.18.3</version>
</dependency>import org.elasticsearch.xcontent.*;
import static org.elasticsearch.xcontent.XContentFactory.*;import org.elasticsearch.xcontent.*;
import static org.elasticsearch.xcontent.XContentFactory.*;
// Creating and writing content
XContentBuilder builder = jsonBuilder()
.startObject()
.field("name", "John Doe")
.field("age", 30)
.startArray("skills")
.value("Java")
.value("Elasticsearch")
.endArray()
.endObject();
String jsonString = builder.toString();
// Parsing content
XContentParser parser = XContentType.JSON.xContent()
.createParser(XContentParserConfiguration.EMPTY, jsonString);
// Navigate through the parsed content
while (parser.nextToken() != null) {
if (parser.currentToken() == XContentParser.Token.FIELD_NAME) {
String fieldName = parser.currentName();
parser.nextToken();
Object value = parser.objectText();
System.out.println(fieldName + ": " + value);
}
}
parser.close();The X-Content library is built around several key components:
XContent interface provides format-agnostic content handlingXContentParser offers pull-parsing for efficient, memory-conscious processingXContentGenerator and XContentBuilder enable fluent content creationObjectParser, ConstructingObjectParser) for structured data bindingHigh-level fluent builders and low-level streaming generators for creating structured content in multiple formats.
public static XContentBuilder jsonBuilder() throws IOException;
public static XContentBuilder yamlBuilder() throws IOException;
public static XContentBuilder smileBuilder() throws IOException;
public static XContentBuilder cborBuilder() throws IOException;
public interface XContentGenerator extends Closeable, Flushable {
void writeStartObject() throws IOException;
void writeEndObject() throws IOException;
void writeFieldName(String name) throws IOException;
void writeString(String value) throws IOException;
void writeNumber(int value) throws IOException;
}Pull-parsing approach for efficiently reading and navigating structured content with support for streaming operations.
public interface XContentParser extends Closeable {
Token nextToken() throws IOException;
Token currentToken();
String currentName() throws IOException;
String text() throws IOException;
int intValue() throws IOException;
Map<String, Object> map() throws IOException;
}
enum Token {
START_OBJECT, END_OBJECT, START_ARRAY, END_ARRAY,
FIELD_NAME, VALUE_STRING, VALUE_NUMBER, VALUE_BOOLEAN, VALUE_NULL
}Declarative, type-safe parsers for converting structured content directly into Java objects with support for constructor-based and setter-based object creation.
public final class ObjectParser<Value, Context> {
public ObjectParser(String name, Supplier<Value> valueSupplier);
public void declareString(BiConsumer<Value, String> consumer, ParseField field);
public void declareObject(BiConsumer<Value, T> consumer,
ContextParser<Context, T> parser, ParseField field);
public Value parse(XContentParser parser, Context context) throws IOException;
}
public final class ConstructingObjectParser<Value, Context> {
public ConstructingObjectParser(String name,
BiFunction<Object[], Context, Value> builder);
}Central factory and type system for working with different content formats (JSON, YAML, CBOR, SMILE).
public enum XContentType implements MediaType {
JSON, YAML, CBOR, SMILE;
public XContent xContent();
public static XContentType fromMediaType(String mediaTypeHeaderValue);
}
public interface XContent {
XContentType type();
XContentParser createParser(XContentParserConfiguration config, String content);
XContentGenerator createGenerator(OutputStream os);
}Configuration system for parsers including named object registries, deprecation handling, and API versioning support.
public interface XContentParserConfiguration {
XContentParserConfiguration withRegistry(NamedXContentRegistry registry);
XContentParserConfiguration withDeprecationHandler(DeprecationHandler handler);
static final XContentParserConfiguration EMPTY = ...;
}
public class NamedXContentRegistry {
public <T, C> T parseNamedObject(Class<T> categoryClass, String name,
XContentParser parser, C context);
static final NamedXContentRegistry EMPTY = ...;
}public interface ToXContent {
XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException;
interface Params {
String param(String key);
}
}
public interface ToXContentObject extends ToXContent {
default boolean isFragment() { return false; }
}
public interface ToXContentFragment extends ToXContent {
default boolean isFragment() { return true; }
}public class ParseField {
public ParseField(String name, String... deprecatedNames);
public String getPreferredName();
public boolean match(String fieldName, DeprecationHandler handler);
}
@FunctionalInterface
public interface ContextParser<Context, T> {
T parse(XContentParser parser, Context context) throws IOException;
}public class XContentParseException extends RuntimeException {
public XContentParseException(String message);
public XContentParseException(XContentLocation location, String message);
}
public class NamedObjectNotFoundException extends RuntimeException {
public NamedObjectNotFoundException(String message);
}
public class XContentGenerationException extends RuntimeException {
public XContentGenerationException(String message);
}