Search in sources :

Example 11 with ParseField

use of org.elasticsearch.common.ParseField in project elasticsearch by elastic.

the class ObjectParserTests method testEmptyObject.

public void testEmptyObject() throws IOException {
    XContentParser parser = createParser(JsonXContent.jsonXContent, "{\"object\" : {}}");
    ObjectParser<StaticTestStruct, Void> objectParser = new ObjectParser<>("foo", StaticTestStruct::new);
    objectParser.declareObject(StaticTestStruct::setObject, objectParser, new ParseField("object"));
    StaticTestStruct s = objectParser.parse(parser, null);
    assertNotNull(s.object);
}
Also used : NamedObjectParser(org.elasticsearch.common.xcontent.ObjectParser.NamedObjectParser) ParseField(org.elasticsearch.common.ParseField)

Example 12 with ParseField

use of org.elasticsearch.common.ParseField in project elasticsearch by elastic.

the class AbstractAllocateAllocationCommand method createAllocateParser.

protected static <T extends Builder<?>> ObjectParser<T, Void> createAllocateParser(String command) {
    ObjectParser<T, Void> parser = new ObjectParser<>(command);
    parser.declareString(Builder::setIndex, new ParseField(INDEX_FIELD));
    parser.declareInt(Builder::setShard, new ParseField(SHARD_FIELD));
    parser.declareString(Builder::setNode, new ParseField(NODE_FIELD));
    return parser;
}
Also used : ObjectParser(org.elasticsearch.common.xcontent.ObjectParser) XContentBuilder(org.elasticsearch.common.xcontent.XContentBuilder) ParseField(org.elasticsearch.common.ParseField)

Example 13 with ParseField

use of org.elasticsearch.common.ParseField in project elasticsearch by elastic.

the class SearchHit method declareMetaDataFields.

/**
     * we need to declare parse fields for each metadata field, except for _ID, _INDEX and _TYPE which are
     * handled individually. All other fields are parsed to an entry in the fields map
     */
private static void declareMetaDataFields(ObjectParser<Map<String, Object>, Void> parser) {
    for (String metadatafield : MapperService.getAllMetaFields()) {
        if (metadatafield.equals(Fields._ID) == false && metadatafield.equals(Fields._INDEX) == false && metadatafield.equals(Fields._TYPE) == false) {
            parser.declareField((map, field) -> {
                @SuppressWarnings("unchecked") Map<String, SearchHitField> fieldMap = (Map<String, SearchHitField>) map.computeIfAbsent(Fields.FIELDS, v -> new HashMap<String, SearchHitField>());
                fieldMap.put(field.getName(), field);
            }, (p, c) -> {
                List<Object> values = new ArrayList<>();
                values.add(parseStoredFieldsValue(p));
                return new SearchHitField(metadatafield, values);
            }, new ParseField(metadatafield), ValueType.VALUE);
        }
    }
}
Also used : ShardId(org.elasticsearch.index.shard.ShardId) StreamOutput(org.elasticsearch.common.io.stream.StreamOutput) Arrays(java.util.Arrays) ConstructingObjectParser.constructorArg(org.elasticsearch.common.xcontent.ConstructingObjectParser.constructorArg) Nullable(org.elasticsearch.common.Nullable) ParsingException(org.elasticsearch.common.ParsingException) XContentParserUtils.throwUnknownField(org.elasticsearch.common.xcontent.XContentParserUtils.throwUnknownField) Streamable(org.elasticsearch.common.io.stream.Streamable) HashMap(java.util.HashMap) XContentBuilder(org.elasticsearch.common.xcontent.XContentBuilder) XContentParserUtils.ensureExpectedToken(org.elasticsearch.common.xcontent.XContentParserUtils.ensureExpectedToken) Strings(org.elasticsearch.common.Strings) ArrayList(java.util.ArrayList) XContentHelper(org.elasticsearch.common.xcontent.XContentHelper) Text(org.elasticsearch.common.text.Text) Map(java.util.Map) HighlightField(org.elasticsearch.search.fetch.subphase.highlight.HighlightField) ConstructingObjectParser.optionalConstructorArg(org.elasticsearch.common.xcontent.ConstructingObjectParser.optionalConstructorArg) Collections.singletonMap(java.util.Collections.singletonMap) ParseField(org.elasticsearch.common.ParseField) Lucene.readExplanation(org.elasticsearch.common.lucene.Lucene.readExplanation) XContentParserUtils.parseStoredFieldsValue(org.elasticsearch.common.xcontent.XContentParserUtils.parseStoredFieldsValue) CompletionSuggestion(org.elasticsearch.search.suggest.completion.CompletionSuggestion) Collections.emptyMap(java.util.Collections.emptyMap) Explanation(org.apache.lucene.search.Explanation) Iterator(java.util.Iterator) ToXContent(org.elasticsearch.common.xcontent.ToXContent) IOException(java.io.IOException) BytesReference(org.elasticsearch.common.bytes.BytesReference) CompressorFactory(org.elasticsearch.common.compress.CompressorFactory) XContentParser(org.elasticsearch.common.xcontent.XContentParser) SourceLookup(org.elasticsearch.search.lookup.SourceLookup) Objects(java.util.Objects) MapperService(org.elasticsearch.index.mapper.MapperService) SourceFieldMapper(org.elasticsearch.index.mapper.SourceFieldMapper) List(java.util.List) HighlightField.readHighlightField(org.elasticsearch.search.fetch.subphase.highlight.HighlightField.readHighlightField) Lucene.writeExplanation(org.elasticsearch.common.lucene.Lucene.writeExplanation) StreamInput(org.elasticsearch.common.io.stream.StreamInput) ValueType(org.elasticsearch.common.xcontent.ObjectParser.ValueType) ObjectParser(org.elasticsearch.common.xcontent.ObjectParser) ToXContentObject(org.elasticsearch.common.xcontent.ToXContentObject) ConstructingObjectParser(org.elasticsearch.common.xcontent.ConstructingObjectParser) Collections.unmodifiableMap(java.util.Collections.unmodifiableMap) Writeable(org.elasticsearch.common.io.stream.Writeable) ElasticsearchParseException(org.elasticsearch.ElasticsearchParseException) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) ToXContentObject(org.elasticsearch.common.xcontent.ToXContentObject) HashMap(java.util.HashMap) Map(java.util.Map) Collections.singletonMap(java.util.Collections.singletonMap) Collections.emptyMap(java.util.Collections.emptyMap) Collections.unmodifiableMap(java.util.Collections.unmodifiableMap) ParseField(org.elasticsearch.common.ParseField)

Example 14 with ParseField

use of org.elasticsearch.common.ParseField in project elasticsearch by elastic.

the class SearchHit method declareInnerHitsParseFields.

public static void declareInnerHitsParseFields(ObjectParser<Map<String, Object>, Void> parser) {
    declareMetaDataFields(parser);
    parser.declareString((map, value) -> map.put(Fields._TYPE, value), new ParseField(Fields._TYPE));
    parser.declareString((map, value) -> map.put(Fields._INDEX, value), new ParseField(Fields._INDEX));
    parser.declareString((map, value) -> map.put(Fields._ID, value), new ParseField(Fields._ID));
    parser.declareString((map, value) -> map.put(Fields._NODE, value), new ParseField(Fields._NODE));
    parser.declareField((map, value) -> map.put(Fields._SCORE, value), SearchHit::parseScore, new ParseField(Fields._SCORE), ValueType.FLOAT_OR_NULL);
    parser.declareLong((map, value) -> map.put(Fields._VERSION, value), new ParseField(Fields._VERSION));
    parser.declareField((map, value) -> map.put(Fields._SHARD, value), (p, c) -> ShardId.fromString(p.text()), new ParseField(Fields._SHARD), ValueType.STRING);
    parser.declareObject((map, value) -> map.put(SourceFieldMapper.NAME, value), (p, c) -> parseSourceBytes(p), new ParseField(SourceFieldMapper.NAME));
    parser.declareObject((map, value) -> map.put(Fields.HIGHLIGHT, value), (p, c) -> parseHighlightFields(p), new ParseField(Fields.HIGHLIGHT));
    parser.declareObject((map, value) -> {
        Map<String, SearchHitField> fieldMap = get(Fields.FIELDS, map, new HashMap<String, SearchHitField>());
        fieldMap.putAll(value);
        map.put(Fields.FIELDS, fieldMap);
    }, (p, c) -> parseFields(p), new ParseField(Fields.FIELDS));
    parser.declareObject((map, value) -> map.put(Fields._EXPLANATION, value), (p, c) -> parseExplanation(p), new ParseField(Fields._EXPLANATION));
    parser.declareObject((map, value) -> map.put(NestedIdentity._NESTED, value), NestedIdentity::fromXContent, new ParseField(NestedIdentity._NESTED));
    parser.declareObject((map, value) -> map.put(Fields.INNER_HITS, value), (p, c) -> parseInnerHits(p), new ParseField(Fields.INNER_HITS));
    parser.declareStringArray((map, list) -> map.put(Fields.MATCHED_QUERIES, list), new ParseField(Fields.MATCHED_QUERIES));
    parser.declareField((map, list) -> map.put(Fields.SORT, list), SearchSortValues::fromXContent, new ParseField(Fields.SORT), ValueType.OBJECT_ARRAY);
}
Also used : ParseField(org.elasticsearch.common.ParseField)

Example 15 with ParseField

use of org.elasticsearch.common.ParseField in project elasticsearch by elastic.

the class ValuesSourceParserHelper method declareFields.

private static <VS extends ValuesSource> void declareFields(ObjectParser<? extends ValuesSourceAggregationBuilder<VS, ?>, QueryParseContext> objectParser, boolean scriptable, boolean formattable, boolean timezoneAware, ValueType targetValueType) {
    objectParser.declareField(ValuesSourceAggregationBuilder::field, XContentParser::text, new ParseField("field"), ObjectParser.ValueType.STRING);
    objectParser.declareField(ValuesSourceAggregationBuilder::missing, XContentParser::objectText, new ParseField("missing"), ObjectParser.ValueType.VALUE);
    objectParser.declareField(ValuesSourceAggregationBuilder::valueType, p -> {
        ValueType valueType = ValueType.resolveForScript(p.text());
        if (targetValueType != null && valueType.isNotA(targetValueType)) {
            throw new ParsingException(p.getTokenLocation(), "Aggregation [" + objectParser.getName() + "] was configured with an incompatible value type [" + valueType + "]. It can only work on value of type [" + targetValueType + "]");
        }
        return valueType;
    }, new ParseField("value_type", "valueType"), ObjectParser.ValueType.STRING);
    if (formattable) {
        objectParser.declareField(ValuesSourceAggregationBuilder::format, XContentParser::text, new ParseField("format"), ObjectParser.ValueType.STRING);
    }
    if (scriptable) {
        objectParser.declareField(ValuesSourceAggregationBuilder::script, (parser, context) -> Script.parse(parser), Script.SCRIPT_PARSE_FIELD, ObjectParser.ValueType.OBJECT_OR_STRING);
    }
    if (timezoneAware) {
        objectParser.declareField(ValuesSourceAggregationBuilder::timeZone, p -> {
            if (p.currentToken() == XContentParser.Token.VALUE_STRING) {
                return DateTimeZone.forID(p.text());
            } else {
                return DateTimeZone.forOffsetHours(p.intValue());
            }
        }, TIME_ZONE, ObjectParser.ValueType.LONG);
    }
}
Also used : ParsingException(org.elasticsearch.common.ParsingException) XContentParser(org.elasticsearch.common.xcontent.XContentParser) ParseField(org.elasticsearch.common.ParseField)

Aggregations

ParseField (org.elasticsearch.common.ParseField)29 NamedObjectParser (org.elasticsearch.common.xcontent.ObjectParser.NamedObjectParser)17 ParsingException (org.elasticsearch.common.ParsingException)7 IOException (java.io.IOException)5 List (java.util.List)5 ArrayList (java.util.ArrayList)4 Arrays (java.util.Arrays)3 ObjectParser (org.elasticsearch.common.xcontent.ObjectParser)3 XContentBuilder (org.elasticsearch.common.xcontent.XContentBuilder)3 UncheckedIOException (java.io.UncheckedIOException)2 URI (java.net.URI)2 HashMap (java.util.HashMap)2 CheckedFunction (org.elasticsearch.common.CheckedFunction)2 Nullable (org.elasticsearch.common.Nullable)2 BytesReference (org.elasticsearch.common.bytes.BytesReference)2 ConstructingObjectParser.constructorArg (org.elasticsearch.common.xcontent.ConstructingObjectParser.constructorArg)2 ConstructingObjectParser.optionalConstructorArg (org.elasticsearch.common.xcontent.ConstructingObjectParser.optionalConstructorArg)2 ValueType (org.elasticsearch.common.xcontent.ObjectParser.ValueType)2 XContentParser (org.elasticsearch.common.xcontent.XContentParser)2 JsonXContent (org.elasticsearch.common.xcontent.json.JsonXContent)2