@@ -644,6 +644,59 @@ public void testNumericDetectionDefault() throws Exception {
644
644
assertThat (mapper , instanceOf (TextFieldMapper .class ));
645
645
}
646
646
647
+ public void testDateDetectionInheritsFormat () throws Exception {
648
+ String mapping = XContentFactory .jsonBuilder ().startObject ().startObject ("type" )
649
+ .startArray ("dynamic_date_formats" )
650
+ .value ("yyyy-MM-dd" )
651
+ .endArray ()
652
+ .startArray ("dynamic_templates" )
653
+ .startObject ()
654
+ .startObject ("dates" )
655
+ .field ("match_mapping_type" , "date" )
656
+ .field ("match" , "*2" )
657
+ .startObject ("mapping" )
658
+ .endObject ()
659
+ .endObject ()
660
+ .endObject ()
661
+ .startObject ()
662
+ .startObject ("dates" )
663
+ .field ("match_mapping_type" , "date" )
664
+ .field ("match" , "*3" )
665
+ .startObject ("mapping" )
666
+ .field ("format" , "yyyy-MM-dd||epoch_millis" )
667
+ .endObject ()
668
+ .endObject ()
669
+ .endObject ()
670
+ .endArray ()
671
+ .endObject ().endObject ().string ();
672
+
673
+ IndexService index = createIndex ("test" );
674
+ client ().admin ().indices ().preparePutMapping ("test" ).setType ("type" ).setSource (mapping ).get ();
675
+ DocumentMapper defaultMapper = index .mapperService ().documentMapper ("type" );
676
+
677
+ ParsedDocument doc = defaultMapper .parse ("test" , "type" , "1" , XContentFactory .jsonBuilder ()
678
+ .startObject ()
679
+ .field ("date1" , "2016-11-20" )
680
+ .field ("date2" , "2016-11-20" )
681
+ .field ("date3" , "2016-11-20" )
682
+ .endObject ()
683
+ .bytes ());
684
+ assertNotNull (doc .dynamicMappingsUpdate ());
685
+ assertAcked (client ().admin ().indices ().preparePutMapping ("test" ).setType ("type" ).setSource (doc .dynamicMappingsUpdate ().toString ()).get ());
686
+
687
+ defaultMapper = index .mapperService ().documentMapper ("type" );
688
+
689
+ DateFieldMapper dateMapper1 = (DateFieldMapper ) defaultMapper .mappers ().smartNameFieldMapper ("date1" );
690
+ DateFieldMapper dateMapper2 = (DateFieldMapper ) defaultMapper .mappers ().smartNameFieldMapper ("date2" );
691
+ DateFieldMapper dateMapper3 = (DateFieldMapper ) defaultMapper .mappers ().smartNameFieldMapper ("date3" );
692
+ // inherited from dynamic date format
693
+ assertEquals ("yyyy-MM-dd" , dateMapper1 .fieldType ().dateTimeFormatter ().format ());
694
+ // inherited from dynamic date format since the mapping in the template did not specify a format
695
+ assertEquals ("yyyy-MM-dd" , dateMapper2 .fieldType ().dateTimeFormatter ().format ());
696
+ // not inherited from the dynamic date format since the template defined an explicit format
697
+ assertEquals ("yyyy-MM-dd||epoch_millis" , dateMapper3 .fieldType ().dateTimeFormatter ().format ());
698
+ }
699
+
647
700
public void testDynamicTemplateOrder () throws IOException {
648
701
// https://github.com/elastic/elasticsearch/issues/18625
649
702
// elasticsearch used to apply templates that do not have a match_mapping_type first
0 commit comments