GeoShapeMapper extends SingleColumnMapper<String> { public GeoShapeMapper(String field, String column, Boolean validated, Integer maxLevels, List<GeoTransformation> transformations) { super(field, column, false, validated, null, String.class, TEXT_TYPES); this.column = column == null ? field : column; if (StringUtils.isWhitespace(column)) { throw new IndexException("Column must not be whitespace, but found '{}'", column); } this.maxLevels = GeospatialUtils.validateGeohashMaxLevels(maxLevels, DEFAULT_MAX_LEVELS); SpatialPrefixTree grid = new GeohashPrefixTree(CONTEXT, this.maxLevels); RecursivePrefixTreeStrategy indexStrategy = new RecursivePrefixTreeStrategy(grid, field); SerializedDVStrategy geometryStrategy = new SerializedDVStrategy(CONTEXT, field); strategy = new CompositeSpatialStrategy(field, indexStrategy, geometryStrategy); this.transformations = transformations == null ? Collections.emptyList() : transformations; } GeoShapeMapper(String field,
                          String column,
                          Boolean validated,
                          Integer maxLevels,
                          List<GeoTransformation> transformations); @Override List<IndexableField> indexableFields(String name, String value); @Override SortField sortField(String name, boolean reverse); @Override String toString(); static final int DEFAULT_MAX_LEVELS; final String column; final int maxLevels; final CompositeSpatialStrategy strategy; final List<GeoTransformation> transformations; }
@Test public void testConstructorWithDefaultArgs() { GeoShapeMapper mapper = geoShapeMapper().build("field"); assertEquals("Field name is not properly set", "field", mapper.field); assertEquals("Column is not properly set", "field", mapper.column); assertEquals("Mapped columns are not properly set", 1, mapper.mappedColumns.size()); assertTrue("Mapped columns are not properly set", mapper.mappedColumns.contains("field")); assertEquals("Max levels is not properly set", GeoShapeMapper.DEFAULT_MAX_LEVELS, mapper.maxLevels); assertNotNull("Spatial strategy for distances is not properly set", mapper.strategy); assertNotNull("Transformations list is not properly set", mapper.transformations); assertTrue("Transformations list is not properly set", mapper.transformations.isEmpty()); }
@Test public void testConstructorWithAllArgs() { GeoShapeMapper mapper = geoShapeMapper().column("column") .validated(true) .maxLevels(10) .transformations(new Centroid(), new BBox()) .build("field"); assertEquals("Field name is not properly set", "field", mapper.field); assertTrue("Validated is not properly set", mapper.validated); assertEquals("Column is not properly set", "column", mapper.column); assertEquals("Mapped columns are not properly set", 1, mapper.mappedColumns.size()); assertTrue("Mapped columns are not properly set", mapper.mappedColumns.contains("column")); assertEquals("Max levels is not properly set", 10, mapper.maxLevels); assertNotNull("Spatial strategy for distances is not properly set", mapper.strategy); assertNotNull("Transformations list is not properly set", mapper.transformations); assertEquals("Transformations list is not properly set", 2, mapper.transformations.size()); }
@Test public void testJsonSerialization() { GeoShapeMapperBuilder builder = geoShapeMapper().column("column").maxLevels(10).transformations(new Centroid()); testJson(builder, "{type:\"geo_shape\",column:\"column\",max_levels:10,transformations:[{type:\"centroid\"}]}"); }
@Test public void testJsonSerializationDefaults() { GeoShapeMapperBuilder builder = geoShapeMapper(); testJson(builder, "{type:\"geo_shape\"}"); }
@Test public void testConstructorWithNullColumn() { geoShapeMapper().column(null).maxLevels(10).build("field"); }
@Test(expected = IndexException.class) public void testConstructorWithEmptyColumn() { geoShapeMapper().column("").maxLevels(10).build("field"); }
@Test(expected = IndexException.class) public void testConstructorWithBlankColumn() { geoShapeMapper().column(" ").maxLevels(10).build("field"); }
@Test public void testConstructorWithNullMaxLevels() { GeoShapeMapper mapper = geoShapeMapper().column("column").maxLevels(null).build("field"); assertEquals("Max levels is not properly set", GeoShapeMapper.DEFAULT_MAX_LEVELS, mapper.maxLevels); }
@Test(expected = IndexException.class) public void testConstructorWithZeroMaxLevels() { geoShapeMapper().column("column").maxLevels(0).build("field"); }
@Test(expected = IndexException.class) public void testConstructorWithNegativeMaxLevels() { geoShapeMapper().column("column").maxLevels(-1).build("field"); }
@Test(expected = IndexException.class) public void testConstructorWithTooLargeMaxLevels() { geoShapeMapper().column("column").maxLevels(25).build("field"); }
@Test(expected = IndexException.class) public void testConstructorWithNullField() { geoShapeMapper().column("column").maxLevels(10).build(null); }
@Test(expected = IndexException.class) public void testConstructorWithEmptyField() { geoShapeMapper().column("column").maxLevels(10).build(""); }
@Test(expected = IndexException.class) public void testConstructorWithBlankField() { geoShapeMapper().column("column").maxLevels(10).build(" "); }
@Test public void testGetShapeFromString() { GeoShapeMapper mapper = geoShapeMapper().column("column").maxLevels(10).build("field"); assertEquals("Latitude is not properly parsed", "POLYGON", mapper.base("column", "POLYGON")); }
@Test public void testExtractAnalyzers() { GeoShapeMapper mapper = geoShapeMapper().column("column").maxLevels(10).build("field"); assertNull("Analyzer must be null", mapper.analyzer); }