Skip to content

Commit

Permalink
Geo Point Fieldmapper: Allow distance for geohash precision
Browse files Browse the repository at this point in the history
Even though mentioned differently in the docs, the geohash precision needed to
be an integer instead of a DistanceUnit.

Closes elastic#5448
  • Loading branch information
spinscale committed Mar 18, 2014
1 parent 0269206 commit dbdfbc8
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 1 deletion.
Expand Up @@ -226,7 +226,11 @@ public static class TypeParser implements Mapper.TypeParser {
} 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);
Expand Down Expand Up @@ -452,6 +456,10 @@ public StringFieldMapper geoHashStringMapper() {
return this.geohashMapper;
}

int geoHashPrecision() {
return geoHashPrecision;
}

public boolean isEnableLatLon() {
return enableLatLon;
}
Expand Down
Expand Up @@ -22,6 +22,7 @@
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;
Expand Down Expand Up @@ -92,4 +93,28 @@ public void testGeoHashValue() throws Exception {
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));
}
}

0 comments on commit dbdfbc8

Please sign in to comment.