diff --git a/modules/elasticsearch/src/main/java/org/elasticsearch/index/mapper/xcontent/XContentGeoPointFieldMapper.java b/modules/elasticsearch/src/main/java/org/elasticsearch/index/mapper/xcontent/XContentGeoPointFieldMapper.java index 03ffcc1d323..8bbb2e5e6e3 100644 --- a/modules/elasticsearch/src/main/java/org/elasticsearch/index/mapper/xcontent/XContentGeoPointFieldMapper.java +++ b/modules/elasticsearch/src/main/java/org/elasticsearch/index/mapper/xcontent/XContentGeoPointFieldMapper.java @@ -19,6 +19,7 @@ package org.elasticsearch.index.mapper.xcontent; +import org.apache.lucene.document.Field; import org.elasticsearch.ElasticSearchIllegalArgumentException; import org.elasticsearch.common.Strings; import org.elasticsearch.common.lucene.geo.GeoHashUtils; @@ -62,6 +63,7 @@ public class XContentGeoPointFieldMapper implements XContentMapper { public static class Defaults { public static final ContentPath.Type PATH_TYPE = ContentPath.Type.FULL; + public static final Field.Store STORE = Field.Store.NO; } public static class Builder extends XContentMapper.Builder { @@ -78,6 +80,8 @@ public class XContentGeoPointFieldMapper implements XContentMapper { private int geohashPrecision = GeoHashUtils.PRECISION; + private Field.Store store = Defaults.STORE; + public Builder(String name) { super(name); this.builder = this; @@ -113,6 +117,11 @@ public class XContentGeoPointFieldMapper implements XContentMapper { return this; } + public Builder store(Field.Store store) { + this.store = store; + return this; + } + @Override public XContentGeoPointFieldMapper build(BuilderContext context) { ContentPath.Type origPathType = context.path().pathType(); context.path().pathType(pathType); @@ -138,8 +147,8 @@ public class XContentGeoPointFieldMapper implements XContentMapper { latMapperBuilder.precisionStep(precisionStep); lonMapperBuilder.precisionStep(precisionStep); } - latMapper = (XContentNumberFieldMapper) latMapperBuilder.includeInAll(false).build(context); - lonMapper = (XContentNumberFieldMapper) lonMapperBuilder.includeInAll(false).build(context); + latMapper = (XContentNumberFieldMapper) latMapperBuilder.includeInAll(false).store(store).build(context); + lonMapper = (XContentNumberFieldMapper) lonMapperBuilder.includeInAll(false).store(store).build(context); } if (enableGeohash) { geohashMapper = stringField(Names.GEOHASH).includeInAll(false).build(context); @@ -161,6 +170,8 @@ public class XContentGeoPointFieldMapper implements XContentMapper { Object fieldNode = entry.getValue(); if (fieldName.equals("path")) { builder.pathType(parsePathType(name, fieldNode.toString())); + } else if (fieldName.equals("store")) { + builder.store(parseStore(name, fieldNode.toString())); } else if (fieldName.equals("lat_lon")) { builder.enableLatLon(XContentMapValues.nodeBooleanValue(fieldNode)); } else if (fieldName.equals("geohash")) { @@ -330,6 +341,7 @@ public class XContentGeoPointFieldMapper implements XContentMapper { builder.field("lat_lon", enableLatLon); builder.field("geohash", enableGeohash); builder.field("resolution", resolution); + builder.field("store", latMapper.name().toLowerCase()); builder.field("geohash_precision", geohashPrecision); if (precisionStep != null) { builder.field("precision_step", precisionStep); diff --git a/modules/elasticsearch/src/test/java/org/elasticsearch/index/mapper/xcontent/geopoint/LatLonMappingGeoPointTests.java b/modules/elasticsearch/src/test/java/org/elasticsearch/index/mapper/xcontent/geopoint/LatLonMappingGeoPointTests.java index 956984fa81a..d6b8c7dd006 100644 --- a/modules/elasticsearch/src/test/java/org/elasticsearch/index/mapper/xcontent/geopoint/LatLonMappingGeoPointTests.java +++ b/modules/elasticsearch/src/test/java/org/elasticsearch/index/mapper/xcontent/geopoint/LatLonMappingGeoPointTests.java @@ -19,15 +19,16 @@ package org.elasticsearch.index.mapper.xcontent.geopoint; +import org.elasticsearch.common.Numbers; import org.elasticsearch.common.lucene.geo.GeoHashUtils; import org.elasticsearch.common.xcontent.XContentFactory; import org.elasticsearch.common.xcontent.XContentType; import org.elasticsearch.index.mapper.ParsedDocument; import org.elasticsearch.index.mapper.xcontent.XContentDocumentMapper; import org.elasticsearch.index.mapper.xcontent.XContentMapperTests; -import org.hamcrest.MatcherAssert; import org.testng.annotations.Test; +import static org.hamcrest.MatcherAssert.*; import static org.hamcrest.Matchers.*; /** @@ -48,9 +49,57 @@ public class LatLonMappingGeoPointTests { .endObject() .copiedBytes()); - MatcherAssert.assertThat(doc.doc().getField("point.lat"), notNullValue()); - MatcherAssert.assertThat(doc.doc().getField("point.lon"), notNullValue()); - MatcherAssert.assertThat(doc.doc().getField("point.geohash"), nullValue()); + assertThat(doc.doc().getField("point.lat"), notNullValue()); + assertThat(doc.doc().getField("point.lat").getBinaryValue(), nullValue()); + assertThat(doc.doc().getField("point.lon"), notNullValue()); + assertThat(doc.doc().getField("point.lon").getBinaryValue(), nullValue()); + assertThat(doc.doc().getField("point.geohash"), nullValue()); + } + + @Test public void testLatLonValuesStored() throws Exception { + String mapping = XContentFactory.contentTextBuilder(XContentType.JSON).startObject().startObject("type") + .startObject("properties").startObject("point").field("type", "geo_point").field("store", "yes").endObject().endObject() + .endObject().endObject().string(); + + XContentDocumentMapper defaultMapper = XContentMapperTests.newParser().parse(mapping); + + ParsedDocument doc = defaultMapper.parse("type", "1", XContentFactory.jsonBuilder() + .startObject() + .startObject("point").field("lat", 1.2).field("lon", 1.3).endObject() + .endObject() + .copiedBytes()); + + assertThat(doc.doc().getField("point.lat"), notNullValue()); + assertThat(doc.doc().getField("point.lat").getBinaryValue(), equalTo(Numbers.doubleToBytes(1.2))); + assertThat(doc.doc().getField("point.lon"), notNullValue()); + assertThat(doc.doc().getField("point.lon").getBinaryValue(), equalTo(Numbers.doubleToBytes(1.3))); + assertThat(doc.doc().getField("point.geohash"), nullValue()); + } + + @Test public void testArrayLatLonValues() throws Exception { + String mapping = XContentFactory.contentTextBuilder(XContentType.JSON).startObject().startObject("type") + .startObject("properties").startObject("point").field("type", "geo_point").field("store", "yes").endObject().endObject() + .endObject().endObject().string(); + + XContentDocumentMapper defaultMapper = XContentMapperTests.newParser().parse(mapping); + + ParsedDocument doc = defaultMapper.parse("type", "1", XContentFactory.jsonBuilder() + .startObject() + .startArray("point") + .startObject().field("lat", 1.2).field("lon", 1.3).endObject() + .startObject().field("lat", 1.4).field("lon", 1.5).endObject() + .endArray() + .endObject() + .copiedBytes()); + + assertThat(doc.doc().getFields("point.lat").length, equalTo(2)); + assertThat(doc.doc().getFields("point.lon").length, equalTo(2)); + assertThat(doc.doc().getFields("point.lat")[0].getBinaryValue(), equalTo(Numbers.doubleToBytes(1.2))); + assertThat(doc.doc().getFields("point.lon")[0].getBinaryValue(), equalTo(Numbers.doubleToBytes(1.3))); + assertThat(doc.doc().getFields("point.lat")[1].getBinaryValue(), equalTo(Numbers.doubleToBytes(1.4))); + assertThat(doc.doc().getFields("point.lon")[1].getBinaryValue(), equalTo(Numbers.doubleToBytes(1.5))); + + assertThat(doc.doc().getField("point.geohash"), nullValue()); } @Test public void testLatLonInOneValue() throws Exception { @@ -66,9 +115,55 @@ public class LatLonMappingGeoPointTests { .endObject() .copiedBytes()); - MatcherAssert.assertThat(doc.doc().getField("point.lat"), notNullValue()); - MatcherAssert.assertThat(doc.doc().getField("point.lon"), notNullValue()); - MatcherAssert.assertThat(doc.doc().getField("point.geohash"), nullValue()); + assertThat(doc.doc().getField("point.lat"), notNullValue()); + assertThat(doc.doc().getField("point.lon"), notNullValue()); + assertThat(doc.doc().getField("point.geohash"), nullValue()); + } + + @Test public void testLatLonInOneValueStored() throws Exception { + String mapping = XContentFactory.contentTextBuilder(XContentType.JSON).startObject().startObject("type") + .startObject("properties").startObject("point").field("type", "geo_point").field("store", "yes").endObject().endObject() + .endObject().endObject().string(); + + XContentDocumentMapper defaultMapper = XContentMapperTests.newParser().parse(mapping); + + ParsedDocument doc = defaultMapper.parse("type", "1", XContentFactory.jsonBuilder() + .startObject() + .field("point", "1.2,1.3") + .endObject() + .copiedBytes()); + + assertThat(doc.doc().getField("point.lat"), notNullValue()); + assertThat(doc.doc().getField("point.lat").getBinaryValue(), equalTo(Numbers.doubleToBytes(1.2))); + assertThat(doc.doc().getField("point.lon"), notNullValue()); + assertThat(doc.doc().getField("point.lon").getBinaryValue(), equalTo(Numbers.doubleToBytes(1.3))); + assertThat(doc.doc().getField("point.geohash"), nullValue()); + } + + @Test public void testLatLonInOneValueArray() throws Exception { + String mapping = XContentFactory.contentTextBuilder(XContentType.JSON).startObject().startObject("type") + .startObject("properties").startObject("point").field("type", "geo_point").field("store", "yes").endObject().endObject() + .endObject().endObject().string(); + + XContentDocumentMapper defaultMapper = XContentMapperTests.newParser().parse(mapping); + + ParsedDocument doc = defaultMapper.parse("type", "1", XContentFactory.jsonBuilder() + .startObject() + .startArray("point") + .value("1.2,1.3") + .value("1.4,1.5") + .endArray() + .endObject() + .copiedBytes()); + + assertThat(doc.doc().getFields("point.lat").length, equalTo(2)); + assertThat(doc.doc().getFields("point.lon").length, equalTo(2)); + assertThat(doc.doc().getFields("point.lat")[0].getBinaryValue(), equalTo(Numbers.doubleToBytes(1.2))); + assertThat(doc.doc().getFields("point.lon")[0].getBinaryValue(), equalTo(Numbers.doubleToBytes(1.3))); + assertThat(doc.doc().getFields("point.lat")[1].getBinaryValue(), equalTo(Numbers.doubleToBytes(1.4))); + assertThat(doc.doc().getFields("point.lon")[1].getBinaryValue(), equalTo(Numbers.doubleToBytes(1.5))); + + assertThat(doc.doc().getField("point.geohash"), nullValue()); } @Test public void testGeoHashValue() throws Exception { @@ -84,8 +179,8 @@ public class LatLonMappingGeoPointTests { .endObject() .copiedBytes()); - MatcherAssert.assertThat(doc.doc().getField("point.lat"), notNullValue()); - MatcherAssert.assertThat(doc.doc().getField("point.lon"), notNullValue()); - MatcherAssert.assertThat(doc.doc().getField("point.geohash"), nullValue()); + assertThat(doc.doc().getField("point.lat"), notNullValue()); + assertThat(doc.doc().getField("point.lon"), notNullValue()); + assertThat(doc.doc().getField("point.geohash"), nullValue()); } }