Test: Check that parsing SearchHit without _type/_id works (#23715)

The hit object can be very small e.g. when using "stored_fields": ["_none_"],
this adds a test that checks that we can still parse back the object.

* also check type/id null
This commit is contained in:
Christoph Büscher 2017-03-24 10:14:52 +01:00 committed by GitHub
parent 8c53555b28
commit e7a8e69900
2 changed files with 21 additions and 3 deletions

View File

@ -493,7 +493,7 @@ public final class SearchHit implements Streamable, ToXContentObject, Iterable<S
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._TYPE, new Text(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));
@ -524,11 +524,11 @@ public final class SearchHit implements Streamable, ToXContentObject, Iterable<S
public static SearchHit createFromMap(Map<String, Object> values) {
String id = get(Fields._ID, values, null);
String type = get(Fields._TYPE, values, null);
Text type = get(Fields._TYPE, values, null);
NestedIdentity nestedIdentity = get(NestedIdentity._NESTED, values, null);
Map<String, SearchHitField> fields = get(Fields.FIELDS, values, null);
SearchHit searchHit = new SearchHit(-1, id, new Text(type), nestedIdentity, fields);
SearchHit searchHit = new SearchHit(-1, id, type, nestedIdentity, fields);
searchHit.index = get(Fields._INDEX, values, null);
searchHit.score(get(Fields._SCORE, values, DEFAULT_SCORE));
searchHit.version(get(Fields._VERSION, values, -1L));

View File

@ -149,6 +149,24 @@ public class SearchHitTests extends ESTestCase {
assertToXContentEquivalent(originalBytes, toXContent(parsed, xContentType, humanReadable), xContentType);
}
/**
* When e.g. with "stored_fields": "_none_", only "_index" and "_score" are returned.
*/
public void testFromXContentWithoutTypeAndId() throws IOException {
String hit = "{\"_index\": \"my_index\", \"_score\": 1}";
SearchHit parsed;
try (XContentParser parser = createParser(JsonXContent.jsonXContent, hit)) {
parser.nextToken(); // jump to first START_OBJECT
parsed = SearchHit.fromXContent(parser);
assertEquals(XContentParser.Token.END_OBJECT, parser.currentToken());
assertNull(parser.nextToken());
}
assertEquals("my_index", parsed.getIndex());
assertEquals(1, parsed.getScore(), Float.MIN_VALUE);
assertNull(parsed.getType());
assertNull(parsed.getId());
}
public void testToXContent() throws IOException {
SearchHit searchHit = new SearchHit(1, "id1", new Text("type"), Collections.emptyMap());
searchHit.score(1.5f);