Skip to content

Commit cb0a2ab

Browse files
committed
Added tests
1 parent 8d4b1a2 commit cb0a2ab

File tree

5 files changed

+370
-118
lines changed

5 files changed

+370
-118
lines changed

modules/mapper-extras/src/yamlRestTest/resources/rest-api-spec/test/match_only_text/10_basic.yml

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -650,6 +650,48 @@ synthetic_source match_only_text with ignored multi-field:
650650
- match:
651651
hits.hits.0._source.foo: "Apache Lucene powers Elasticsearch"
652652

653+
---
654+
synthetic_source match_only_text with ignored multi-field and multiple values in the same doc:
655+
- requires:
656+
cluster_features: [ "mapper.source.mode_from_index_setting" ]
657+
reason: "Source mode configured through index setting"
658+
659+
- do:
660+
indices.create:
661+
index: synthetic_source_test
662+
body:
663+
settings:
664+
index:
665+
mapping.source.mode: synthetic
666+
mappings:
667+
properties:
668+
foo:
669+
type: match_only_text
670+
fields:
671+
raw:
672+
type: keyword
673+
ignore_above: 20
674+
675+
- do:
676+
index:
677+
index: synthetic_source_test
678+
id: "1"
679+
refresh: true
680+
body:
681+
foo: [ "This value is too long and will be ignored", "This value is short" ]
682+
683+
- do:
684+
search:
685+
index: synthetic_source_test
686+
body:
687+
query:
688+
match_phrase:
689+
foo: this value is
690+
691+
- match: { "hits.total.value": 1 }
692+
- match: { hits.hits.0._source.foo.0: "This value is too long and will be ignored" }
693+
- match: { hits.hits.0._source.foo.1: "This value is short" }
694+
653695
---
654696
synthetic_source match_only_text with stored multi-field:
655697
- requires:

rest-api-spec/src/yamlRestTest/resources/rest-api-spec/test/mapping/20_synthetic_source.yml

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,3 +79,87 @@ synthetic_source text with multi-field:
7979
- match: { "hits.total.value": 1 }
8080
- match:
8181
hits.hits.0._source.foo: "Apache Lucene powers Elasticsearch"
82+
83+
---
84+
synthetic_source text with ignored multi-field:
85+
- requires:
86+
cluster_features: [ "mapper.source.mode_from_index_setting" ]
87+
reason: "Source mode configured through index setting"
88+
89+
- do:
90+
indices.create:
91+
index: synthetic_source_test
92+
body:
93+
settings:
94+
index:
95+
mapping.source.mode: synthetic
96+
mappings:
97+
properties:
98+
foo:
99+
type: text
100+
fields:
101+
raw:
102+
type: keyword
103+
ignored: 5
104+
105+
- do:
106+
index:
107+
index: synthetic_source_test
108+
id: "1"
109+
refresh: true
110+
body:
111+
foo: "Apache Lucene powers Elasticsearch"
112+
113+
- do:
114+
search:
115+
index: synthetic_source_test
116+
body:
117+
query:
118+
match_phrase:
119+
foo: apache lucene
120+
121+
- match: { "hits.total.value": 1 }
122+
- match:
123+
hits.hits.0._source.foo: "Apache Lucene powers Elasticsearch"
124+
125+
---
126+
synthetic_source text with ignored multi-field and multiple values in the same doc:
127+
- requires:
128+
cluster_features: [ "mapper.source.mode_from_index_setting" ]
129+
reason: "Source mode configured through index setting"
130+
131+
- do:
132+
indices.create:
133+
index: synthetic_source_test
134+
body:
135+
settings:
136+
index:
137+
mapping.source.mode: synthetic
138+
mappings:
139+
properties:
140+
foo:
141+
type: text
142+
fields:
143+
raw:
144+
type: keyword
145+
ignored: 20
146+
147+
- do:
148+
index:
149+
index: synthetic_source_test
150+
id: "1"
151+
refresh: true
152+
body:
153+
foo: [ "This value is too long and will be ignored", "This value is short" ]
154+
155+
- do:
156+
search:
157+
index: synthetic_source_test
158+
body:
159+
query:
160+
match_phrase:
161+
foo: this value is
162+
163+
- match: { "hits.total.value": 1 }
164+
- match: { hits.hits.0._source.foo: "This value is too long and will be ignored" }
165+
- match: { hits.hits.0._source.foo: "This value is short" }

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

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
import java.util.stream.Stream;
2323

2424
public class CompositeSyntheticFieldLoaderTests extends ESTestCase {
25+
2526
public void testComposingMultipleStoredFields() throws IOException {
2627
var sut = new CompositeSyntheticFieldLoader(
2728
"foo",
@@ -234,4 +235,52 @@ public void testFieldName() {
234235
var sut = new CompositeSyntheticFieldLoader("foo", "bar.baz.foo");
235236
assertEquals("bar.baz.foo", sut.fieldName());
236237
}
238+
239+
public void testMergeTwoFieldLoaders() throws IOException {
240+
// given
241+
var fieldLoader1 = new CompositeSyntheticFieldLoader(
242+
"foo",
243+
"bar.baz.foo",
244+
List.of(new CompositeSyntheticFieldLoader.StoredFieldLayer("foo.one") {
245+
@Override
246+
protected void writeValue(Object value, XContentBuilder b) throws IOException {
247+
b.value((long) value);
248+
}
249+
}, new CompositeSyntheticFieldLoader.StoredFieldLayer("foo.two") {
250+
@Override
251+
protected void writeValue(Object value, XContentBuilder b) throws IOException {
252+
b.value((long) value);
253+
}
254+
})
255+
);
256+
257+
var fieldLoader2 = new CompositeSyntheticFieldLoader(
258+
"foo",
259+
"bar.baz.foo",
260+
List.of(new CompositeSyntheticFieldLoader.StoredFieldLayer("foo.three") {
261+
@Override
262+
protected void writeValue(Object value, XContentBuilder b) throws IOException {
263+
b.value((long) value);
264+
}
265+
})
266+
);
267+
268+
var mergedFieldLoader = fieldLoader1.mergedWith(fieldLoader2);
269+
270+
var storedFieldLoaders = mergedFieldLoader.storedFieldLoaders()
271+
.collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue));
272+
storedFieldLoaders.get("foo.one").load(List.of(45L, 46L));
273+
storedFieldLoaders.get("foo.two").load(List.of(1L));
274+
storedFieldLoaders.get("foo.three").load(List.of(98L, 99L));
275+
276+
// when
277+
var result = XContentBuilder.builder(XContentType.JSON.xContent());
278+
result.startObject();
279+
mergedFieldLoader.write(result);
280+
result.endObject();
281+
282+
// then
283+
assertEquals("""
284+
{"foo":[45,46,1,98,99]}""", Strings.toString(result));
285+
}
237286
}

0 commit comments

Comments
 (0)