Mapper: geo_point type to allow to set store on the mapping, closes #359.

This commit is contained in:
kimchy 2010-09-07 14:10:29 +03:00
parent 7c48f27e60
commit c045b4d0ef
2 changed files with 119 additions and 12 deletions

View File

@ -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<Builder, XContentGeoPointFieldMapper> {
@ -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);

View File

@ -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());
}
}