@@ -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