Make GeoShapeQueryBuilder serialize shape
This commit is contained in:
parent
2a810d680d
commit
f07e61c05b
|
@ -36,7 +36,7 @@ public class CircleBuilder extends ShapeBuilder {
|
|||
public static final String FIELD_RADIUS = "radius";
|
||||
public static final GeoShapeType TYPE = GeoShapeType.CIRCLE;
|
||||
|
||||
static final CircleBuilder PROTOTYPE = new CircleBuilder();
|
||||
public static final CircleBuilder PROTOTYPE = new CircleBuilder();
|
||||
|
||||
private DistanceUnit unit;
|
||||
private double radius;
|
||||
|
|
|
@ -25,7 +25,6 @@ import com.vividsolutions.jts.geom.Coordinate;
|
|||
import org.elasticsearch.common.io.stream.StreamInput;
|
||||
import org.elasticsearch.common.io.stream.StreamOutput;
|
||||
import org.elasticsearch.common.xcontent.XContentBuilder;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.Locale;
|
||||
import java.util.Objects;
|
||||
|
@ -34,7 +33,7 @@ public class EnvelopeBuilder extends ShapeBuilder {
|
|||
|
||||
public static final GeoShapeType TYPE = GeoShapeType.ENVELOPE;
|
||||
|
||||
static final EnvelopeBuilder PROTOTYPE = new EnvelopeBuilder();
|
||||
public static final EnvelopeBuilder PROTOTYPE = new EnvelopeBuilder();
|
||||
|
||||
protected Coordinate topLeft;
|
||||
protected Coordinate bottomRight;
|
||||
|
|
|
@ -38,7 +38,7 @@ public class LineStringBuilder extends PointCollection<LineStringBuilder> {
|
|||
|
||||
public static final GeoShapeType TYPE = GeoShapeType.LINESTRING;
|
||||
|
||||
static final LineStringBuilder PROTOTYPE = new LineStringBuilder();
|
||||
public static final LineStringBuilder PROTOTYPE = new LineStringBuilder();
|
||||
|
||||
@Override
|
||||
public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException {
|
||||
|
@ -148,7 +148,7 @@ public class LineStringBuilder extends PointCollection<LineStringBuilder> {
|
|||
|
||||
@Override
|
||||
public int hashCode() {
|
||||
return Objects.hash(points, translated);
|
||||
return Objects.hash(points, translated());
|
||||
}
|
||||
|
||||
@Override
|
||||
|
@ -161,7 +161,7 @@ public class LineStringBuilder extends PointCollection<LineStringBuilder> {
|
|||
}
|
||||
LineStringBuilder other = (LineStringBuilder) obj;
|
||||
return Objects.equals(points, other.points) &&
|
||||
(translated == other.translated);
|
||||
(translated() == other.translated());
|
||||
}
|
||||
|
||||
@Override
|
||||
|
@ -170,7 +170,7 @@ public class LineStringBuilder extends PointCollection<LineStringBuilder> {
|
|||
for (Coordinate point : points) {
|
||||
writeCoordinateTo(point, out);
|
||||
}
|
||||
out.writeBoolean(translated);
|
||||
out.writeBoolean(translated());
|
||||
}
|
||||
|
||||
@Override
|
||||
|
@ -180,7 +180,7 @@ public class LineStringBuilder extends PointCollection<LineStringBuilder> {
|
|||
for (int i=0; i < size; i++) {
|
||||
lineStringBuilder.point(readCoordinateFrom(in));
|
||||
}
|
||||
lineStringBuilder.translated = in.readBoolean();
|
||||
lineStringBuilder.translated(in.readBoolean());
|
||||
return lineStringBuilder;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -37,7 +37,7 @@ public class MultiLineStringBuilder extends ShapeBuilder {
|
|||
|
||||
public static final GeoShapeType TYPE = GeoShapeType.MULTILINESTRING;
|
||||
|
||||
static final MultiLineStringBuilder PROTOTYPE = new MultiLineStringBuilder();
|
||||
public static final MultiLineStringBuilder PROTOTYPE = new MultiLineStringBuilder();
|
||||
|
||||
private final ArrayList<LineStringBuilder> lines = new ArrayList<>();
|
||||
|
||||
|
|
|
@ -36,7 +36,7 @@ public class MultiPointBuilder extends PointCollection<MultiPointBuilder> {
|
|||
|
||||
public static final GeoShapeType TYPE = GeoShapeType.MULTIPOINT;
|
||||
|
||||
final static MultiPointBuilder PROTOTYPE = new MultiPointBuilder();
|
||||
public final static MultiPointBuilder PROTOTYPE = new MultiPointBuilder();
|
||||
|
||||
@Override
|
||||
public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException {
|
||||
|
|
|
@ -36,7 +36,7 @@ import com.vividsolutions.jts.geom.Coordinate;
|
|||
public class MultiPolygonBuilder extends ShapeBuilder {
|
||||
|
||||
public static final GeoShapeType TYPE = GeoShapeType.MULTIPOLYGON;
|
||||
static final MultiPolygonBuilder PROTOTYPE = new MultiPolygonBuilder();
|
||||
public static final MultiPolygonBuilder PROTOTYPE = new MultiPolygonBuilder();
|
||||
|
||||
private final ArrayList<PolygonBuilder> polygons = new ArrayList<>();
|
||||
|
||||
|
|
|
@ -32,7 +32,7 @@ import com.vividsolutions.jts.geom.Coordinate;
|
|||
public class PointBuilder extends ShapeBuilder {
|
||||
|
||||
public static final GeoShapeType TYPE = GeoShapeType.POINT;
|
||||
static final PointBuilder PROTOTYPE = new PointBuilder();
|
||||
public static final PointBuilder PROTOTYPE = new PointBuilder();
|
||||
|
||||
private Coordinate coordinate;
|
||||
|
||||
|
|
|
@ -53,7 +53,7 @@ import java.util.Objects;
|
|||
public class PolygonBuilder extends ShapeBuilder {
|
||||
|
||||
public static final GeoShapeType TYPE = GeoShapeType.POLYGON;
|
||||
static final PolygonBuilder PROTOTYPE = new PolygonBuilder();
|
||||
public static final PolygonBuilder PROTOTYPE = new PolygonBuilder();
|
||||
|
||||
private static final Coordinate[][] EMPTY = new Coordinate[0][];
|
||||
|
||||
|
|
|
@ -708,15 +708,4 @@ public abstract class ShapeBuilder extends ToXContentToBytes implements NamedWri
|
|||
public String getWriteableName() {
|
||||
return type().shapeName();
|
||||
}
|
||||
|
||||
// NORELEASE this should be deleted as soon as all shape builders implement writable
|
||||
@Override
|
||||
public void writeTo(StreamOutput out) throws IOException {
|
||||
}
|
||||
|
||||
// NORELEASE this should be deleted as soon as all shape builders implement writable
|
||||
@Override
|
||||
public ShapeBuilder readFrom(StreamInput in) throws IOException {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -0,0 +1,42 @@
|
|||
/*
|
||||
* Licensed to Elasticsearch under one or more contributor
|
||||
* license agreements. See the NOTICE file distributed with
|
||||
* this work for additional information regarding copyright
|
||||
* ownership. Elasticsearch licenses this file to you under
|
||||
* the Apache License, Version 2.0 (the "License"); you may
|
||||
* not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing,
|
||||
* software distributed under the License is distributed on an
|
||||
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
||||
* KIND, either express or implied. See the License for the
|
||||
* specific language governing permissions and limitations
|
||||
* under the License.
|
||||
*/
|
||||
|
||||
package org.elasticsearch.common.geo.builders;
|
||||
|
||||
import org.elasticsearch.common.inject.Inject;
|
||||
import org.elasticsearch.common.io.stream.NamedWriteableRegistry;
|
||||
|
||||
/**
|
||||
* Register the shape builder prototypes with the {@link NamedWriteableRegistry}
|
||||
*/
|
||||
public class ShapeBuilderRegistry {
|
||||
|
||||
@Inject
|
||||
public ShapeBuilderRegistry(NamedWriteableRegistry namedWriteableRegistry) {
|
||||
namedWriteableRegistry.registerPrototype(ShapeBuilder.class, PointBuilder.PROTOTYPE);
|
||||
namedWriteableRegistry.registerPrototype(ShapeBuilder.class, CircleBuilder.PROTOTYPE);
|
||||
namedWriteableRegistry.registerPrototype(ShapeBuilder.class, EnvelopeBuilder.PROTOTYPE);
|
||||
namedWriteableRegistry.registerPrototype(ShapeBuilder.class, MultiPointBuilder.PROTOTYPE);
|
||||
namedWriteableRegistry.registerPrototype(ShapeBuilder.class, LineStringBuilder.PROTOTYPE);
|
||||
namedWriteableRegistry.registerPrototype(ShapeBuilder.class, MultiLineStringBuilder.PROTOTYPE);
|
||||
namedWriteableRegistry.registerPrototype(ShapeBuilder.class, PolygonBuilder.PROTOTYPE);
|
||||
namedWriteableRegistry.registerPrototype(ShapeBuilder.class, MultiPolygonBuilder.PROTOTYPE);
|
||||
namedWriteableRegistry.registerPrototype(ShapeBuilder.class, GeometryCollectionBuilder.PROTOTYPE);
|
||||
}
|
||||
}
|
|
@ -31,19 +31,16 @@ import org.elasticsearch.action.get.GetRequest;
|
|||
import org.elasticsearch.action.get.GetResponse;
|
||||
import org.elasticsearch.client.Client;
|
||||
import org.elasticsearch.common.Strings;
|
||||
import org.elasticsearch.common.bytes.BytesArray;
|
||||
import org.elasticsearch.common.bytes.BytesReference;
|
||||
import org.elasticsearch.common.geo.ShapeRelation;
|
||||
import org.elasticsearch.common.geo.ShapesAvailability;
|
||||
import org.elasticsearch.common.geo.SpatialStrategy;
|
||||
import org.elasticsearch.common.geo.builders.PointBuilder;
|
||||
import org.elasticsearch.common.geo.builders.ShapeBuilder;
|
||||
import org.elasticsearch.common.io.stream.StreamInput;
|
||||
import org.elasticsearch.common.io.stream.StreamOutput;
|
||||
import org.elasticsearch.common.xcontent.XContentBuilder;
|
||||
import org.elasticsearch.common.xcontent.XContentFactory;
|
||||
import org.elasticsearch.common.xcontent.XContentHelper;
|
||||
import org.elasticsearch.common.xcontent.XContentParser;
|
||||
import org.elasticsearch.common.xcontent.XContentType;
|
||||
import org.elasticsearch.index.mapper.MappedFieldType;
|
||||
import org.elasticsearch.index.mapper.geo.GeoShapeFieldMapper;
|
||||
import org.elasticsearch.search.internal.SearchContext;
|
||||
|
@ -61,13 +58,11 @@ public class GeoShapeQueryBuilder extends AbstractQueryBuilder<GeoShapeQueryBuil
|
|||
public static final String DEFAULT_SHAPE_FIELD_NAME = "shape";
|
||||
public static final ShapeRelation DEFAULT_SHAPE_RELATION = ShapeRelation.INTERSECTS;
|
||||
|
||||
static final GeoShapeQueryBuilder PROTOTYPE = new GeoShapeQueryBuilder("field", new BytesArray(new byte[1]));
|
||||
static final GeoShapeQueryBuilder PROTOTYPE = new GeoShapeQueryBuilder("field", new PointBuilder());
|
||||
|
||||
private final String fieldName;
|
||||
|
||||
// TODO make the ShapeBuilder and subclasses Writable and implement hashCode
|
||||
// and Equals so ShapeBuilder can be used here
|
||||
private BytesReference shapeBytes;
|
||||
private ShapeBuilder shape;
|
||||
|
||||
private SpatialStrategy strategy;
|
||||
|
||||
|
@ -88,7 +83,7 @@ public class GeoShapeQueryBuilder extends AbstractQueryBuilder<GeoShapeQueryBuil
|
|||
* @param shape
|
||||
* Shape used in the Query
|
||||
*/
|
||||
public GeoShapeQueryBuilder(String fieldName, ShapeBuilder shape) throws IOException {
|
||||
public GeoShapeQueryBuilder(String fieldName, ShapeBuilder shape) {
|
||||
this(fieldName, shape, null, null);
|
||||
}
|
||||
|
||||
|
@ -105,37 +100,21 @@ public class GeoShapeQueryBuilder extends AbstractQueryBuilder<GeoShapeQueryBuil
|
|||
* Index type of the indexed Shapes
|
||||
*/
|
||||
public GeoShapeQueryBuilder(String fieldName, String indexedShapeId, String indexedShapeType) {
|
||||
this(fieldName, (BytesReference) null, indexedShapeId, indexedShapeType);
|
||||
this(fieldName, (ShapeBuilder) null, indexedShapeId, indexedShapeType);
|
||||
}
|
||||
|
||||
GeoShapeQueryBuilder(String fieldName, BytesReference shapeBytes) {
|
||||
this(fieldName, shapeBytes, null, null);
|
||||
}
|
||||
|
||||
private GeoShapeQueryBuilder(String fieldName, ShapeBuilder shape, String indexedShapeId, String indexedShapeType) throws IOException {
|
||||
this(fieldName, new BytesArray(new byte[1]), indexedShapeId, indexedShapeType);
|
||||
if (shape != null) {
|
||||
this.shapeBytes = shape.buildAsBytes(XContentType.JSON);
|
||||
if (this.shapeBytes.length() == 0) {
|
||||
throw new IllegalArgumentException("shape must not be empty");
|
||||
}
|
||||
} else {
|
||||
throw new IllegalArgumentException("shape must not be null");
|
||||
}
|
||||
}
|
||||
|
||||
private GeoShapeQueryBuilder(String fieldName, BytesReference shapeBytes, String indexedShapeId, String indexedShapeType) {
|
||||
private GeoShapeQueryBuilder(String fieldName, ShapeBuilder shape, String indexedShapeId, String indexedShapeType) {
|
||||
if (fieldName == null) {
|
||||
throw new IllegalArgumentException("fieldName is required");
|
||||
}
|
||||
if ((shapeBytes == null || shapeBytes.length() == 0) && indexedShapeId == null) {
|
||||
if (shape == null && indexedShapeId == null) {
|
||||
throw new IllegalArgumentException("either shapeBytes or indexedShapeId and indexedShapeType are required");
|
||||
}
|
||||
if (indexedShapeId != null && indexedShapeType == null) {
|
||||
throw new IllegalArgumentException("indexedShapeType is required if indexedShapeId is specified");
|
||||
}
|
||||
this.fieldName = fieldName;
|
||||
this.shapeBytes = shapeBytes;
|
||||
this.shape = shape;
|
||||
this.indexedShapeId = indexedShapeId;
|
||||
this.indexedShapeType = indexedShapeType;
|
||||
}
|
||||
|
@ -148,10 +127,10 @@ public class GeoShapeQueryBuilder extends AbstractQueryBuilder<GeoShapeQueryBuil
|
|||
}
|
||||
|
||||
/**
|
||||
* @return the JSON bytes for the shape used in the Query
|
||||
* @return the shape used in the Query
|
||||
*/
|
||||
public BytesReference shapeBytes() {
|
||||
return shapeBytes;
|
||||
public ShapeBuilder shape() {
|
||||
return shape;
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -258,15 +237,11 @@ public class GeoShapeQueryBuilder extends AbstractQueryBuilder<GeoShapeQueryBuil
|
|||
|
||||
@Override
|
||||
protected Query doToQuery(QueryShardContext context) throws IOException {
|
||||
ShapeBuilder shape;
|
||||
if (shapeBytes == null) {
|
||||
ShapeBuilder shapeToQuery = shape;
|
||||
if (shapeToQuery == null) {
|
||||
GetRequest getRequest = new GetRequest(indexedShapeIndex, indexedShapeType, indexedShapeId);
|
||||
getRequest.copyContextAndHeadersFrom(SearchContext.current());
|
||||
shape = fetch(context.getClient(), getRequest, indexedShapePath);
|
||||
} else {
|
||||
XContentParser shapeParser = XContentHelper.createParser(shapeBytes);
|
||||
shapeParser.nextToken();
|
||||
shape = ShapeBuilder.parse(shapeParser);
|
||||
shapeToQuery = fetch(context.getClient(), getRequest, indexedShapePath);
|
||||
}
|
||||
MappedFieldType fieldType = context.fieldMapper(fieldName);
|
||||
if (fieldType == null) {
|
||||
|
@ -291,12 +266,12 @@ public class GeoShapeQueryBuilder extends AbstractQueryBuilder<GeoShapeQueryBuil
|
|||
// in this case, execute disjoint as exists && !intersects
|
||||
BooleanQuery.Builder bool = new BooleanQuery.Builder();
|
||||
Query exists = ExistsQueryBuilder.newFilter(context, fieldName);
|
||||
Query intersects = strategy.makeQuery(getArgs(shape, ShapeRelation.INTERSECTS));
|
||||
Query intersects = strategy.makeQuery(getArgs(shapeToQuery, ShapeRelation.INTERSECTS));
|
||||
bool.add(exists, BooleanClause.Occur.MUST);
|
||||
bool.add(intersects, BooleanClause.Occur.MUST_NOT);
|
||||
query = new ConstantScoreQuery(bool.build());
|
||||
} else {
|
||||
query = new ConstantScoreQuery(strategy.makeQuery(getArgs(shape, relation)));
|
||||
query = new ConstantScoreQuery(strategy.makeQuery(getArgs(shapeToQuery, relation)));
|
||||
}
|
||||
return query;
|
||||
}
|
||||
|
@ -378,11 +353,9 @@ public class GeoShapeQueryBuilder extends AbstractQueryBuilder<GeoShapeQueryBuil
|
|||
builder.field(GeoShapeQueryParser.STRATEGY_FIELD.getPreferredName(), strategy.getStrategyName());
|
||||
}
|
||||
|
||||
if (shapeBytes != null) {
|
||||
if (shape != null) {
|
||||
builder.field(GeoShapeQueryParser.SHAPE_FIELD.getPreferredName());
|
||||
XContentParser parser = XContentFactory.xContent(XContentType.JSON).createParser(shapeBytes);
|
||||
parser.nextToken();
|
||||
builder.copyCurrentStructure(parser);
|
||||
shape.toXContent(builder, params);
|
||||
} else {
|
||||
builder.startObject(GeoShapeQueryParser.INDEXED_SHAPE_FIELD.getPreferredName())
|
||||
.field(GeoShapeQueryParser.SHAPE_ID_FIELD.getPreferredName(), indexedShapeId)
|
||||
|
@ -412,8 +385,7 @@ public class GeoShapeQueryBuilder extends AbstractQueryBuilder<GeoShapeQueryBuil
|
|||
String fieldName = in.readString();
|
||||
GeoShapeQueryBuilder builder;
|
||||
if (in.readBoolean()) {
|
||||
BytesReference shapeBytes = in.readBytesReference();
|
||||
builder = new GeoShapeQueryBuilder(fieldName, shapeBytes);
|
||||
builder = new GeoShapeQueryBuilder(fieldName, in.readShape());
|
||||
} else {
|
||||
String indexedShapeId = in.readOptionalString();
|
||||
String indexedShapeType = in.readOptionalString();
|
||||
|
@ -437,10 +409,10 @@ public class GeoShapeQueryBuilder extends AbstractQueryBuilder<GeoShapeQueryBuil
|
|||
@Override
|
||||
protected void doWriteTo(StreamOutput out) throws IOException {
|
||||
out.writeString(fieldName);
|
||||
boolean hasShapeBytes = shapeBytes != null;
|
||||
out.writeBoolean(hasShapeBytes);
|
||||
if (hasShapeBytes) {
|
||||
out.writeBytesReference(shapeBytes);
|
||||
boolean hasShape = shape != null;
|
||||
out.writeBoolean(hasShape);
|
||||
if (hasShape) {
|
||||
out.writeShape(shape);
|
||||
} else {
|
||||
out.writeOptionalString(indexedShapeId);
|
||||
out.writeOptionalString(indexedShapeType);
|
||||
|
@ -464,14 +436,14 @@ public class GeoShapeQueryBuilder extends AbstractQueryBuilder<GeoShapeQueryBuil
|
|||
&& Objects.equals(indexedShapePath, other.indexedShapePath)
|
||||
&& Objects.equals(indexedShapeType, other.indexedShapeType)
|
||||
&& Objects.equals(relation, other.relation)
|
||||
&& Objects.equals(shapeBytes, other.shapeBytes)
|
||||
&& Objects.equals(shape, other.shape)
|
||||
&& Objects.equals(strategy, other.strategy);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected int doHashCode() {
|
||||
return Objects.hash(fieldName, indexedShapeId, indexedShapeIndex,
|
||||
indexedShapePath, indexedShapeType, relation, shapeBytes, strategy);
|
||||
indexedShapePath, indexedShapeType, relation, shape, strategy);
|
||||
}
|
||||
|
||||
@Override
|
||||
|
|
|
@ -22,11 +22,9 @@ package org.elasticsearch.index.query;
|
|||
import org.elasticsearch.common.ParseField;
|
||||
import org.elasticsearch.common.ParsingException;
|
||||
import org.elasticsearch.common.Strings;
|
||||
import org.elasticsearch.common.bytes.BytesReference;
|
||||
import org.elasticsearch.common.geo.ShapeRelation;
|
||||
import org.elasticsearch.common.geo.SpatialStrategy;
|
||||
import org.elasticsearch.common.xcontent.XContentBuilder;
|
||||
import org.elasticsearch.common.xcontent.XContentFactory;
|
||||
import org.elasticsearch.common.geo.builders.ShapeBuilder;
|
||||
import org.elasticsearch.common.xcontent.XContentParser;
|
||||
|
||||
import java.io.IOException;
|
||||
|
@ -54,7 +52,7 @@ public class GeoShapeQueryParser implements QueryParser<GeoShapeQueryBuilder> {
|
|||
String fieldName = null;
|
||||
ShapeRelation shapeRelation = null;
|
||||
SpatialStrategy strategy = null;
|
||||
BytesReference shape = null;
|
||||
ShapeBuilder shape = null;
|
||||
|
||||
String id = null;
|
||||
String type = null;
|
||||
|
@ -79,8 +77,7 @@ public class GeoShapeQueryParser implements QueryParser<GeoShapeQueryBuilder> {
|
|||
currentFieldName = parser.currentName();
|
||||
token = parser.nextToken();
|
||||
if (parseContext.parseFieldMatcher().match(currentFieldName, SHAPE_FIELD)) {
|
||||
XContentBuilder builder = XContentFactory.jsonBuilder().copyCurrentStructure(parser);
|
||||
shape = builder.bytes();
|
||||
shape = ShapeBuilder.parse(parser);
|
||||
} else if (parseContext.parseFieldMatcher().match(currentFieldName, STRATEGY_FIELD)) {
|
||||
String strategyName = parser.text();
|
||||
strategy = SpatialStrategy.fromString(strategyName);
|
||||
|
|
|
@ -22,6 +22,7 @@ package org.elasticsearch.indices;
|
|||
import org.elasticsearch.action.update.UpdateHelper;
|
||||
import org.elasticsearch.cluster.metadata.MetaDataIndexUpgradeService;
|
||||
import org.elasticsearch.common.geo.ShapesAvailability;
|
||||
import org.elasticsearch.common.geo.builders.ShapeBuilderRegistry;
|
||||
import org.elasticsearch.common.inject.AbstractModule;
|
||||
import org.elasticsearch.common.util.ExtensionPoint;
|
||||
import org.elasticsearch.index.NodeServicesProvider;
|
||||
|
@ -218,6 +219,7 @@ public class IndicesModule extends AbstractModule {
|
|||
bind(IndicesFieldDataCacheListener.class).asEagerSingleton();
|
||||
bind(TermVectorsService.class).asEagerSingleton();
|
||||
bind(NodeServicesProvider.class).asEagerSingleton();
|
||||
bind(ShapeBuilderRegistry.class).asEagerSingleton();
|
||||
}
|
||||
|
||||
// public for testing
|
||||
|
|
|
@ -52,6 +52,7 @@ public abstract class AbstractShapeBuilderTestCase<SB extends ShapeBuilder> exte
|
|||
namedWriteableRegistry.registerPrototype(ShapeBuilder.class, MultiLineStringBuilder.PROTOTYPE);
|
||||
namedWriteableRegistry.registerPrototype(ShapeBuilder.class, PolygonBuilder.PROTOTYPE);
|
||||
namedWriteableRegistry.registerPrototype(ShapeBuilder.class, MultiPolygonBuilder.PROTOTYPE);
|
||||
namedWriteableRegistry.registerPrototype(ShapeBuilder.class, GeometryCollectionBuilder.PROTOTYPE);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -27,7 +27,7 @@ import java.io.IOException;
|
|||
|
||||
public class CircleBuilderTests extends AbstractShapeBuilderTestCase<CircleBuilder> {
|
||||
|
||||
static CircleBuilderTests PROTOTYPE = new CircleBuilderTests();
|
||||
final static CircleBuilderTests PROTOTYPE = new CircleBuilderTests();
|
||||
|
||||
@Override
|
||||
protected CircleBuilder createTestShapeBuilder() {
|
||||
|
|
|
@ -37,7 +37,7 @@ public class MultiPolygonBuilderTests extends AbstractShapeBuilderTestCase<Multi
|
|||
PolygonBuilder pgb = (PolygonBuilder) RandomShapeGenerator.createShape(getRandom(), ShapeType.POLYGON);
|
||||
pgb.orientation = mpb.orientation;
|
||||
// NORELEASE translated might have been changed by createShape, but won't survive xContent->Parse roundtrip
|
||||
pgb.shell().translated = false;
|
||||
pgb.shell().translated(false);
|
||||
mpb.polygon(pgb);
|
||||
}
|
||||
return mpb;
|
||||
|
|
|
@ -26,7 +26,7 @@ import org.elasticsearch.test.geo.RandomShapeGenerator.ShapeType;
|
|||
|
||||
public class PointBuilderTests extends AbstractShapeBuilderTestCase<PointBuilder> {
|
||||
|
||||
static PointBuilderTests PROTOTYPE = new PointBuilderTests();
|
||||
final static PointBuilderTests PROTOTYPE = new PointBuilderTests();
|
||||
|
||||
@Override
|
||||
protected PointBuilder createTestShapeBuilder() {
|
||||
|
@ -35,6 +35,6 @@ public class PointBuilderTests extends AbstractShapeBuilderTestCase<PointBuilder
|
|||
|
||||
@Override
|
||||
protected PointBuilder mutate(PointBuilder original) {
|
||||
return new PointBuilder().coordinate(new Coordinate(original.longitude()/2, original.latitude()/2));
|
||||
return new PointBuilder().coordinate(new Coordinate(original.longitude() / 2, original.latitude() / 2));
|
||||
}
|
||||
}
|
||||
|
|
|
@ -36,7 +36,7 @@ public class PolygonBuilderTests extends AbstractShapeBuilderTestCase<PolygonBui
|
|||
PolygonBuilder pgb = (PolygonBuilder) RandomShapeGenerator.createShape(getRandom(), ShapeType.POLYGON);
|
||||
pgb.orientation = randomFrom(Orientation.values());
|
||||
// NORELEASE translated might have been changed by createShape, but won't survive xContent->Parse roundtrip
|
||||
pgb.shell().translated = false;
|
||||
pgb.shell().translated(false);
|
||||
return pgb;
|
||||
}
|
||||
|
||||
|
|
|
@ -47,6 +47,7 @@ import org.elasticsearch.common.bytes.BytesArray;
|
|||
import org.elasticsearch.common.bytes.BytesReference;
|
||||
import org.elasticsearch.common.collect.Tuple;
|
||||
import org.elasticsearch.common.compress.CompressedXContent;
|
||||
import org.elasticsearch.common.geo.builders.ShapeBuilderRegistry;
|
||||
import org.elasticsearch.common.inject.AbstractModule;
|
||||
import org.elasticsearch.common.inject.Injector;
|
||||
import org.elasticsearch.common.inject.ModulesBuilder;
|
||||
|
@ -191,6 +192,7 @@ public abstract class AbstractQueryTestCase<QB extends AbstractQueryBuilder<QB>>
|
|||
// skip services
|
||||
bindQueryParsersExtension();
|
||||
bindMapperExtension();
|
||||
bind(ShapeBuilderRegistry.class).asEagerSingleton();
|
||||
}
|
||||
},
|
||||
new ScriptModule(settings) {
|
||||
|
|
|
@ -29,6 +29,7 @@ import org.elasticsearch.common.bytes.BytesArray;
|
|||
import org.elasticsearch.common.geo.ShapeRelation;
|
||||
import org.elasticsearch.common.geo.SpatialStrategy;
|
||||
import org.elasticsearch.common.geo.builders.EnvelopeBuilder;
|
||||
import org.elasticsearch.common.geo.builders.PolygonBuilder;
|
||||
import org.elasticsearch.common.geo.builders.ShapeBuilder;
|
||||
import org.elasticsearch.common.geo.builders.ShapeBuilders;
|
||||
import org.elasticsearch.common.xcontent.XContentBuilder;
|
||||
|
@ -59,14 +60,17 @@ public class GeoShapeQueryBuilderTests extends AbstractQueryTestCase<GeoShapeQue
|
|||
protected GeoShapeQueryBuilder doCreateTestQueryBuilder() {
|
||||
ShapeType shapeType = ShapeType.randomType(getRandom());
|
||||
ShapeBuilder shape = RandomShapeGenerator.createShapeWithin(getRandom(), null, shapeType);
|
||||
// NORELEASE PolygonBuilder gets validated on creation, so the shells 'translate' might get set
|
||||
// this causes problems in the test, because we can't parse this property in fromXContent
|
||||
// so we switch it to false again.
|
||||
if (shape instanceof PolygonBuilder) {
|
||||
((PolygonBuilder) shape).shell().translated(false);
|
||||
}
|
||||
|
||||
GeoShapeQueryBuilder builder;
|
||||
clearShapeFields();
|
||||
if (randomBoolean()) {
|
||||
try {
|
||||
builder = new GeoShapeQueryBuilder(GEO_SHAPE_FIELD_NAME, shape);
|
||||
} catch (IOException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
builder = new GeoShapeQueryBuilder(GEO_SHAPE_FIELD_NAME, shape);
|
||||
} else {
|
||||
indexedShapeToReturn = shape;
|
||||
indexedShapeId = randomAsciiOfLengthBetween(3, 20);
|
||||
|
@ -225,17 +229,18 @@ public class GeoShapeQueryBuilderTests extends AbstractQueryTestCase<GeoShapeQue
|
|||
|
||||
public void testFromJson() throws IOException {
|
||||
String json =
|
||||
"{\n" +
|
||||
" \"geo_shape\" : {\n" +
|
||||
" \"location\" : {\n" +
|
||||
" \"shape\" : {\n" +
|
||||
" \"type\" : \"envelope\",\n" +
|
||||
" \"coordinates\" : [ [ 13.0, 53.0 ], [ 14.0, 52.0 ] ]\n" +
|
||||
" },\n" +
|
||||
" \"relation\" : \"intersects\"\n" +
|
||||
" },\n" +
|
||||
" \"boost\" : 42.0\n" +
|
||||
" }\n" +
|
||||
"{\n" +
|
||||
" \"geo_shape\" : {\n" +
|
||||
" \"location\" : {\n" +
|
||||
" \"shape\" : {\n" +
|
||||
" \"type\" : \"envelope\",\n" +
|
||||
" \"orientation\" : \"right\",\n" +
|
||||
" \"coordinates\" : [ [ 13.0, 53.0 ], [ 14.0, 52.0 ] ]\n" +
|
||||
" },\n" +
|
||||
" \"relation\" : \"intersects\"\n" +
|
||||
" },\n" +
|
||||
" \"boost\" : 42.0\n" +
|
||||
" }\n" +
|
||||
"}";
|
||||
GeoShapeQueryBuilder parsed = (GeoShapeQueryBuilder) parseQuery(json);
|
||||
checkGeneratedJson(json, parsed);
|
||||
|
|
Loading…
Reference in New Issue