3838import java .util .Collections ;
3939import java .util .List ;
4040import java .util .Map ;
41+ import java .util .Set ;
4142import java .util .SortedSet ;
4243import java .util .TreeSet ;
4344import java .util .function .Supplier ;
@@ -56,7 +57,7 @@ private static <T> SortedSet<T> set(T... values) {
5657 return new TreeSet <>(Arrays .asList (values ));
5758 }
5859
59- void assertFieldNames (SortedSet <String > expected , ParsedDocument doc ) {
60+ void assertFieldNames (Set <String > expected , ParsedDocument doc ) {
6061 String [] got = doc .rootDoc ().getValues ("_field_names" );
6162 assertEquals (expected , set (got ));
6263 }
@@ -120,6 +121,26 @@ public void testExplicitEnabled() throws Exception {
120121 assertFieldNames (set ("field" , "field.keyword" , "_id" , "_version" , "_seq_no" , "_primary_term" , "_source" ), doc );
121122 }
122123
124+ public void testDedup () throws Exception {
125+ String mapping = XContentFactory .jsonBuilder ().startObject ().startObject ("type" )
126+ .startObject ("_field_names" ).field ("enabled" , true ).endObject ()
127+ .endObject ().endObject ().string ();
128+ DocumentMapper docMapper = createIndex ("test" ).mapperService ().documentMapperParser ().parse ("type" , new CompressedXContent (mapping ));
129+ FieldNamesFieldMapper fieldNamesMapper = docMapper .metadataMapper (FieldNamesFieldMapper .class );
130+ assertTrue (fieldNamesMapper .fieldType ().isEnabled ());
131+
132+ ParsedDocument doc = docMapper .parse (SourceToParse .source ("test" , "type" , "1" , XContentFactory .jsonBuilder ()
133+ .startObject ()
134+ .field ("field" , 3 ) // will create 2 lucene fields under the hood: index and doc values
135+ .endObject ()
136+ .bytes (),
137+ XContentType .JSON ));
138+
139+ Set <String > fields = set ("field" , "_id" , "_version" , "_seq_no" , "_primary_term" , "_source" );
140+ assertFieldNames (fields , doc );
141+ assertEquals (fields .size (), doc .rootDoc ().getValues ("_field_names" ).length );
142+ }
143+
123144 public void testDisabled () throws Exception {
124145 String mapping = XContentFactory .jsonBuilder ().startObject ().startObject ("type" )
125146 .startObject ("_field_names" ).field ("enabled" , false ).endObject ()
0 commit comments