Class ObjectParser<Value,​Context>

    • Constructor Detail

      • ObjectParser

        public ObjectParser​(java.lang.String name)
        Creates a new ObjectParser instance with a name. This name is used to reference the parser in exceptions and messages.
      • ObjectParser

        public ObjectParser​(java.lang.String name,
                            @Nullable
                            java.util.function.Supplier<Value> valueSupplier)
        Creates a new ObjectParser instance which a name.
        Parameters:
        name - the parsers name, used to reference the parser in exceptions and messages.
        valueSupplier - a supplier that creates a new Value instance used when the parser is used as an inner object parser.
      • ObjectParser

        public ObjectParser​(java.lang.String name,
                            boolean ignoreUnknownFields,
                            @Nullable
                            java.util.function.Supplier<Value> valueSupplier)
        Creates a new ObjectParser instance which a name.
        Parameters:
        name - the parsers name, used to reference the parser in exceptions and messages.
        ignoreUnknownFields - Should this parser ignore unknown fields? This should generally be set to true only when parsing responses from external systems, never when parsing requests from users.
        valueSupplier - a supplier that creates a new Value instance used when the parser is used as an inner object parser.
    • Method Detail

      • fromList

        public static <Value,​ElementValue> java.util.function.BiConsumer<Value,​java.util.List<ElementValue>> fromList​(java.lang.Class<ElementValue> c,
                                                                                                                                  java.util.function.BiConsumer<Value,​ElementValue[]> consumer)
        Adapts an array (or varags) setter into a list setter.
      • parse

        public Value parse​(XContentParser parser,
                           Context context)
                    throws java.io.IOException
        Parses a Value from the given XContentParser
        Parameters:
        parser - the parser to build a value from
        context - context needed for parsing
        Returns:
        a new value instance drawn from the provided value supplier on ObjectParser(String, Supplier)
        Throws:
        java.io.IOException - if an IOException occurs.
      • parse

        public Value parse​(XContentParser parser,
                           Value value,
                           Context context)
                    throws java.io.IOException
        Parses a Value from the given XContentParser
        Parameters:
        parser - the parser to build a value from
        value - the value to fill from the parser
        context - a context that is passed along to all declared field parsers
        Returns:
        the parsed value
        Throws:
        java.io.IOException - if an IOException occurs.
      • declareObjectOrDefault

        public <T> void declareObjectOrDefault​(java.util.function.BiConsumer<Value,​T> consumer,
                                               java.util.function.BiFunction<XContentParser,​Context,​T> objectParser,
                                               java.util.function.Supplier<T> defaultValue,
                                               ParseField field)
      • declareNamedObjects

        public <T> void declareNamedObjects​(java.util.function.BiConsumer<Value,​java.util.List<T>> consumer,
                                            ObjectParser.NamedObjectParser<T,​Context> namedObjectParser,
                                            java.util.function.Consumer<Value> orderedModeCallback,
                                            ParseField field)
        Description copied from class: AbstractObjectParser
        Declares named objects in the style of highlighting's field element. These are usually named inside and object like this:
         
         {
           "highlight": {
             "fields": {        <------ this one
               "title": {},
               "body": {},
               "category": {}
             }
           }
         }
         
         
        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.
        Specified by:
        declareNamedObjects in class AbstractObjectParser<Value,​Context>
        Parameters:
        consumer - sets the values once they have been parsed
        namedObjectParser - parses each named object
        orderedModeCallback - called when the named object is parsed using the "ordered" mode (the array of objects)
        field - the field to parse
      • declareNamedObjects

        public <T> void declareNamedObjects​(java.util.function.BiConsumer<Value,​java.util.List<T>> consumer,
                                            ObjectParser.NamedObjectParser<T,​Context> namedObjectParser,
                                            ParseField field)
        Description copied from class: AbstractObjectParser
        Declares named objects in the style of aggregations. These are named inside and object like this:
         
         {
           "aggregations": {
             "name_1": { "aggregation_type": {} },
             "name_2": { "aggregation_type": {} },
             "name_3": { "aggregation_type": {} }
             }
           }
         }
         
         
        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.
        Specified by:
        declareNamedObjects in class AbstractObjectParser<Value,​Context>
        Parameters:
        consumer - sets the values once they have been parsed
        namedObjectParser - parses each named object
        field - the field to parse
      • toString

        public java.lang.String toString()
        Overrides:
        toString in class java.lang.Object