Skip to content

Commit acf0aed

Browse files
committed
cleanup DocumentParser and
add tests with array in parent object field.
1 parent 969139e commit acf0aed

File tree

2 files changed

+95
-5
lines changed

2 files changed

+95
-5
lines changed

server/src/main/java/org/elasticsearch/index/mapper/DocumentParser.java

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -460,9 +460,7 @@ static void parseObjectOrField(DocumentParserContext context, Mapper mapper) thr
460460
if (context.canAddIgnoredField()
461461
&& (fieldMapper.syntheticSourceMode() == FieldMapper.SyntheticSourceMode.FALLBACK
462462
|| sourceKeepMode == Mapper.SourceKeepMode.ALL
463-
|| (sourceKeepMode == Mapper.SourceKeepMode.ARRAYS
464-
&& context.inArrayScope()
465-
&& mapper.supportStoringArrayOffsets(context) == false)
463+
|| (sourceKeepMode == Mapper.SourceKeepMode.ARRAYS && context.inArrayScope())
466464
|| (context.isWithinCopyTo() == false && context.isCopyToDestinationField(mapper.fullPath())))) {
467465
context = context.addIgnoredFieldFromContext(
468466
IgnoredSourceFieldMapper.NameValue.fromContext(context, fieldMapper.fullPath(), null)
@@ -766,10 +764,10 @@ private static void parseNonDynamicArray(
766764
previousToken = token;
767765
}
768766
if (mapper != null
767+
&& context.canAddIgnoredField()
769768
&& mapper.supportStoringArrayOffsets(context)
770769
&& previousToken == XContentParser.Token.START_ARRAY
771-
&& context.isImmediateParentAnArray()
772-
&& context.getRecordedSource() == false) {
770+
&& context.isImmediateParentAnArray()) {
773771
context.getOffSetContext().maybeRecordEmptyArray(mapper.getOffsetFieldName());
774772
}
775773
if (elements <= 1 && canRemoveSingleLeafElement) {

server/src/test/java/org/elasticsearch/index/mapper/SyntheticSourceNativeArrayIntegrationTests.java

Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,9 +89,36 @@ public void testSynthesizeObjectArray() throws Exception {
8989
document.add(new Object[] { "c", "b", "a" });
9090
documents.add(document);
9191
}
92+
{
93+
List<Object[]> document = new ArrayList<>();
94+
document.add(new Object[] { "9", "7", "5" });
95+
document.add(new Object[] { "2", "4", "6" });
96+
document.add(new Object[] { "7", "6", "5" });
97+
documents.add(document);
98+
}
9299
verifySyntheticObjectArray(documents);
93100
}
94101

102+
public void testSynthesizeArrayInObjectField() throws Exception {
103+
List<Object[]> documents = new ArrayList<>();
104+
documents.add(new Object[] { "z", "y", "x" });
105+
documents.add(new Object[] { "m", "l", "m" });
106+
documents.add(new Object[] { "c", "b", "a" });
107+
documents.add(new Object[] { "9", "7", "5" });
108+
documents.add(new Object[] { "2", "4", "6" });
109+
documents.add(new Object[] { "7", "6", "5" });
110+
verifySyntheticArrayInObject(documents);
111+
}
112+
113+
public void testSynthesizeArrayInObjectFieldRandom() throws Exception {
114+
List<Object[]> documents = new ArrayList<>();
115+
int numDocs = randomIntBetween(8, 256);
116+
for (int i = 0; i < numDocs; i++) {
117+
documents.add(generateRandomStringArray(64, 8, false, true));
118+
}
119+
verifySyntheticArrayInObject(documents);
120+
}
121+
95122
private void verifySyntheticArray(Object[][] arrays) throws IOException {
96123
var mapping = jsonBuilder().startObject()
97124
.startObject("properties")
@@ -236,4 +263,69 @@ private void verifySyntheticObjectArray(List<List<Object[]>> documents) throws I
236263
}
237264
}
238265

266+
private void verifySyntheticArrayInObject(List<Object[]> documents) throws IOException {
267+
var indexService = createIndex(
268+
"test-index",
269+
Settings.builder().put("index.mapping.source.mode", "synthetic").put("index.mapping.synthetic_source_keep", "arrays").build(),
270+
jsonBuilder().startObject()
271+
.startObject("properties")
272+
.startObject("object")
273+
.startObject("properties")
274+
.startObject("field")
275+
.field("type", "keyword")
276+
.endObject()
277+
.endObject()
278+
.endObject()
279+
.endObject()
280+
.endObject()
281+
);
282+
for (int i = 0; i < documents.size(); i++) {
283+
var arrayValue = documents.get(i);
284+
285+
var indexRequest = new IndexRequest("test-index");
286+
indexRequest.id("my-id-" + i);
287+
var source = jsonBuilder().startObject();
288+
source.startObject("object");
289+
source.array("field", arrayValue);
290+
source.endObject();
291+
indexRequest.source(source.endObject());
292+
indexRequest.setRefreshPolicy(WriteRequest.RefreshPolicy.IMMEDIATE);
293+
client().index(indexRequest).actionGet();
294+
295+
var searchRequest = new SearchRequest("test-index");
296+
searchRequest.source().query(new IdsQueryBuilder().addIds("my-id-" + i));
297+
var searchResponse = client().search(searchRequest).actionGet();
298+
try {
299+
var hit = searchResponse.getHits().getHits()[0];
300+
assertThat(hit.getId(), equalTo("my-id-" + i));
301+
var sourceAsMap = hit.getSourceAsMap();
302+
var objectArray = (Map<?, ?>) sourceAsMap.get("object");
303+
304+
List<?> actual = (List<?>) objectArray.get("field");
305+
if (arrayValue == null) {
306+
assertThat(actual, nullValue());
307+
} else if (arrayValue.length == 0) {
308+
assertThat(actual, empty());
309+
} else {
310+
assertThat(actual, Matchers.contains(arrayValue));
311+
}
312+
} finally {
313+
searchResponse.decRef();
314+
}
315+
}
316+
317+
indexService.getShard(0).forceMerge(new ForceMergeRequest("test-index").maxNumSegments(1));
318+
try (var searcher = indexService.getShard(0).acquireSearcher(getTestName())) {
319+
var reader = searcher.getDirectoryReader();
320+
for (int i = 0; i < documents.size(); i++) {
321+
var document = reader.storedFields().document(i);
322+
// Verify that there is no ignored source:
323+
Set<String> storedFieldNames = new LinkedHashSet<>(document.getFields().stream().map(IndexableField::name).toList());
324+
assertThat(storedFieldNames, contains("_id"));
325+
}
326+
var fieldInfo = FieldInfos.getMergedFieldInfos(reader).fieldInfo("object.field.offsets");
327+
assertThat(fieldInfo.getDocValuesType(), equalTo(DocValuesType.BINARY));
328+
}
329+
}
330+
239331
}

0 commit comments

Comments
 (0)