17
17
import java .util .SortedMap ;
18
18
19
19
import static org .elasticsearch .xpack .ml .filestructurefinder .FileStructureOverrides .EMPTY_OVERRIDES ;
20
+ import static org .elasticsearch .xpack .ml .filestructurefinder .FileStructureUtils .MAPPING_TYPE_SETTING ;
20
21
import static org .hamcrest .Matchers .contains ;
21
22
import static org .hamcrest .Matchers .equalTo ;
22
23
import static org .hamcrest .Matchers .instanceOf ;
@@ -238,26 +239,26 @@ public void testGuessMappingGivenNothing() {
238
239
}
239
240
240
241
public void testGuessMappingGivenKeyword () {
241
- Map <String , String > expected = Collections .singletonMap (FileStructureUtils . MAPPING_TYPE_SETTING , "keyword" );
242
+ Map <String , String > expected = Collections .singletonMap (MAPPING_TYPE_SETTING , "keyword" );
242
243
243
244
assertEquals (expected , guessMapping (explanation , "foo" , Arrays .asList ("ERROR" , "INFO" , "DEBUG" )));
244
245
assertEquals (expected , guessMapping (explanation , "foo" , Arrays .asList ("2018-06-11T13:26:47Z" , "not a date" )));
245
246
}
246
247
247
248
public void testGuessMappingGivenText () {
248
- Map <String , String > expected = Collections .singletonMap (FileStructureUtils . MAPPING_TYPE_SETTING , "text" );
249
+ Map <String , String > expected = Collections .singletonMap (MAPPING_TYPE_SETTING , "text" );
249
250
250
251
assertEquals (expected , guessMapping (explanation , "foo" , Arrays .asList ("a" , "the quick brown fox jumped over the lazy dog" )));
251
252
}
252
253
253
254
public void testGuessMappingGivenIp () {
254
- Map <String , String > expected = Collections .singletonMap (FileStructureUtils . MAPPING_TYPE_SETTING , "ip" );
255
+ Map <String , String > expected = Collections .singletonMap (MAPPING_TYPE_SETTING , "ip" );
255
256
256
257
assertEquals (expected , guessMapping (explanation , "foo" , Arrays .asList ("10.0.0.1" , "172.16.0.1" , "192.168.0.1" )));
257
258
}
258
259
259
260
public void testGuessMappingGivenDouble () {
260
- Map <String , String > expected = Collections .singletonMap (FileStructureUtils . MAPPING_TYPE_SETTING , "double" );
261
+ Map <String , String > expected = Collections .singletonMap (MAPPING_TYPE_SETTING , "double" );
261
262
262
263
assertEquals (expected , guessMapping (explanation , "foo" , Arrays .asList ("3.14159265359" , "0" , "-8" )));
263
264
// 12345678901234567890 is too long for long
@@ -267,39 +268,39 @@ public void testGuessMappingGivenDouble() {
267
268
}
268
269
269
270
public void testGuessMappingGivenLong () {
270
- Map <String , String > expected = Collections .singletonMap (FileStructureUtils . MAPPING_TYPE_SETTING , "long" );
271
+ Map <String , String > expected = Collections .singletonMap (MAPPING_TYPE_SETTING , "long" );
271
272
272
273
assertEquals (expected , guessMapping (explanation , "foo" , Arrays .asList ("500" , "3" , "-3" )));
273
274
assertEquals (expected , guessMapping (explanation , "foo" , Arrays .asList (500 , 6 , 0 )));
274
275
}
275
276
276
277
public void testGuessMappingGivenDate () {
277
278
Map <String , String > expected = new HashMap <>();
278
- expected .put (FileStructureUtils . MAPPING_TYPE_SETTING , "date" );
279
+ expected .put (MAPPING_TYPE_SETTING , "date" );
279
280
expected .put (FileStructureUtils .MAPPING_FORMAT_SETTING , "iso8601" );
280
281
281
282
assertEquals (expected , guessMapping (explanation , "foo" , Arrays .asList ("2018-06-11T13:26:47Z" , "2018-06-11T13:27:12Z" )));
282
283
}
283
284
284
285
public void testGuessMappingGivenBoolean () {
285
- Map <String , String > expected = Collections .singletonMap (FileStructureUtils . MAPPING_TYPE_SETTING , "boolean" );
286
+ Map <String , String > expected = Collections .singletonMap (MAPPING_TYPE_SETTING , "boolean" );
286
287
287
288
assertEquals (expected , guessMapping (explanation , "foo" , Arrays .asList ("false" , "true" )));
288
289
assertEquals (expected , guessMapping (explanation , "foo" , Arrays .asList (true , false )));
289
290
}
290
291
291
292
public void testGuessMappingGivenArray () {
292
- Map <String , String > expected = Collections .singletonMap (FileStructureUtils . MAPPING_TYPE_SETTING , "long" );
293
+ Map <String , String > expected = Collections .singletonMap (MAPPING_TYPE_SETTING , "long" );
293
294
294
295
assertEquals (expected , guessMapping (explanation , "foo" , Arrays .asList (42 , Arrays .asList (1 , -99 ))));
295
296
296
- expected = Collections .singletonMap (FileStructureUtils . MAPPING_TYPE_SETTING , "keyword" );
297
+ expected = Collections .singletonMap (MAPPING_TYPE_SETTING , "keyword" );
297
298
298
299
assertEquals (expected , guessMapping (explanation , "foo" , Arrays .asList (new String []{ "x" , "y" }, "z" )));
299
300
}
300
301
301
302
public void testGuessMappingGivenObject () {
302
- Map <String , String > expected = Collections .singletonMap (FileStructureUtils . MAPPING_TYPE_SETTING , "object" );
303
+ Map <String , String > expected = Collections .singletonMap (MAPPING_TYPE_SETTING , "object" );
303
304
304
305
assertEquals (expected , guessMapping (explanation , "foo" ,
305
306
Arrays .asList (Collections .singletonMap ("name" , "value1" ), Collections .singletonMap ("name" , "value2" ))));
@@ -330,12 +331,12 @@ public void testGuessMappingsAndCalculateFieldStats() {
330
331
331
332
Map <String , Object > mappings = mappingsAndFieldStats .v1 ();
332
333
assertNotNull (mappings );
333
- assertEquals (Collections .singletonMap (FileStructureUtils . MAPPING_TYPE_SETTING , "keyword" ), mappings .get ("foo" ));
334
+ assertEquals (Collections .singletonMap (MAPPING_TYPE_SETTING , "keyword" ), mappings .get ("foo" ));
334
335
Map <String , String > expectedTimeMapping = new HashMap <>();
335
- expectedTimeMapping .put (FileStructureUtils . MAPPING_TYPE_SETTING , "date" );
336
+ expectedTimeMapping .put (MAPPING_TYPE_SETTING , "date" );
336
337
expectedTimeMapping .put (FileStructureUtils .MAPPING_FORMAT_SETTING , "yyyy-MM-dd HH:mm:ss,SSS" );
337
338
assertEquals (expectedTimeMapping , mappings .get ("time" ));
338
- assertEquals (Collections .singletonMap (FileStructureUtils . MAPPING_TYPE_SETTING , "long" ), mappings .get ("bar" ));
339
+ assertEquals (Collections .singletonMap (MAPPING_TYPE_SETTING , "long" ), mappings .get ("bar" ));
339
340
assertNull (mappings .get ("nothing" ));
340
341
341
342
Map <String , FieldStats > fieldStats = mappingsAndFieldStats .v2 ();
@@ -446,7 +447,7 @@ public void testMakeIngestPipelineDefinitionGivenDelimitedWithConversion() {
446
447
String mappingType = expectConversion ? randomFrom ("long" , "double" , "boolean" ) : randomFrom ("keyword" , "text" , "date" );
447
448
String firstTargetField = ((List <String >) csvProcessorSettings .get ("target_fields" )).get (0 );
448
449
Map <String , Object > mappingsForConversions =
449
- Collections .singletonMap (firstTargetField , Collections .singletonMap (FileStructureUtils . MAPPING_TYPE_SETTING , mappingType ));
450
+ Collections .singletonMap (firstTargetField , Collections .singletonMap (MAPPING_TYPE_SETTING , mappingType ));
450
451
451
452
Map <String , Object > pipeline = FileStructureUtils .makeIngestPipelineDefinition (null , Collections .emptyMap (), csvProcessorSettings ,
452
453
mappingsForConversions , null , null , false );
@@ -558,6 +559,55 @@ public void testMakeIngestPipelineDefinitionGivenSemiStructured() {
558
559
assertEquals (Collections .emptyMap (), pipeline );
559
560
}
560
561
562
+ public void testGuessGeoPoint () {
563
+ Map <String , String > mapping = FileStructureUtils .guessScalarMapping (
564
+ explanation ,
565
+ "foo" ,
566
+ Arrays .asList ("POINT (-77.03653 38.897676)" , "POINT (-50.03653 28.8973)" ),
567
+ NOOP_TIMEOUT_CHECKER
568
+ );
569
+ assertThat (mapping .get (MAPPING_TYPE_SETTING ), equalTo ("geo_point" ));
570
+
571
+ mapping = FileStructureUtils .guessScalarMapping (
572
+ explanation ,
573
+ "foo" ,
574
+ Arrays .asList ("POINT (-77.03653 38.897676)" , "bar" ),
575
+ NOOP_TIMEOUT_CHECKER
576
+ );
577
+ assertThat (mapping .get (MAPPING_TYPE_SETTING ), equalTo ("keyword" ));
578
+ }
579
+
580
+ public void testGuessGeoShape () {
581
+ Map <String , String > mapping = FileStructureUtils .guessScalarMapping (
582
+ explanation ,
583
+ "foo" ,
584
+ Arrays .asList (
585
+ "POINT (-77.03653 38.897676)" ,
586
+ "LINESTRING (-77.03653 38.897676, -77.009051 38.889939)" ,
587
+ "POLYGON ((100.0 0.0, 101.0 0.0, 101.0 1.0, 100.0 1.0, 100.0 0.0))" ,
588
+ "POLYGON ((100.0 0.0, 101.0 0.0, 101.0 1.0, 100.0 1.0, 100.0 0.0), " +
589
+ "(100.2 0.2, 100.8 0.2, 100.8 0.8, 100.2 0.8, 100.2 0.2))" ,
590
+ "MULTIPOINT (102.0 2.0, 103.0 2.0)" ,
591
+ "MULTILINESTRING ((102.0 2.0, 103.0 2.0, 103.0 3.0, 102.0 3.0), (100.0 0.0, 101.0 0.0, 101.0 1.0, 100.0 1.0)," +
592
+ " (100.2 0.2, 100.8 0.2, 100.8 0.8, 100.2 0.8))" ,
593
+ "MULTIPOLYGON (((102.0 2.0, 103.0 2.0, 103.0 3.0, 102.0 3.0, 102.0 2.0)), ((100.0 0.0, 101.0 0.0, 101.0 1.0, " +
594
+ "100.0 1.0, 100.0 0.0), (100.2 0.2, 100.8 0.2, 100.8 0.8, 100.2 0.8, 100.2 0.2)))" ,
595
+ "GEOMETRYCOLLECTION (POINT (100.0 0.0), LINESTRING (101.0 0.0, 102.0 1.0))" ,
596
+ "BBOX (100.0, 102.0, 2.0, 0.0)"
597
+ ),
598
+ NOOP_TIMEOUT_CHECKER
599
+ );
600
+ assertThat (mapping .get (MAPPING_TYPE_SETTING ), equalTo ("geo_shape" ));
601
+
602
+ mapping = FileStructureUtils .guessScalarMapping (
603
+ explanation ,
604
+ "foo" ,
605
+ Arrays .asList ("POINT (-77.03653 38.897676)" , "LINESTRING (-77.03653 38.897676, -77.009051 38.889939)" , "bar" ),
606
+ NOOP_TIMEOUT_CHECKER
607
+ );
608
+ assertThat (mapping .get (MAPPING_TYPE_SETTING ), equalTo ("keyword" ));
609
+ }
610
+
561
611
private Map <String , String > guessMapping (List <String > explanation , String fieldName , List <Object > fieldValues ) {
562
612
Tuple <Map <String , String >, FieldStats > mappingAndFieldStats = FileStructureUtils .guessMappingAndCalculateFieldStats (explanation ,
563
613
fieldName , fieldValues , NOOP_TIMEOUT_CHECKER );
0 commit comments