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;
}
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);
}
}
}
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);
}
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);
}
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");
}
Aggregations