Search in sources :

Example 1 with ParseField

use of org.graylog.shaded.elasticsearch7.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 2 with ParseField

use of org.graylog.shaded.elasticsearch7.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 3 with ParseField

use of org.graylog.shaded.elasticsearch7.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 4 with ParseField

use of org.graylog.shaded.elasticsearch7.org.elasticsearch.common.ParseField in project elasticsearch by elastic.

the class ObjectParserTests method testObjectOrDefault.

public void testObjectOrDefault() throws IOException {
    XContentParser parser = createParser(JsonXContent.jsonXContent, "{\"object\" : { \"test\": 2}}");
    ObjectParser<StaticTestStruct, Void> objectParser = new ObjectParser<>("foo", StaticTestStruct::new);
    objectParser.declareInt(StaticTestStruct::setTest, new ParseField("test"));
    objectParser.declareObjectOrDefault(StaticTestStruct::setObject, objectParser, StaticTestStruct::new, new ParseField("object"));
    StaticTestStruct s = objectParser.parse(parser, null);
    assertEquals(s.object.test, 2);
    parser = createParser(JsonXContent.jsonXContent, "{\"object\" : false }");
    s = objectParser.parse(parser, null);
    assertNull(s.object);
    parser = createParser(JsonXContent.jsonXContent, "{\"object\" : true }");
    s = objectParser.parse(parser, null);
    assertNotNull(s.object);
    assertEquals(s.object.test, 0);
}
Also used : NamedObjectParser(org.elasticsearch.common.xcontent.ObjectParser.NamedObjectParser) ParseField(org.elasticsearch.common.ParseField)

Example 5 with ParseField

use of org.graylog.shaded.elasticsearch7.org.elasticsearch.common.ParseField in project elasticsearch by elastic.

the class ObjectParserTests method testIgnoreUnknownArrays.

public void testIgnoreUnknownArrays() throws IOException {
    XContentBuilder b = XContentBuilder.builder(XContentType.JSON.xContent());
    b.startObject();
    {
        b.field("test", "foo");
        b.startArray("junk");
        {
            b.startObject();
            {
                b.field("really", "junk");
            }
            b.endObject();
        }
        b.endArray();
    }
    b.endObject();
    b = shuffleXContent(b);
    XContentParser parser = createParser(JsonXContent.jsonXContent, b.bytes());
    class TestStruct {

        public String test;
    }
    ObjectParser<TestStruct, Void> objectParser = new ObjectParser<>("foo", true, null);
    objectParser.declareField((i, c, x) -> c.test = i.text(), new ParseField("test"), ObjectParser.ValueType.STRING);
    TestStruct s = objectParser.parse(parser, new TestStruct(), null);
    assertEquals(s.test, "foo");
}
Also used : NamedObjectParser(org.elasticsearch.common.xcontent.ObjectParser.NamedObjectParser) ParseField(org.elasticsearch.common.ParseField)

Aggregations

ParseField (org.elasticsearch.common.ParseField)31 NamedObjectParser (org.elasticsearch.common.xcontent.ObjectParser.NamedObjectParser)17 ParsingException (org.elasticsearch.common.ParsingException)7 IOException (java.io.IOException)6 List (java.util.List)6 XContentBuilder (org.elasticsearch.common.xcontent.XContentBuilder)5 ArrayList (java.util.ArrayList)4 ObjectParser (org.elasticsearch.common.xcontent.ObjectParser)4 UncheckedIOException (java.io.UncheckedIOException)3 Arrays (java.util.Arrays)3 HashMap (java.util.HashMap)3 URI (java.net.URI)2 Map (java.util.Map)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