Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Geo: move indexShape to AbstractGeometryFieldMapper.Indexer #44979

Merged
merged 4 commits into from
Jul 30, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@

import java.io.IOException;
import java.text.ParseException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
Expand Down Expand Up @@ -78,6 +79,7 @@ public interface Indexer<Parsed, Processed> {

Class<Processed> processedClass();

List<IndexableField> indexShape(ParseContext context, Processed shape);
}

/**
Expand Down Expand Up @@ -410,8 +412,6 @@ public Orientation orientation() {
return ((AbstractGeometryFieldType)fieldType).orientation();
}

protected abstract void indexShape(ParseContext context, Processed shape);

/** parsing logic for geometry indexing */
@Override
public void parse(ParseContext context) throws IOException {
Expand All @@ -428,7 +428,13 @@ public void parse(ParseContext context) throws IOException {
}
shape = geometryIndexer.prepareForIndexing(geometry);
}
indexShape(context, shape);

List<IndexableField> fields = new ArrayList<>();
fields.addAll(geometryIndexer.indexShape(context, shape));
createFieldNamesField(context, fields);
for (IndexableField field : fields) {
context.doc().add(field);
}
} catch (Exception e) {
if (ignoreMalformed.value() == false) {
throw new MapperParsingException("failed to parse field [{}] of type [{}]", e, fieldType().name(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,29 +18,14 @@
*/
package org.elasticsearch.index.mapper;

import org.apache.lucene.document.Field;
import org.apache.lucene.document.LatLonShape;
import org.apache.lucene.geo.Line;
import org.apache.lucene.geo.Polygon;
import org.apache.lucene.index.IndexableField;
import org.elasticsearch.common.Explicit;
import org.elasticsearch.common.geo.GeometryParser;
import org.elasticsearch.common.geo.builders.ShapeBuilder;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.geo.geometry.Circle;
import org.elasticsearch.geo.geometry.Geometry;
import org.elasticsearch.geo.geometry.GeometryCollection;
import org.elasticsearch.geo.geometry.GeometryVisitor;
import org.elasticsearch.geo.geometry.LinearRing;
import org.elasticsearch.geo.geometry.MultiLine;
import org.elasticsearch.geo.geometry.MultiPoint;
import org.elasticsearch.geo.geometry.MultiPolygon;
import org.elasticsearch.geo.geometry.Point;
import org.elasticsearch.index.query.VectorGeoShapeQueryProcessor;

import java.util.ArrayList;
import java.util.Arrays;

/**
* FieldMapper for indexing {@link LatLonShape}s.
* <p>
Expand Down Expand Up @@ -80,12 +65,14 @@ public GeoShapeFieldMapper build(BuilderContext context) {
protected void setupFieldType(BuilderContext context) {
super.setupFieldType(context);

GeometryParser geometryParser = new GeometryParser(orientation == ShapeBuilder.Orientation.RIGHT, coerce(context).value(),
ignoreZValue().value());
GeoShapeFieldType fieldType = (GeoShapeFieldType)fieldType();
boolean orientation = fieldType.orientation == ShapeBuilder.Orientation.RIGHT;

GeometryParser geometryParser = new GeometryParser(orientation, coerce(context).value(), ignoreZValue().value());

((GeoShapeFieldType)fieldType()).setGeometryIndexer(new GeoShapeIndexer(orientation == ShapeBuilder.Orientation.RIGHT));
((GeoShapeFieldType)fieldType()).setGeometryParser( (parser, mapper) -> geometryParser.parse(parser));
((GeoShapeFieldType)fieldType()).setGeometryQueryBuilder(new VectorGeoShapeQueryProcessor());
fieldType.setGeometryIndexer(new GeoShapeIndexer(orientation, fieldType.name()));
fieldType.setGeometryParser( (parser, mapper) -> geometryParser.parse(parser));
fieldType.setGeometryQueryBuilder(new VectorGeoShapeQueryProcessor());
}
}

Expand All @@ -107,11 +94,6 @@ public GeoShapeFieldType clone() {
public String typeName() {
return CONTENT_TYPE;
}

@Override
protected Indexer<Geometry, Geometry> geometryIndexer() {
return new GeoShapeIndexer(orientation == ShapeBuilder.Orientation.RIGHT);
}
}

public GeoShapeFieldMapper(String simpleName, MappedFieldType fieldType, MappedFieldType defaultFieldType,
Expand All @@ -127,103 +109,6 @@ public GeoShapeFieldType fieldType() {
return (GeoShapeFieldType) super.fieldType();
}

@Override
protected void indexShape(ParseContext context, Geometry luceneShape) {
luceneShape.visit(new LuceneGeometryIndexer(context));
}

private class LuceneGeometryIndexer implements GeometryVisitor<Void, RuntimeException> {
private ParseContext context;

private LuceneGeometryIndexer(ParseContext context) {
this.context = context;
}

@Override
public Void visit(Circle circle) {
throw new IllegalArgumentException("invalid shape type found [Circle] while indexing shape");
}

@Override
public Void visit(GeometryCollection<?> collection) {
for (Geometry geometry : collection) {
geometry.visit(this);
}
return null;
}

@Override
public Void visit(org.elasticsearch.geo.geometry.Line line) {
indexFields(context, LatLonShape.createIndexableFields(name(), new Line(line.getLats(), line.getLons())));
return null;
}

@Override
public Void visit(LinearRing ring) {
throw new IllegalArgumentException("invalid shape type found [LinearRing] while indexing shape");
}

@Override
public Void visit(MultiLine multiLine) {
for (org.elasticsearch.geo.geometry.Line line : multiLine) {
visit(line);
}
return null;
}

@Override
public Void visit(MultiPoint multiPoint) {
for(Point point : multiPoint) {
visit(point);
}
return null;
}

@Override
public Void visit(MultiPolygon multiPolygon) {
for(org.elasticsearch.geo.geometry.Polygon polygon : multiPolygon) {
visit(polygon);
}
return null;
}

@Override
public Void visit(Point point) {
indexFields(context, LatLonShape.createIndexableFields(name(), point.getLat(), point.getLon()));
return null;
}

@Override
public Void visit(org.elasticsearch.geo.geometry.Polygon polygon) {
indexFields(context, LatLonShape.createIndexableFields(name(), toLucenePolygon(polygon)));
return null;
}

@Override
public Void visit(org.elasticsearch.geo.geometry.Rectangle r) {
Polygon p = new Polygon(new double[]{r.getMinLat(), r.getMinLat(), r.getMaxLat(), r.getMaxLat(), r.getMinLat()},
new double[]{r.getMinLon(), r.getMaxLon(), r.getMaxLon(), r.getMinLon(), r.getMinLon()});
indexFields(context, LatLonShape.createIndexableFields(name(), p));
return null;
}
}

public static Polygon toLucenePolygon(org.elasticsearch.geo.geometry.Polygon polygon) {
Polygon[] holes = new Polygon[polygon.getNumberOfHoles()];
for(int i = 0; i<holes.length; i++) {
holes[i] = new Polygon(polygon.getHole(i).getLats(), polygon.getHole(i).getLons());
}
return new Polygon(polygon.getPolygon().getLats(), polygon.getPolygon().getLons(), holes);
}

private void indexFields(ParseContext context, Field[] fields) {
ArrayList<IndexableField> flist = new ArrayList<>(Arrays.asList(fields));
createFieldNamesField(context, flist);
for (IndexableField f : flist) {
context.doc().add(f);
}
}

@Override
protected String contentType() {
return CONTENT_TYPE;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@

package org.elasticsearch.index.mapper;

import org.apache.lucene.document.LatLonShape;
import org.apache.lucene.index.IndexableField;
import org.elasticsearch.common.collect.Tuple;
import org.elasticsearch.geo.geometry.Circle;
import org.elasticsearch.geo.geometry.Geometry;
Expand Down Expand Up @@ -60,8 +62,11 @@ public final class GeoShapeIndexer implements AbstractGeometryFieldMapper.Indexe

private final boolean orientation;

public GeoShapeIndexer(boolean orientation) {
private final String name;

public GeoShapeIndexer(boolean orientation, String name) {
this.orientation = orientation;
this.name = name;
}

public Geometry prepareForIndexing(Geometry geometry) {
Expand Down Expand Up @@ -181,6 +186,13 @@ public Class<Geometry> processedClass() {
return Geometry.class;
}

@Override
public List<IndexableField> indexShape(ParseContext context, Geometry shape) {
LuceneGeometryIndexer visitor = new LuceneGeometryIndexer(name);
shape.visit(visitor);
return visitor.fields();
}

/**
* Calculate the intersection of a line segment and a vertical dateline.
*
Expand Down Expand Up @@ -936,4 +948,99 @@ private static Point[][] holes(Edge[] holes, int numHoles) {
return points;
}


private static class LuceneGeometryIndexer implements GeometryVisitor<Void, RuntimeException> {
private List<IndexableField> fields = new ArrayList<>();
private String name;

private LuceneGeometryIndexer(String name) {
this.name = name;
}

List<IndexableField> fields() {
return fields;
}

@Override
public Void visit(Circle circle) {
throw new IllegalArgumentException("invalid shape type found [Circle] while indexing shape");
}

@Override
public Void visit(GeometryCollection<?> collection) {
for (Geometry geometry : collection) {
geometry.visit(this);
}
return null;
}

@Override
public Void visit(org.elasticsearch.geo.geometry.Line line) {
addFields(LatLonShape.createIndexableFields(name, new org.apache.lucene.geo.Line(line.getLats(), line.getLons())));
return null;
}

@Override
public Void visit(LinearRing ring) {
throw new IllegalArgumentException("invalid shape type found [LinearRing] while indexing shape");
}

@Override
public Void visit(MultiLine multiLine) {
for (org.elasticsearch.geo.geometry.Line line : multiLine) {
visit(line);
}
return null;
}

@Override
public Void visit(MultiPoint multiPoint) {
for(Point point : multiPoint) {
visit(point);
}
return null;
}

@Override
public Void visit(MultiPolygon multiPolygon) {
for(org.elasticsearch.geo.geometry.Polygon polygon : multiPolygon) {
visit(polygon);
}
return null;
}

@Override
public Void visit(Point point) {
addFields(LatLonShape.createIndexableFields(name, point.getLat(), point.getLon()));
return null;
}

@Override
public Void visit(org.elasticsearch.geo.geometry.Polygon polygon) {
addFields(LatLonShape.createIndexableFields(name, toLucenePolygon(polygon)));
return null;
}

@Override
public Void visit(org.elasticsearch.geo.geometry.Rectangle r) {
org.apache.lucene.geo.Polygon p = new org.apache.lucene.geo.Polygon(
new double[]{r.getMinLat(), r.getMinLat(), r.getMaxLat(), r.getMaxLat(), r.getMinLat()},
new double[]{r.getMinLon(), r.getMaxLon(), r.getMaxLon(), r.getMinLon(), r.getMinLon()});
addFields(LatLonShape.createIndexableFields(name, p));
return null;
}

private void addFields(IndexableField[] fields) {
this.fields.addAll(Arrays.asList(fields));
}
}


public static org.apache.lucene.geo.Polygon toLucenePolygon(org.elasticsearch.geo.geometry.Polygon polygon) {
org.apache.lucene.geo.Polygon[] holes = new org.apache.lucene.geo.Polygon[polygon.getNumberOfHoles()];
for(int i = 0; i<holes.length; i++) {
holes[i] = new org.apache.lucene.geo.Polygon(polygon.getHole(i).getLats(), polygon.getHole(i).getLons());
}
return new org.apache.lucene.geo.Polygon(polygon.getPolygon().getLats(), polygon.getPolygon().getLons(), holes);
}
}
Loading