3333import java .io .IOException ;
3434import java .io .UncheckedIOException ;
3535import java .util .Collections ;
36+ import java .util .HashMap ;
3637import java .util .List ;
3738import java .util .Map ;
3839import java .util .function .BiConsumer ;
@@ -47,7 +48,7 @@ public class ExactMatchProcessorTests extends ESTestCase {
4748
4849 public void testBasics () throws Exception {
4950 MockSearchFunction mockSearch = mockedSearchFunction (Map .of ("elastic.co" , Map .of ("globalRank" , 451 , "tldRank" ,23 , "tld" , "co" )));
50- ExactMatchProcessor processor = new ExactMatchProcessor ("_tag" , mockSearch , "_name" , "domain" , false ,
51+ ExactMatchProcessor processor = new ExactMatchProcessor ("_tag" , mockSearch , "_name" , "domain" , false , true ,
5152 List .of (new EnrichSpecification ("tldRank" , "tld_rank" ), new EnrichSpecification ("tld" , "tld" )));
5253 IngestDocument ingestDocument = new IngestDocument ("_index" , "_type" , "_id" , "_routing" , 1L , VersionType .INTERNAL ,
5354 Map .of ("domain" , "elastic.co" ));
@@ -76,7 +77,7 @@ public void testBasics() throws Exception {
7677
7778 public void testNoMatch () throws Exception {
7879 MockSearchFunction mockSearch = mockedSearchFunction ();
79- ExactMatchProcessor processor = new ExactMatchProcessor ("_tag" , mockSearch , "_name" , "domain" , false ,
80+ ExactMatchProcessor processor = new ExactMatchProcessor ("_tag" , mockSearch , "_name" , "domain" , false , true ,
8081 List .of (new EnrichSpecification ("tldRank" , "tld_rank" ), new EnrichSpecification ("tld" , "tld" )));
8182 IngestDocument ingestDocument = new IngestDocument ("_index" , "_type" , "_id" , "_routing" , 1L , VersionType .INTERNAL ,
8283 Map .of ("domain" , "elastic.com" ));
@@ -106,7 +107,7 @@ public void testNoMatch() throws Exception {
106107 public void testSearchFailure () throws Exception {
107108 String indexName = ".enrich-_name" ;
108109 MockSearchFunction mockSearch = mockedSearchFunction (new IndexNotFoundException (indexName ));
109- ExactMatchProcessor processor = new ExactMatchProcessor ("_tag" , mockSearch , "_name" , "domain" , false ,
110+ ExactMatchProcessor processor = new ExactMatchProcessor ("_tag" , mockSearch , "_name" , "domain" , false , true ,
110111 List .of (new EnrichSpecification ("tldRank" , "tld_rank" ), new EnrichSpecification ("tld" , "tld" )));
111112 IngestDocument ingestDocument = new IngestDocument ("_index" , "_type" , "_id" , "_routing" , 1L , VersionType .INTERNAL ,
112113 Map .of ("domain" , "elastic.com" ));
@@ -141,7 +142,7 @@ public void testSearchFailure() throws Exception {
141142 public void testIgnoreKeyMissing () throws Exception {
142143 {
143144 ExactMatchProcessor processor = new ExactMatchProcessor ("_tag" , mockedSearchFunction (), "_name" , "domain" ,
144- true , List .of (new EnrichSpecification ("tldRank" , "tld_rank" ), new EnrichSpecification ("tld" , "tld" )));
145+ true , true , List .of (new EnrichSpecification ("tldRank" , "tld_rank" ), new EnrichSpecification ("tld" , "tld" )));
145146 IngestDocument ingestDocument = new IngestDocument ("_index" , "_type" , "_id" , "_routing" , 1L , VersionType .INTERNAL , Map .of ());
146147
147148 assertThat (ingestDocument .getSourceAndMetadata ().size (), equalTo (6 ));
@@ -152,7 +153,7 @@ public void testIgnoreKeyMissing() throws Exception {
152153 }
153154 {
154155 ExactMatchProcessor processor = new ExactMatchProcessor ("_tag" , mockedSearchFunction (), "_name" , "domain" ,
155- false , List .of (new EnrichSpecification ("tldRank" , "tld_rank" ), new EnrichSpecification ("tld" , "tld" )));
156+ false , true , List .of (new EnrichSpecification ("tldRank" , "tld_rank" ), new EnrichSpecification ("tld" , "tld" )));
156157 IngestDocument ingestDocument = new IngestDocument ("_index" , "_type" , "_id" , "_routing" , 1L , VersionType .INTERNAL , Map .of ());
157158 IngestDocument [] resultHolder = new IngestDocument [1 ];
158159 Exception [] exceptionHolder = new Exception [1 ];
@@ -166,6 +167,43 @@ public void testIgnoreKeyMissing() throws Exception {
166167 }
167168 }
168169
170+ public void testExistingFieldWithOverrideDisabled () throws Exception {
171+ MockSearchFunction mockSearch = mockedSearchFunction (Map .of ("elastic.co" , Map .of ("globalRank" , 451 , "tldRank" ,23 , "tld" , "co" )));
172+ ExactMatchProcessor processor = new ExactMatchProcessor ("_tag" , mockSearch , "_name" , "domain" , false , false ,
173+ List .of (new EnrichSpecification ("tld" , "tld" )));
174+
175+ IngestDocument ingestDocument = new IngestDocument (new HashMap <>(Map .of ("domain" , "elastic.co" , "tld" , "tld" )), Map .of ());
176+ IngestDocument [] resultHolder = new IngestDocument [1 ];
177+ Exception [] exceptionHolder = new Exception [1 ];
178+ processor .execute (ingestDocument , (result , e ) -> {
179+ resultHolder [0 ] = result ;
180+ exceptionHolder [0 ] = e ;
181+ });
182+ assertThat (exceptionHolder [0 ], nullValue ());
183+ assertThat (resultHolder [0 ].hasField ("tld" ), equalTo (true ));
184+ assertThat (resultHolder [0 ].getFieldValue ("tld" , Object .class ), equalTo ("tld" ));
185+ }
186+
187+ public void testExistingNullFieldWithOverrideDisabled () throws Exception {
188+ MockSearchFunction mockSearch = mockedSearchFunction (Map .of ("elastic.co" , Map .of ("globalRank" , 451 , "tldRank" ,23 , "tld" , "co" )));
189+ ExactMatchProcessor processor = new ExactMatchProcessor ("_tag" , mockSearch , "_name" , "domain" , false , false ,
190+ List .of (new EnrichSpecification ("tld" , "tld" )));
191+
192+ Map <String , Object > source = new HashMap <>();
193+ source .put ("domain" , "elastic.co" );
194+ source .put ("tld" , null );
195+ IngestDocument ingestDocument = new IngestDocument (source , Map .of ());
196+ IngestDocument [] resultHolder = new IngestDocument [1 ];
197+ Exception [] exceptionHolder = new Exception [1 ];
198+ processor .execute (ingestDocument , (result , e ) -> {
199+ resultHolder [0 ] = result ;
200+ exceptionHolder [0 ] = e ;
201+ });
202+ assertThat (exceptionHolder [0 ], nullValue ());
203+ assertThat (resultHolder [0 ].hasField ("tld" ), equalTo (true ));
204+ assertThat (resultHolder [0 ].getFieldValue ("tld" , Object .class ), equalTo (null ));
205+ }
206+
169207 private static final class MockSearchFunction implements BiConsumer <SearchRequest , BiConsumer <SearchResponse , Exception >> {
170208 private final SearchResponse mockResponse ;
171209 private final SetOnce <SearchRequest > capturedRequest ;
0 commit comments