From 0ca7fddb668c5e9fe30a24d097954bbe80bc26d1 Mon Sep 17 00:00:00 2001 From: Alexander Reelsen Date: Tue, 18 Mar 2014 11:08:53 +0100 Subject: [PATCH] Geo Point Fieldmapper: Allow distance for geohash precision Even though mentioned differently in the docs, the geohash precision needed to be an integer instead of a DistanceUnit. Closes #5448 --- .../index/mapper/geo/GeoPointFieldMapper.java | 10 +++++++- .../geo/GeohashMappingGeoPointTests.java | 25 +++++++++++++++++++ 2 files changed, 34 insertions(+), 1 deletion(-) diff --git a/src/main/java/org/elasticsearch/index/mapper/geo/GeoPointFieldMapper.java b/src/main/java/org/elasticsearch/index/mapper/geo/GeoPointFieldMapper.java index 2b797eef18b..a5f728eaa53 100644 --- a/src/main/java/org/elasticsearch/index/mapper/geo/GeoPointFieldMapper.java +++ b/src/main/java/org/elasticsearch/index/mapper/geo/GeoPointFieldMapper.java @@ -226,7 +226,11 @@ public class GeoPointFieldMapper extends AbstractFieldMapper implement } else if (fieldName.equals("precision_step")) { builder.precisionStep(XContentMapValues.nodeIntegerValue(fieldNode)); } else if (fieldName.equals("geohash_precision")) { - builder.geoHashPrecision(XContentMapValues.nodeIntegerValue(fieldNode)); + if (fieldNode instanceof Integer) { + builder.geoHashPrecision(XContentMapValues.nodeIntegerValue(fieldNode)); + } else { + builder.geoHashPrecision(GeoUtils.geoHashLevelsForPrecision(fieldNode.toString())); + } } else if (fieldName.equals("validate")) { builder.validateLat = XContentMapValues.nodeBooleanValue(fieldNode); builder.validateLon = XContentMapValues.nodeBooleanValue(fieldNode); @@ -452,6 +456,10 @@ public class GeoPointFieldMapper extends AbstractFieldMapper implement return this.geohashMapper; } + int geoHashPrecision() { + return geoHashPrecision; + } + public boolean isEnableLatLon() { return enableLatLon; } diff --git a/src/test/java/org/elasticsearch/index/mapper/geo/GeohashMappingGeoPointTests.java b/src/test/java/org/elasticsearch/index/mapper/geo/GeohashMappingGeoPointTests.java index 6c284c05181..ffa8900dde4 100644 --- a/src/test/java/org/elasticsearch/index/mapper/geo/GeohashMappingGeoPointTests.java +++ b/src/test/java/org/elasticsearch/index/mapper/geo/GeohashMappingGeoPointTests.java @@ -22,6 +22,7 @@ package org.elasticsearch.index.mapper.geo; import org.elasticsearch.common.geo.GeoHashUtils; import org.elasticsearch.common.xcontent.XContentFactory; import org.elasticsearch.index.mapper.DocumentMapper; +import org.elasticsearch.index.mapper.FieldMapper; import org.elasticsearch.index.mapper.MapperTestUtils; import org.elasticsearch.index.mapper.ParsedDocument; import org.elasticsearch.test.ElasticsearchTestCase; @@ -92,4 +93,28 @@ public class GeohashMappingGeoPointTests extends ElasticsearchTestCase { MatcherAssert.assertThat(doc.rootDoc().get("point.geohash"), equalTo(GeoHashUtils.encode(1.2, 1.3))); MatcherAssert.assertThat(doc.rootDoc().get("point"), notNullValue()); } + + @Test + public void testGeoHashPrecisionAsInteger() throws Exception { + String mapping = XContentFactory.jsonBuilder().startObject().startObject("type") + .startObject("properties").startObject("point").field("type", "geo_point").field("geohash_precision", 10).endObject().endObject() + .endObject().endObject().string(); + DocumentMapper defaultMapper = MapperTestUtils.newParser().parse(mapping); + FieldMapper mapper = defaultMapper.mappers().smartName("point").mapper(); + assertThat(mapper, instanceOf(GeoPointFieldMapper.class)); + GeoPointFieldMapper geoPointFieldMapper = (GeoPointFieldMapper) mapper; + assertThat(geoPointFieldMapper.geoHashPrecision(), is(10)); + } + + @Test + public void testGeoHashPrecisionAsLength() throws Exception { + String mapping = XContentFactory.jsonBuilder().startObject().startObject("type") + .startObject("properties").startObject("point").field("type", "geo_point").field("geohash_precision", "5m").endObject().endObject() + .endObject().endObject().string(); + DocumentMapper defaultMapper = MapperTestUtils.newParser().parse(mapping); + FieldMapper mapper = defaultMapper.mappers().smartName("point").mapper(); + assertThat(mapper, instanceOf(GeoPointFieldMapper.class)); + GeoPointFieldMapper geoPointFieldMapper = (GeoPointFieldMapper) mapper; + assertThat(geoPointFieldMapper.geoHashPrecision(), is(10)); + } }