@@ -56,7 +56,7 @@ public void testBasics() throws Exception {
5656 documents .put ("elastic.co" , document1 );
5757 }
5858 MockSearchFunction mockSearch = mockedSearchFunction (documents );
59- ExactMatchProcessor processor = new ExactMatchProcessor ("_tag" , mockSearch , "_name" , "domain" , false ,
59+ ExactMatchProcessor processor = new ExactMatchProcessor ("_tag" , mockSearch , "_name" , "domain" , false , true ,
6060 Arrays .asList (new EnrichSpecification ("tldRank" , "tld_rank" ), new EnrichSpecification ("tld" , "tld" )));
6161 IngestDocument ingestDocument = new IngestDocument ("_index" , "_type" , "_id" , "_routing" , 1L , VersionType .INTERNAL ,
6262 Collections .singletonMap ("domain" , "elastic.co" ));
@@ -85,7 +85,7 @@ public void testBasics() throws Exception {
8585
8686 public void testNoMatch () throws Exception {
8787 MockSearchFunction mockSearch = mockedSearchFunction ();
88- ExactMatchProcessor processor = new ExactMatchProcessor ("_tag" , mockSearch , "_name" , "domain" , false ,
88+ ExactMatchProcessor processor = new ExactMatchProcessor ("_tag" , mockSearch , "_name" , "domain" , false , true ,
8989 Arrays .asList (new EnrichSpecification ("tldRank" , "tld_rank" ), new EnrichSpecification ("tld" , "tld" )));
9090 IngestDocument ingestDocument = new IngestDocument ("_index" , "_type" , "_id" , "_routing" , 1L , VersionType .INTERNAL ,
9191 Collections .singletonMap ("domain" , "elastic.com" ));
@@ -115,7 +115,7 @@ public void testNoMatch() throws Exception {
115115 public void testSearchFailure () throws Exception {
116116 String indexName = ".enrich-_name" ;
117117 MockSearchFunction mockSearch = mockedSearchFunction (new IndexNotFoundException (indexName ));
118- ExactMatchProcessor processor = new ExactMatchProcessor ("_tag" , mockSearch , "_name" , "domain" , false ,
118+ ExactMatchProcessor processor = new ExactMatchProcessor ("_tag" , mockSearch , "_name" , "domain" , false , true ,
119119 Arrays .asList (new EnrichSpecification ("tldRank" , "tld_rank" ), new EnrichSpecification ("tld" , "tld" )));
120120 IngestDocument ingestDocument = new IngestDocument ("_index" , "_type" , "_id" , "_routing" , 1L , VersionType .INTERNAL ,
121121 Collections .singletonMap ("domain" , "elastic.com" ));
@@ -150,7 +150,7 @@ public void testSearchFailure() throws Exception {
150150 public void testIgnoreKeyMissing () throws Exception {
151151 {
152152 ExactMatchProcessor processor = new ExactMatchProcessor ("_tag" , mockedSearchFunction (), "_name" , "domain" ,
153- true , Arrays .asList (new EnrichSpecification ("tldRank" , "tld_rank" ), new EnrichSpecification ("tld" , "tld" )));
153+ true , true , Arrays .asList (new EnrichSpecification ("tldRank" , "tld_rank" ), new EnrichSpecification ("tld" , "tld" )));
154154 IngestDocument ingestDocument = new IngestDocument ("_index" , "_type" , "_id" , "_routing" , 1L , VersionType .INTERNAL ,
155155 Collections .emptyMap ());
156156
@@ -162,7 +162,7 @@ public void testIgnoreKeyMissing() throws Exception {
162162 }
163163 {
164164 ExactMatchProcessor processor = new ExactMatchProcessor ("_tag" , mockedSearchFunction (), "_name" , "domain" ,
165- false , Arrays .asList (new EnrichSpecification ("tldRank" , "tld_rank" ), new EnrichSpecification ("tld" , "tld" )));
165+ false , true , Arrays .asList (new EnrichSpecification ("tldRank" , "tld_rank" ), new EnrichSpecification ("tld" , "tld" )));
166166 IngestDocument ingestDocument = new IngestDocument ("_index" , "_type" , "_id" , "_routing" , 1L , VersionType .INTERNAL ,
167167 Collections .emptyMap ());
168168 IngestDocument [] resultHolder = new IngestDocument [1 ];
@@ -177,6 +177,43 @@ public void testIgnoreKeyMissing() throws Exception {
177177 }
178178 }
179179
180+ public void testExistingFieldWithOverrideDisabled () throws Exception {
181+ MockSearchFunction mockSearch = mockedSearchFunction (mapOf ("elastic.co" , mapOf ("globalRank" , 451 , "tldRank" , 23 , "tld" , "co" )));
182+ ExactMatchProcessor processor = new ExactMatchProcessor ("_tag" , mockSearch , "_name" , "domain" , false , false ,
183+ Collections .singletonList (new EnrichSpecification ("tld" , "tld" )));
184+
185+ IngestDocument ingestDocument = new IngestDocument (new HashMap <>(mapOf ("domain" , "elastic.co" , "tld" , "tld" )), mapOf ());
186+ IngestDocument [] resultHolder = new IngestDocument [1 ];
187+ Exception [] exceptionHolder = new Exception [1 ];
188+ processor .execute (ingestDocument , (result , e ) -> {
189+ resultHolder [0 ] = result ;
190+ exceptionHolder [0 ] = e ;
191+ });
192+ assertThat (exceptionHolder [0 ], nullValue ());
193+ assertThat (resultHolder [0 ].hasField ("tld" ), equalTo (true ));
194+ assertThat (resultHolder [0 ].getFieldValue ("tld" , Object .class ), equalTo ("tld" ));
195+ }
196+
197+ public void testExistingNullFieldWithOverrideDisabled () throws Exception {
198+ MockSearchFunction mockSearch = mockedSearchFunction (mapOf ("elastic.co" , mapOf ("globalRank" , 451 , "tldRank" , 23 , "tld" , "co" )));
199+ ExactMatchProcessor processor = new ExactMatchProcessor ("_tag" , mockSearch , "_name" , "domain" , false , false ,
200+ Collections .singletonList (new EnrichSpecification ("tld" , "tld" )));
201+
202+ Map <String , Object > source = new HashMap <>();
203+ source .put ("domain" , "elastic.co" );
204+ source .put ("tld" , null );
205+ IngestDocument ingestDocument = new IngestDocument (source , mapOf ());
206+ IngestDocument [] resultHolder = new IngestDocument [1 ];
207+ Exception [] exceptionHolder = new Exception [1 ];
208+ processor .execute (ingestDocument , (result , e ) -> {
209+ resultHolder [0 ] = result ;
210+ exceptionHolder [0 ] = e ;
211+ });
212+ assertThat (exceptionHolder [0 ], nullValue ());
213+ assertThat (resultHolder [0 ].hasField ("tld" ), equalTo (true ));
214+ assertThat (resultHolder [0 ].getFieldValue ("tld" , Object .class ), equalTo (null ));
215+ }
216+
180217 private static final class MockSearchFunction implements BiConsumer <SearchRequest , BiConsumer <SearchResponse , Exception >> {
181218 private final SearchResponse mockResponse ;
182219 private final SetOnce <SearchRequest > capturedRequest ;
@@ -240,4 +277,29 @@ public SearchResponse mockResponse(Map<String, Map<String, ?>> documents) {
240277 new Aggregations (Collections .emptyList ()), new Suggest (Collections .emptyList ()),
241278 false , false , null , 1 ), null , 1 , 1 , 0 , 1 , ShardSearchFailure .EMPTY_ARRAY , new SearchResponse .Clusters (1 , 1 , 0 ));
242279 }
280+
281+ static <K , V > Map <K , V > mapOf () {
282+ return Collections .emptyMap ();
283+ }
284+
285+ static <K , V > Map <K , V > mapOf (K key1 , V value1 ) {
286+ Map <K , V > map = new HashMap <>();
287+ map .put (key1 , value1 );
288+ return map ;
289+ }
290+
291+ static <K , V > Map <K , V > mapOf (K key1 , V value1 , K key2 , V value2 ) {
292+ Map <K , V > map = new HashMap <>();
293+ map .put (key1 , value1 );
294+ map .put (key2 , value2 );
295+ return map ;
296+ }
297+
298+ static Map <String , ?> mapOf (String key1 , Object value1 , String key2 , Object value2 , String key3 , Object value3 ) {
299+ Map <String , Object > map = new HashMap <>();
300+ map .put (key1 , value1 );
301+ map .put (key2 , value2 );
302+ map .put (key3 , value3 );
303+ return map ;
304+ }
243305}
0 commit comments