[GEO] Fix orientation persistence

Fixing geo_shape field mapper to persist the orientation parameter. Also adding parsing and integration tests to ensure persistence across cluster restarts.
This commit is contained in:
Nicholas Knize 2015-03-14 17:06:59 -05:00
parent 5788289a6d
commit 1a79e1c8cc
3 changed files with 104 additions and 0 deletions

View File

@ -292,6 +292,10 @@ public class GeoShapeFieldMapper extends AbstractFieldMapper<String> {
if (includeDefaults || defaultStrategy.getDistErrPct() != Defaults.DISTANCE_ERROR_PCT) {
builder.field(Names.DISTANCE_ERROR_PCT, defaultStrategy.getDistErrPct());
}
if (includeDefaults || orientation() != Defaults.ORIENTATION) {
builder.field(Names.ORIENTATION, orientation());
}
}
@Override

View File

@ -22,6 +22,7 @@ import org.apache.lucene.spatial.prefix.PrefixTreeStrategy;
import org.apache.lucene.spatial.prefix.tree.GeohashPrefixTree;
import org.apache.lucene.spatial.prefix.tree.QuadPrefixTree;
import org.elasticsearch.common.geo.GeoUtils;
import org.elasticsearch.common.geo.builders.ShapeBuilder;
import org.elasticsearch.common.xcontent.XContentFactory;
import org.elasticsearch.index.mapper.DocumentMapper;
import org.elasticsearch.index.mapper.DocumentMapperParser;
@ -54,6 +55,46 @@ public class GeoShapeFieldMapperTests extends ElasticsearchSingleNodeTest {
assertThat(strategy.getDistErrPct(), equalTo(GeoShapeFieldMapper.Defaults.DISTANCE_ERROR_PCT));
assertThat(strategy.getGrid(), instanceOf(GeohashPrefixTree.class));
assertThat(strategy.getGrid().getMaxLevels(), equalTo(GeoShapeFieldMapper.Defaults.GEOHASH_LEVELS));
assertThat(geoShapeFieldMapper.orientation(), equalTo(GeoShapeFieldMapper.Defaults.ORIENTATION));
}
/**
* Test that orientation parameter correctly parses
* @throws IOException
*/
public void testOrientationParsing() throws IOException {
String mapping = XContentFactory.jsonBuilder().startObject().startObject("type1")
.startObject("properties").startObject("location")
.field("type", "geo_shape")
.field("orientation", "left")
.endObject().endObject()
.endObject().endObject().string();
DocumentMapper defaultMapper = createIndex("test").mapperService().documentMapperParser().parse(mapping);
FieldMapper fieldMapper = defaultMapper.mappers().name("location").mapper();
assertThat(fieldMapper, instanceOf(GeoShapeFieldMapper.class));
ShapeBuilder.Orientation orientation = ((GeoShapeFieldMapper)fieldMapper).orientation();
assertThat(orientation, equalTo(ShapeBuilder.Orientation.CLOCKWISE));
assertThat(orientation, equalTo(ShapeBuilder.Orientation.LEFT));
assertThat(orientation, equalTo(ShapeBuilder.Orientation.CW));
// explicit right orientation test
mapping = XContentFactory.jsonBuilder().startObject().startObject("type1")
.startObject("properties").startObject("location")
.field("type", "geo_shape")
.field("orientation", "right")
.endObject().endObject()
.endObject().endObject().string();
defaultMapper = createIndex("test2").mapperService().documentMapperParser().parse(mapping);
fieldMapper = defaultMapper.mappers().name("location").mapper();
assertThat(fieldMapper, instanceOf(GeoShapeFieldMapper.class));
orientation = ((GeoShapeFieldMapper)fieldMapper).orientation();
assertThat(orientation, equalTo(ShapeBuilder.Orientation.COUNTER_CLOCKWISE));
assertThat(orientation, equalTo(ShapeBuilder.Orientation.RIGHT));
assertThat(orientation, equalTo(ShapeBuilder.Orientation.CCW));
}
@Test

View File

@ -22,6 +22,10 @@ package org.elasticsearch.search.geo;
import org.apache.lucene.util.LuceneTestCase;
import org.elasticsearch.action.get.GetResponse;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.index.IndexService;
import org.elasticsearch.index.mapper.FieldMapper;
import org.elasticsearch.index.mapper.geo.GeoShapeFieldMapper;
import org.elasticsearch.indices.IndicesService;
import org.elasticsearch.test.geo.RandomShapeGenerator;
import org.elasticsearch.common.geo.ShapeRelation;
import org.elasticsearch.common.geo.builders.GeometryCollectionBuilder;
@ -456,4 +460,59 @@ public class GeoShapeIntegrationTests extends ElasticsearchIntegrationTest {
assertSearchResponse(result);
assertHitCount(result, 1);
}
/**
* Test that orientation parameter correctly persists across cluster restart
* @throws IOException
*/
public void testOrientationPersistence() throws Exception {
String idxName = "orientation";
String mapping = XContentFactory.jsonBuilder().startObject().startObject("shape")
.startObject("properties").startObject("location")
.field("type", "geo_shape")
.field("orientation", "left")
.endObject().endObject()
.endObject().endObject().string();
// create index
assertAcked(prepareCreate(idxName).addMapping("shape", mapping));
mapping = XContentFactory.jsonBuilder().startObject().startObject("shape")
.startObject("properties").startObject("location")
.field("type", "geo_shape")
.field("orientation", "right")
.endObject().endObject()
.endObject().endObject().string();
assertAcked(prepareCreate(idxName+"2").addMapping("shape", mapping));
ensureGreen();
// test mapper persistence through restart
internalCluster().fullRestart();
ensureGreen();
// left orientation test
IndicesService indicesService = internalCluster().getInstance(IndicesService.class);
IndexService indexService = indicesService.indexService(idxName);
FieldMapper fieldMapper = indexService.mapperService().smartNameFieldMapper("location");
assertThat(fieldMapper, instanceOf(GeoShapeFieldMapper.class));
GeoShapeFieldMapper gsfm = (GeoShapeFieldMapper)fieldMapper;
ShapeBuilder.Orientation orientation = gsfm.orientation();
assertThat(orientation, equalTo(ShapeBuilder.Orientation.CLOCKWISE));
assertThat(orientation, equalTo(ShapeBuilder.Orientation.LEFT));
assertThat(orientation, equalTo(ShapeBuilder.Orientation.CW));
// right orientation test
indicesService = internalCluster().getInstance(IndicesService.class);
indexService = indicesService.indexService(idxName+"2");
fieldMapper = indexService.mapperService().smartNameFieldMapper("location");
assertThat(fieldMapper, instanceOf(GeoShapeFieldMapper.class));
gsfm = (GeoShapeFieldMapper)fieldMapper;
orientation = gsfm.orientation();
assertThat(orientation, equalTo(ShapeBuilder.Orientation.COUNTER_CLOCKWISE));
assertThat(orientation, equalTo(ShapeBuilder.Orientation.RIGHT));
assertThat(orientation, equalTo(ShapeBuilder.Orientation.CCW));
}
}