Skip to content

Fix TextFieldBlockLoaderTests test assumption. #129361

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 7 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ public TextFieldBlockLoaderTests(Params params) {

@Override
protected Object expected(Map<String, Object> fieldMapping, Object value, TestContext testContext) {
logger.info("field mapping={}", fieldMapping);
logger.info("value={}", value);
return expectedValue(fieldMapping, value, params, testContext);
}

Expand Down Expand Up @@ -82,7 +84,7 @@ public static Object expectedValue(Map<String, Object> fieldMapping, Object valu
.map(BytesRef::new)
.collect(Collectors.toList());

if (store == false) {
if (store == false && ignoreAbove.equals(Integer.MAX_VALUE)) {
// using doc_values for synthetic source
indexed = new ArrayList<>(new HashSet<>(indexed));
indexed.sort(BytesRef::compareTo);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import org.apache.lucene.index.LeafReaderContext;
import org.apache.lucene.store.Directory;
import org.apache.lucene.tests.index.RandomIndexWriter;
import org.apache.lucene.util.BytesRef;
import org.elasticsearch.common.bytes.BytesReference;
import org.elasticsearch.index.IndexSettings;
import org.elasticsearch.index.fieldvisitor.StoredFieldLoader;
Expand All @@ -24,6 +25,8 @@
import org.junit.Assert;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Set;

Expand All @@ -44,9 +47,30 @@ public void runTest(MapperService mapperService, Map<String, Object> document, O
var documentXContent = XContentBuilder.builder(XContentType.JSON.xContent()).map(document);

Object blockLoaderResult = setupAndInvokeBlockLoader(mapperService, documentXContent, blockLoaderFieldName);
expected = attemptMakeReadable(expected);
blockLoaderResult = attemptMakeReadable(blockLoaderResult);
Assert.assertEquals(expected, blockLoaderResult);
}

// Attempt to make assertions readable:
private static Object attemptMakeReadable(Object expected) {
try {
if (expected instanceof BytesRef bytesRef) {
expected = bytesRef.utf8ToString();
} else if (expected instanceof List<?> list && list.getFirst() instanceof BytesRef) {
List<String> expectedList = new ArrayList<>(list.size());
for (Object e : list) {
expectedList.add(((BytesRef) e).utf8ToString());
}
expected = expectedList;
}
return expected;
} catch (Exception | AssertionError e) {
// ip/geo fields can't be converted to strings:
return expected;
}
}

private Object setupAndInvokeBlockLoader(MapperService mapperService, XContentBuilder document, String fieldName) throws IOException {
try (Directory directory = newDirectory()) {
RandomIndexWriter iw = new RandomIndexWriter(random(), directory);
Expand Down