Skip to content

Commit 4754f47

Browse files
committed
fix tests
1 parent 0d49121 commit 4754f47

File tree

4 files changed

+54
-19
lines changed

4 files changed

+54
-19
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ public void write(LeafStoredFieldLoader storedFields, int docId, XContentBuilder
108108

109109
@Override
110110
public Set<String> requiredStoredFields() {
111-
return Set.of(SourceFieldMapper.NAME);
111+
return Set.of();
112112
}
113113
};
114114

server/src/main/java/org/elasticsearch/search/lookup/ConcurrentSegmentSourceProvider.java

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@
1313
import org.elasticsearch.common.util.concurrent.ConcurrentCollections;
1414
import org.elasticsearch.index.fieldvisitor.LeafStoredFieldLoader;
1515
import org.elasticsearch.index.fieldvisitor.StoredFieldLoader;
16-
import org.elasticsearch.index.mapper.SourceFieldMapper;
1716
import org.elasticsearch.index.mapper.SourceLoader;
1817

1918
import java.io.IOException;
@@ -31,11 +30,10 @@ class ConcurrentSegmentSourceProvider implements SourceProvider {
3130
private final StoredFieldLoader storedFieldLoader;
3231
private final Map<Object, Leaf> leaves = ConcurrentCollections.newConcurrentMap();
3332

34-
ConcurrentSegmentSourceProvider(SourceLoader loader) {
33+
ConcurrentSegmentSourceProvider(SourceLoader loader, boolean loadSource) {
3534
this.sourceLoader = loader;
3635
var fields = sourceLoader.requiredStoredFields();
37-
boolean loadSource = fields.contains(SourceFieldMapper.NAME);
38-
this.storedFieldLoader = fields.isEmpty() ? StoredFieldLoader.empty() : StoredFieldLoader.create(loadSource, fields);
36+
this.storedFieldLoader = StoredFieldLoader.create(loadSource, fields);
3937
}
4038

4139
@Override

server/src/main/java/org/elasticsearch/search/lookup/SourceProvider.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,6 @@ public interface SourceProvider {
3434
* multiple threads.
3535
*/
3636
static SourceProvider fromLookup(MappingLookup lookup, SourceFieldMetrics metrics) {
37-
return new ConcurrentSegmentSourceProvider(lookup.newSourceLoader(metrics));
37+
return new ConcurrentSegmentSourceProvider(lookup.newSourceLoader(metrics), lookup.isSourceSynthetic() == false);
3838
}
3939
}

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

Lines changed: 50 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -50,11 +50,12 @@ public void testPatchSourceFlat() throws IOException {
5050
}));
5151
assertSourcePatch(
5252
mapperService,
53-
Map.of("field", Map.of("obj", Map.of("key1", "value1")), "another_field", randomAlphaOfLengthBetween(5, 10))
53+
Map.of("field", Map.of("obj", Map.of("key1", "value1")), "another_field", randomAlphaOfLengthBetween(5, 10)),
54+
true
5455
);
5556
}
5657

57-
public void testPatchSourceFlatWithCopyTo() throws IOException {
58+
public void testPatchSourceFlatToCopyTo() throws IOException {
5859
var mapperService = createMapperService(mapping(b -> {
5960
// field
6061
b.startObject("field");
@@ -72,7 +73,28 @@ public void testPatchSourceFlatWithCopyTo() throws IOException {
7273
b.field("type", "keyword");
7374
b.endObject();
7475
}));
75-
assertSourcePatch(mapperService, Map.of("field", "key1"));
76+
assertSourcePatch(mapperService, Map.of("field", "key1"), true);
77+
}
78+
79+
public void testPatchSourceFlatFromCopyTo() throws IOException {
80+
var mapperService = createMapperService(mapping(b -> {
81+
// field
82+
b.startObject("field");
83+
b.field("type", "patch");
84+
b.endObject();
85+
86+
// another_field
87+
b.startObject("another_field");
88+
b.field("type", "keyword");
89+
b.field("copy_to", new String[] { "field" });
90+
b.endObject();
91+
92+
// another_field
93+
b.startObject("extra_field");
94+
b.field("type", "keyword");
95+
b.endObject();
96+
}));
97+
assertSourcePatch(mapperService, Map.of("another_field", "value1", "extra_field", "value2"), false);
7698
}
7799

78100
public void testPatchSourceFlatMulti() throws IOException {
@@ -96,7 +118,8 @@ public void testPatchSourceFlatMulti() throws IOException {
96118
List.of(Map.of("obj", Map.of("key1", "value1")), Map.of("another", "one")),
97119
"another_field",
98120
randomAlphaOfLengthBetween(5, 10)
99-
)
121+
),
122+
true
100123
)
101124
);
102125
assertThat(exc.status(), equalTo(RestStatus.BAD_REQUEST));
@@ -129,7 +152,8 @@ public void testPatchSourceObject() throws IOException {
129152
}));
130153
assertSourcePatch(
131154
mapperService,
132-
Map.of("obj", Map.of("field", Map.of("key1", "value1")), "another_field", randomAlphaOfLengthBetween(5, 10))
155+
Map.of("obj", Map.of("field", Map.of("key1", "value1")), "another_field", randomAlphaOfLengthBetween(5, 10)),
156+
true
133157
);
134158
}
135159

@@ -157,7 +181,11 @@ public void testPatchSourceObjectFlat() throws IOException {
157181
b.field("type", "keyword");
158182
b.endObject();
159183
}));
160-
assertSourcePatch(mapperService, Map.of("obj.field", Map.of("key1", "value1"), "another_field", randomAlphaOfLengthBetween(5, 10)));
184+
assertSourcePatch(
185+
mapperService,
186+
Map.of("obj.field", Map.of("key1", "value1"), "another_field", randomAlphaOfLengthBetween(5, 10)),
187+
true
188+
);
161189
}
162190

163191
public void testPatchSourceNestedObject() throws IOException {
@@ -187,7 +215,8 @@ public void testPatchSourceNestedObject() throws IOException {
187215
}));
188216
assertSourcePatch(
189217
mapperService,
190-
Map.of("nested", Map.of("field", Map.of("key1", "value1")), "another_field", randomAlphaOfLengthBetween(5, 10))
218+
Map.of("nested", Map.of("field", Map.of("key1", "value1")), "another_field", randomAlphaOfLengthBetween(5, 10)),
219+
true
191220
);
192221
}
193222

@@ -228,7 +257,8 @@ public void testPatchSourceNestedArray() throws IOException {
228257
),
229258
"another_field",
230259
randomAlphaOfLengthBetween(5, 10)
231-
)
260+
),
261+
true
232262
);
233263
}
234264

@@ -290,7 +320,8 @@ public void testPatchSourceMulti() throws IOException {
290320
Map.of("field", Map.of("key1", "value1")),
291321
"another_field",
292322
randomAlphaOfLengthBetween(5, 10)
293-
)
323+
),
324+
true
294325
);
295326
}
296327

@@ -347,18 +378,24 @@ public void testPatchSourceMultiFlat() throws IOException {
347378
Map.of("key1", "value1"),
348379
"another_field",
349380
randomAlphaOfLengthBetween(5, 10)
350-
)
381+
),
382+
true
351383
);
352384
}
353385

354-
public static void assertSourcePatch(MapperService mapperService, Map<String, Object> source) throws IOException {
386+
public static void assertSourcePatch(MapperService mapperService, Map<String, Object> source, boolean needsPatching)
387+
throws IOException {
355388
XContentBuilder builder = JsonXContent.contentBuilder();
356389
builder.value(source);
357390
SourceToParse origSource = new SourceToParse("0", BytesReference.bytes(builder), builder.contentType());
358391
ParsedDocument doc = mapperService.documentMapper().parse(origSource);
359392
var storedSource = doc.rootDoc().getField(SourceFieldMapper.NAME).binaryValue();
360-
assertThat(storedSource.length, lessThan(origSource.source().length()));
361-
assertFalse(storedSource.utf8ToString().equals(origSource.source().utf8ToString()));
393+
if (needsPatching) {
394+
assertThat(storedSource.length, lessThan(origSource.source().length()));
395+
assertFalse(storedSource.utf8ToString().equals(origSource.source().utf8ToString()));
396+
} else {
397+
assertThat(storedSource.utf8ToString(), equalTo(origSource.source().utf8ToString()));
398+
}
362399
withLuceneIndex(mapperService, iw -> iw.addDocuments(doc.docs()), ir -> {
363400
Source actual = SourceProvider.fromLookup(mapperService.mappingLookup(), mapperService.getMapperMetrics().sourceFieldMetrics())
364401
.getSource(ir.leaves().get(0), doc.docs().size() - 1);

0 commit comments

Comments
 (0)