Skip to content

Commit 363c784

Browse files
committed
Actual fix was to check for synthetic_source_keep instead of ignore_above. Also improved blockLoaderResult assertion by making it readable when it fails.
1 parent 2a8f8da commit 363c784

File tree

2 files changed

+91
-24
lines changed

2 files changed

+91
-24
lines changed

server/src/test/java/org/elasticsearch/index/mapper/blockloader/TextFieldBlockLoaderTests.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ public TextFieldBlockLoaderTests(Params params) {
2929
protected Object expected(Map<String, Object> fieldMapping, Object value, TestContext testContext) {
3030
logger.info("field mapping={}", fieldMapping);
3131
logger.info("value={}", value);
32+
logger.info("params={}", params.toString());
3233
return expectedValue(fieldMapping, value, params, testContext);
3334
}
3435

@@ -84,7 +85,8 @@ public static Object expectedValue(Map<String, Object> fieldMapping, Object valu
8485
.map(BytesRef::new)
8586
.collect(Collectors.toList());
8687

87-
if (store == false && ignoreAbove.equals(Integer.MAX_VALUE)) {
88+
String ssk = (String) keywordMultiFieldMapping.get("synthetic_source_keep");
89+
if (store == false && "arrays".equals(ssk) == false) {
8890
// using doc_values for synthetic source
8991
indexed = new ArrayList<>(new HashSet<>(indexed));
9092
indexed.sort(BytesRef::compareTo);

test/framework/src/main/java/org/elasticsearch/index/mapper/BlockLoaderTestRunner.java

Lines changed: 88 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -22,16 +22,20 @@
2222
import org.elasticsearch.search.lookup.SearchLookup;
2323
import org.elasticsearch.xcontent.XContentBuilder;
2424
import org.elasticsearch.xcontent.XContentType;
25-
import org.junit.Assert;
25+
import org.hamcrest.BaseMatcher;
26+
import org.hamcrest.Description;
27+
import org.hamcrest.Matcher;
2628

2729
import java.io.IOException;
30+
import java.lang.reflect.Array;
2831
import java.util.ArrayList;
2932
import java.util.List;
3033
import java.util.Map;
3134
import java.util.Set;
3235

3336
import static org.apache.lucene.tests.util.LuceneTestCase.newDirectory;
3437
import static org.apache.lucene.tests.util.LuceneTestCase.random;
38+
import static org.elasticsearch.index.mapper.BlockLoaderTestRunner.PrettyEqual.prettyEqualTo;
3539
import static org.hamcrest.MatcherAssert.assertThat;
3640
import static org.hamcrest.Matchers.equalTo;
3741

@@ -47,28 +51,7 @@ public void runTest(MapperService mapperService, Map<String, Object> document, O
4751
var documentXContent = XContentBuilder.builder(XContentType.JSON.xContent()).map(document);
4852

4953
Object blockLoaderResult = setupAndInvokeBlockLoader(mapperService, documentXContent, blockLoaderFieldName);
50-
expected = attemptMakeReadable(expected);
51-
blockLoaderResult = attemptMakeReadable(blockLoaderResult);
52-
Assert.assertEquals(expected, blockLoaderResult);
53-
}
54-
55-
// Attempt to make assertions readable:
56-
private static Object attemptMakeReadable(Object expected) {
57-
try {
58-
if (expected instanceof BytesRef bytesRef) {
59-
expected = bytesRef.utf8ToString();
60-
} else if (expected instanceof List<?> list && list.getFirst() instanceof BytesRef) {
61-
List<String> expectedList = new ArrayList<>(list.size());
62-
for (Object e : list) {
63-
expectedList.add(((BytesRef) e).utf8ToString());
64-
}
65-
expected = expectedList;
66-
}
67-
return expected;
68-
} catch (Exception | AssertionError e) {
69-
// ip/geo fields can't be converted to strings:
70-
return expected;
71-
}
54+
assertThat(blockLoaderResult, prettyEqualTo(expected));
7255
}
7356

7457
private Object setupAndInvokeBlockLoader(MapperService mapperService, XContentBuilder document, String fieldName) throws IOException {
@@ -169,4 +152,86 @@ public FieldNamesFieldMapper.FieldNamesFieldType fieldNames() {
169152
}
170153
});
171154
}
155+
156+
// Copied from org.hamcrest.core.IsEqual and modified to pretty print failure when bytesref
157+
static class PrettyEqual<T> extends BaseMatcher<T> {
158+
159+
private final Object expectedValue;
160+
161+
PrettyEqual(T equalArg) {
162+
expectedValue = equalArg;
163+
}
164+
165+
@Override
166+
public boolean matches(Object actualValue) {
167+
return areEqual(actualValue, expectedValue);
168+
}
169+
170+
@Override
171+
public void describeTo(Description description) {
172+
description.appendValue(attemptMakeReadable(expectedValue));
173+
}
174+
175+
@Override
176+
public void describeMismatch(Object item, Description description) {
177+
super.describeMismatch(attemptMakeReadable(item), description);
178+
}
179+
180+
private static boolean areEqual(Object actual, Object expected) {
181+
if (actual == null) {
182+
return expected == null;
183+
}
184+
185+
if (expected != null && isArray(actual)) {
186+
return isArray(expected) && areArraysEqual(actual, expected);
187+
}
188+
189+
return actual.equals(expected);
190+
}
191+
192+
private static boolean areArraysEqual(Object actualArray, Object expectedArray) {
193+
return areArrayLengthsEqual(actualArray, expectedArray) && areArrayElementsEqual(actualArray, expectedArray);
194+
}
195+
196+
private static boolean areArrayLengthsEqual(Object actualArray, Object expectedArray) {
197+
return Array.getLength(actualArray) == Array.getLength(expectedArray);
198+
}
199+
200+
private static boolean areArrayElementsEqual(Object actualArray, Object expectedArray) {
201+
for (int i = 0; i < Array.getLength(actualArray); i++) {
202+
if (areEqual(Array.get(actualArray, i), Array.get(expectedArray, i)) == false) {
203+
return false;
204+
}
205+
}
206+
return true;
207+
}
208+
209+
private static boolean isArray(Object o) {
210+
return o.getClass().isArray();
211+
}
212+
213+
// Attempt to make assertions readable:
214+
static Object attemptMakeReadable(Object expected) {
215+
try {
216+
if (expected instanceof BytesRef bytesRef) {
217+
expected = bytesRef.utf8ToString();
218+
} else if (expected instanceof List<?> list && list.getFirst() instanceof BytesRef) {
219+
List<String> expectedList = new ArrayList<>(list.size());
220+
for (Object e : list) {
221+
expectedList.add(((BytesRef) e).utf8ToString());
222+
}
223+
expected = expectedList;
224+
}
225+
return expected;
226+
} catch (Exception | AssertionError e) {
227+
// ip/geo fields can't be converted to strings:
228+
return expected;
229+
}
230+
}
231+
232+
public static <T> Matcher<T> prettyEqualTo(T operand) {
233+
return new PrettyEqual<>(operand);
234+
}
235+
236+
}
172237
}

0 commit comments

Comments
 (0)