Module org.elasticsearch.xcontent
Package org.elasticsearch.xcontent
Class AbstractObjectParser<Value,Context>
java.lang.Object
org.elasticsearch.xcontent.AbstractObjectParser<Value,Context>
- Direct Known Subclasses:
ConstructingObjectParser,InstantiatingObjectParser.Builder,ObjectParser
Superclass for
ObjectParser and ConstructingObjectParser. Defines most of the "declare" methods so they can be shared.-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionvoiddeclareBoolean(BiConsumer<Value, Boolean> consumer, ParseField field) voiddeclareDouble(BiConsumer<Value, Double> consumer, ParseField field) voiddeclareDoubleArray(BiConsumer<Value, List<Double>> consumer, ParseField field) voiddeclareDoubleOrNull(BiConsumer<Value, Double> consumer, double nullValue, ParseField field) Declare a double field that parses explicitnulls in the json to a default value.abstract voiddeclareExclusiveFieldSet(String... exclusiveSet) Declares a set of fields of which at most one must appear for parsing to succeed E.g.<T> voiddeclareField(BiConsumer<Value, T> consumer, CheckedFunction<XContentParser, T, IOException> parser, ParseField parseField, ObjectParser.ValueType type) abstract <T> voiddeclareField(BiConsumer<Value, T> consumer, ContextParser<Context, T> parser, ParseField parseField, ObjectParser.ValueType type) Declare some field.<T> voiddeclareFieldArray(BiConsumer<Value, List<T>> consumer, ContextParser<Context, T> itemParser, ParseField field, ObjectParser.ValueType type) Declares a field that can contain an array of elements listed in the type ValueType enumvoiddeclareFloat(BiConsumer<Value, Float> consumer, ParseField field) voiddeclareFloatArray(BiConsumer<Value, List<Float>> consumer, ParseField field) voiddeclareFloatOrNull(BiConsumer<Value, Float> consumer, float nullValue, ParseField field) Declare a float field that parses explicitnulls in the json to a default value.voiddeclareInt(BiConsumer<Value, Integer> consumer, ParseField field) voiddeclareIntArray(BiConsumer<Value, List<Integer>> consumer, ParseField field) voiddeclareIntOrNull(BiConsumer<Value, Integer> consumer, int nullValue, ParseField field) Declare an integer field that parses explicitnulls in the json to a default value.voiddeclareLong(BiConsumer<Value, Long> consumer, ParseField field) voiddeclareLongArray(BiConsumer<Value, List<Long>> consumer, ParseField field) voiddeclareLongOrNull(BiConsumer<Value, Long> consumer, long nullValue, ParseField field) abstract <T> voiddeclareNamedObject(BiConsumer<Value, T> consumer, ObjectParser.NamedObjectParser<T, Context> namedObjectParser, ParseField parseField) Declares a single named object.abstract <T> voiddeclareNamedObjects(BiConsumer<Value, List<T>> consumer, ObjectParser.NamedObjectParser<T, Context> namedObjectParser, Consumer<Value> orderedModeCallback, ParseField parseField) Declares named objects in the style of highlighting's field element.abstract <T> voiddeclareNamedObjects(BiConsumer<Value, List<T>> consumer, ObjectParser.NamedObjectParser<T, Context> namedObjectParser, ParseField parseField) Declares named objects in the style of aggregations.<T> voiddeclareObject(BiConsumer<Value, T> consumer, ContextParser<Context, T> objectParser, ParseField field) <T> voiddeclareObjectArray(BiConsumer<Value, List<T>> consumer, ContextParser<Context, T> objectParser, ParseField field) <T> voiddeclareObjectArrayOrNull(BiConsumer<Value, List<T>> consumer, ContextParser<Context, T> objectParser, ParseField field) likedeclareObjectArray(BiConsumer, ContextParser, ParseField), but can also handle single null values, in which case the consumer isn't called<T> voiddeclareObjectOrNull(BiConsumer<Value, T> consumer, ContextParser<Context, T> objectParser, T nullValue, ParseField field) Declare an object field that parses explicitnulls in the json to a default value.abstract voiddeclareRequiredFieldSet(String... requiredSet) Declares a set of fields that are required for parsing to succeed.voiddeclareString(BiConsumer<Value, String> consumer, ParseField field) <T> voiddeclareString(BiConsumer<Value, T> consumer, Function<String, T> fromStringFunction, ParseField field) Declare a field of typeTparsed from string and converted toTusing provided function.voiddeclareStringArray(BiConsumer<Value, List<String>> consumer, ParseField field) voiddeclareStringOrNull(BiConsumer<Value, String> consumer, ParseField field) abstract StringgetName()static <T,Context>
List<T> parseArray(XContentParser parser, Context context, ContextParser<Context, T> itemParser)
-
Constructor Details
-
AbstractObjectParser
protected AbstractObjectParser()
-
-
Method Details
-
declareField
public abstract <T> void declareField(BiConsumer<Value, T> consumer, ContextParser<Context, T> parser, ParseField parseField, ObjectParser.ValueType type) Declare some field. Usually it is easier to usedeclareString(BiConsumer, ParseField)ordeclareObject(BiConsumer, ContextParser, ParseField)rather than call this directly. -
declareNamedObject
public abstract <T> void declareNamedObject(BiConsumer<Value, T> consumer, ObjectParser.NamedObjectParser<T, Context> namedObjectParser, ParseField parseField) Declares a single named object.{ "object_name": { "instance_name": { "field1": "value1", ... } } } }- Parameters:
consumer- sets the value once it has been parsednamedObjectParser- parses the named objectparseField- the field to parse
-
declareNamedObjects
public abstract <T> void declareNamedObjects(BiConsumer<Value, List<T>> consumer, ObjectParser.NamedObjectParser<T, Context> namedObjectParser, ParseField parseField) Declares named objects in the style of aggregations. These are named inside and object like this:
Unlike the other version of this method, "ordered" mode (arrays of objects) is not supported. See NamedObjectHolder in ObjectParserTests for examples of how to invoke this.{ "aggregations": { "name_1": { "aggregation_type": {} }, "name_2": { "aggregation_type": {} }, "name_3": { "aggregation_type": {} } } } }- Parameters:
consumer- sets the values once they have been parsednamedObjectParser- parses each named objectparseField- the field to parse
-
declareNamedObjects
public abstract <T> void declareNamedObjects(BiConsumer<Value, List<T>> consumer, ObjectParser.NamedObjectParser<T, Context> namedObjectParser, Consumer<Value> orderedModeCallback, ParseField parseField) Declares named objects in the style of highlighting's field element. These are usually named inside and object like this:
but, when order is important, some may be written this way:{ "highlight": { "fields": { <------ this one "title": {}, "body": {}, "category": {} } } }
This is because json doesn't enforce ordering. Elasticsearch reads it in the order sent but tools that generate json are free to put object members in an unordered Map, jumbling them. Thus, if you care about order you can send the object in the second way. See NamedObjectHolder in ObjectParserTests for examples of how to invoke this.{ "highlight": { "fields": [ <------ this one {"title": {}}, {"body": {}}, {"category": {}} ] } }- Parameters:
consumer- sets the values once they have been parsednamedObjectParser- parses each named objectorderedModeCallback- called when the named object is parsed using the "ordered" mode (the array of objects)parseField- the field to parse
-
getName
-
declareField
public <T> void declareField(BiConsumer<Value, T> consumer, CheckedFunction<XContentParser, T, IOException> parser, ParseField parseField, ObjectParser.ValueType type) -
declareObject
public <T> void declareObject(BiConsumer<Value, T> consumer, ContextParser<Context, T> objectParser, ParseField field) -
declareObjectOrNull
public <T> void declareObjectOrNull(BiConsumer<Value, T> consumer, ContextParser<Context, T> objectParser, T nullValue, ParseField field) Declare an object field that parses explicitnulls in the json to a default value. -
declareFloat
-
declareFloatOrNull
Declare a float field that parses explicitnulls in the json to a default value. -
declareDouble
-
declareDoubleOrNull
public void declareDoubleOrNull(BiConsumer<Value, Double> consumer, double nullValue, ParseField field) Declare a double field that parses explicitnulls in the json to a default value. -
declareLong
-
declareLongOrNull
-
declareInt
-
declareIntOrNull
Declare an integer field that parses explicitnulls in the json to a default value. -
declareString
-
declareString
public <T> void declareString(BiConsumer<Value, T> consumer, Function<String, T> fromStringFunction, ParseField field) Declare a field of typeTparsed from string and converted toTusing provided function. Throws if the next token is not a string. -
declareStringOrNull
-
declareBoolean
-
declareObjectArray
public <T> void declareObjectArray(BiConsumer<Value, List<T>> consumer, ContextParser<Context, T> objectParser, ParseField field) -
declareObjectArrayOrNull
public <T> void declareObjectArrayOrNull(BiConsumer<Value, List<T>> consumer, ContextParser<Context, T> objectParser, ParseField field) likedeclareObjectArray(BiConsumer, ContextParser, ParseField), but can also handle single null values, in which case the consumer isn't called -
declareStringArray
-
declareDoubleArray
-
declareFloatArray
-
declareLongArray
-
declareIntArray
-
declareFieldArray
public <T> void declareFieldArray(BiConsumer<Value, List<T>> consumer, ContextParser<Context, T> itemParser, ParseField field, ObjectParser.ValueType type) Declares a field that can contain an array of elements listed in the type ValueType enum -
declareRequiredFieldSet
Declares a set of fields that are required for parsing to succeed. Only one of the values provided per String[] must be matched. E.g.declareRequiredFieldSet("foo", "bar");means at least one of "foo" or "bar" fields must be present. If neither of those fields are present, an exception will be thrown. Multiple required sets can be configured:
requires that one of "foo" or "bar" fields are present, and also that one of "bizz" or "buzz" fields are present. In JSON, it means any of these combinations are acceptable:parser.declareRequiredFieldSet("foo", "bar"); parser.declareRequiredFieldSet("bizz", "buzz");{"foo":"...", "bizz": "..."}{"bar":"...", "bizz": "..."}{"foo":"...", "buzz": "..."}{"bar":"...", "buzz": "..."}{"foo":"...", "bar":"...", "bizz": "..."}{"foo":"...", "bar":"...", "buzz": "..."}{"foo":"...", "bizz":"...", "buzz": "..."}{"bar":"...", "bizz":"...", "buzz": "..."}{"foo":"...", "bar":"...", "bizz": "...", "buzz": "..."}
failure cases Provided JSON Reason for failure {"foo":"..."}Missing "bizz" or "buzz" field {"bar":"..."}Missing "bizz" or "buzz" field {"bizz": "..."}Missing "foo" or "bar" field {"buzz": "..."}Missing "foo" or "bar" field {"foo":"...", "bar": "..."}Missing "bizz" or "buzz" field {"bizz":"...", "buzz": "..."}Missing "foo" or "bar" field {"unrelated":"..."}Missing "foo" or "bar" field, and missing "bizz" or "buzz" field - Parameters:
requiredSet- A set of required fields, where at least one of the fields in the array _must_ be present
-
declareExclusiveFieldSet
Declares a set of fields of which at most one must appear for parsing to succeed E.g.declareExclusiveFieldSet("foo", "bar");means that only one of 'foo' or 'bar' must be present, and if both appear then an exception will be thrown. Note that this does not make 'foo' or 'bar' required - seedeclareRequiredFieldSet(String...)for required fields. Multiple exclusive sets may be declared- Parameters:
exclusiveSet- a set of field names, at most one of which must appear
-
parseArray
public static <T,Context> List<T> parseArray(XContentParser parser, Context context, ContextParser<Context, T> itemParser) throws IOException- Throws:
IOException
-