Skip to content

Commit 9583e1a

Browse files
committed
Test that DocumentField can handle maps and lists.
1 parent 88d42d8 commit 9583e1a

File tree

6 files changed

+44
-30
lines changed

6 files changed

+44
-30
lines changed

server/src/main/java/org/elasticsearch/common/document/DocumentField.java

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -113,10 +113,9 @@ public void writeTo(StreamOutput out) throws IOException {
113113
public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException {
114114
builder.startArray(name);
115115
for (Object value : values) {
116-
// this call doesn't really need to support writing any kind of object.
117-
// Stored fields values are converted using MappedFieldType#valueForDisplay.
118-
// As a result they can either be Strings, Numbers, or Booleans, that's
119-
// all.
116+
// This call doesn't really need to support writing any kind of object, since the values
117+
// here are always serializable to xContent. Each value could be a leaf types like a string,
118+
// number, or boolean, a list of such values, or a map of such values with string keys.
120119
builder.value(value);
121120
}
122121
builder.endArray();

server/src/test/java/org/elasticsearch/index/get/DocumentFieldTests.java

Lines changed: 37 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@
3838
import java.util.Arrays;
3939
import java.util.Collections;
4040
import java.util.List;
41+
import java.util.Map;
4142
import java.util.function.Supplier;
4243

4344
import static org.elasticsearch.common.xcontent.XContentHelper.toXContent;
@@ -101,29 +102,43 @@ private static DocumentField mutateDocumentField(DocumentField documentField) {
101102
}
102103

103104
public static Tuple<DocumentField, DocumentField> randomDocumentField(XContentType xContentType) {
104-
if (randomBoolean()) {
105-
String metaField = randomValueOtherThanMany(field -> field.equals(TypeFieldMapper.NAME)
106-
|| field.equals(IndexFieldMapper.NAME) || field.equals(IdFieldMapper.NAME),
107-
() -> randomFrom(MapperService.getAllMetaFields()));
108-
DocumentField documentField;
109-
if (metaField.equals(IgnoredFieldMapper.NAME)) {
110-
int numValues = randomIntBetween(1, 3);
111-
List<Object> ignoredFields = new ArrayList<>(numValues);
112-
for (int i = 0; i < numValues; i++) {
113-
ignoredFields.add(randomAlphaOfLengthBetween(3, 10));
105+
switch(randomIntBetween(0, 3)) {
106+
case 0:
107+
String metaField = randomValueOtherThanMany(field -> field.equals(TypeFieldMapper.NAME)
108+
|| field.equals(IndexFieldMapper.NAME) || field.equals(IdFieldMapper.NAME),
109+
() -> randomFrom(MapperService.getAllMetaFields()));
110+
DocumentField documentField;
111+
if (metaField.equals(IgnoredFieldMapper.NAME)) {
112+
int numValues = randomIntBetween(1, 3);
113+
List<Object> ignoredFields = new ArrayList<>(numValues);
114+
for (int i = 0; i < numValues; i++) {
115+
ignoredFields.add(randomAlphaOfLengthBetween(3, 10));
116+
}
117+
documentField = new DocumentField(metaField, ignoredFields);
118+
} else {
119+
//meta fields are single value only, besides _ignored
120+
documentField = new DocumentField(metaField, Collections.singletonList(randomAlphaOfLengthBetween(3, 10)));
114121
}
115-
documentField = new DocumentField(metaField, ignoredFields);
116-
} else {
117-
//meta fields are single value only, besides _ignored
118-
documentField = new DocumentField(metaField, Collections.singletonList(randomAlphaOfLengthBetween(3, 10)));
119-
}
120-
return Tuple.tuple(documentField, documentField);
121-
} else {
122-
String fieldName = randomAlphaOfLengthBetween(3, 10);
123-
Tuple<List<Object>, List<Object>> tuple = RandomObjects.randomStoredFieldValues(random(), xContentType);
124-
DocumentField input = new DocumentField(fieldName, tuple.v1());
125-
DocumentField expected = new DocumentField(fieldName, tuple.v2());
126-
return Tuple.tuple(input, expected);
122+
return Tuple.tuple(documentField, documentField);
123+
case 1:
124+
String fieldName = randomAlphaOfLengthBetween(3, 10);
125+
Tuple<List<Object>, List<Object>> tuple = RandomObjects.randomStoredFieldValues(random(), xContentType);
126+
DocumentField input = new DocumentField(fieldName, tuple.v1());
127+
DocumentField expected = new DocumentField(fieldName, tuple.v2());
128+
return Tuple.tuple(input, expected);
129+
case 2:
130+
List<Object> listValues = randomList(1, 5, () -> randomList(1, 5, ESTestCase::randomInt));
131+
DocumentField listField = new DocumentField(randomAlphaOfLength(5), listValues);
132+
return Tuple.tuple(listField, listField);
133+
case 3:
134+
List<Object> objectValues = randomList(1, 5, () ->
135+
Map.of(randomAlphaOfLength(5), randomInt(),
136+
randomAlphaOfLength(5), randomBoolean(),
137+
randomAlphaOfLength(5), randomAlphaOfLength(10)));
138+
DocumentField objectField = new DocumentField(randomAlphaOfLength(5), objectValues);
139+
return Tuple.tuple(objectField, objectField);
140+
default:
141+
throw new IllegalStateException();
127142
}
128143
}
129144
}

server/src/test/java/org/elasticsearch/search/SearchHitTests.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -188,7 +188,7 @@ public void testFromXContentLenientParsing() throws IOException {
188188
XContentType xContentType = randomFrom(XContentType.values());
189189
SearchHit searchHit = createTestItem(xContentType, true, true);
190190
BytesReference originalBytes = toXContent(searchHit, xContentType, true);
191-
Predicate<String> pathsToExclude = path -> (path.endsWith("highlight") || path.endsWith("fields") || path.contains("_source")
191+
Predicate<String> pathsToExclude = path -> (path.endsWith("highlight") || path.contains("fields") || path.contains("_source")
192192
|| path.contains("inner_hits"));
193193
BytesReference withRandomFields = insertRandomFields(xContentType, originalBytes, pathsToExclude, random());
194194

server/src/test/java/org/elasticsearch/search/suggest/CompletionSuggestionOptionTests.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ private void doTestFromXContent(boolean addRandomFields) throws IOException {
8787
// also there can be inner search hits fields inside this option, we need to exclude another couple of paths
8888
// where we cannot add random stuff
8989
Predicate<String> excludeFilter = (path) -> (path.endsWith(CompletionSuggestion.Entry.Option.CONTEXTS.getPreferredName())
90-
|| path.endsWith("highlight") || path.endsWith("fields") || path.contains("_source") || path.contains("inner_hits"));
90+
|| path.endsWith("highlight") || path.contains("fields") || path.contains("_source") || path.contains("inner_hits"));
9191
mutated = insertRandomFields(xContentType, originalBytes, excludeFilter, random());
9292
} else {
9393
mutated = originalBytes;

server/src/test/java/org/elasticsearch/search/suggest/SuggestionEntryTests.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ private void doTestFromXContent(boolean addRandomFields) throws IOException {
104104
// where we cannot add random stuff
105105
Predicate<String> excludeFilter = (
106106
path) -> (path.endsWith(CompletionSuggestion.Entry.Option.CONTEXTS.getPreferredName()) || path.endsWith("highlight")
107-
|| path.endsWith("fields") || path.contains("_source") || path.contains("inner_hits"));
107+
|| path.contains("fields") || path.contains("_source") || path.contains("inner_hits"));
108108
mutated = insertRandomFields(xContentType, originalBytes, excludeFilter, random());
109109
} else {
110110
mutated = originalBytes;

server/src/test/java/org/elasticsearch/search/suggest/SuggestionTests.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@ private void doTestFromXContent(boolean addRandomFields) throws IOException {
124124
// - the root object should be excluded since it contains the named suggestion arrays
125125
Predicate<String> excludeFilter = path -> (path.isEmpty()
126126
|| path.endsWith(CompletionSuggestion.Entry.Option.CONTEXTS.getPreferredName()) || path.endsWith("highlight")
127-
|| path.endsWith("fields") || path.contains("_source") || path.contains("inner_hits"));
127+
|| path.contains("fields") || path.contains("_source") || path.contains("inner_hits"));
128128
mutated = insertRandomFields(xContentType, originalBytes, excludeFilter, random());
129129
} else {
130130
mutated = originalBytes;

0 commit comments

Comments
 (0)