Search in sources :

Example 6 with ShapeBuilder

use of org.elasticsearch.common.geo.builders.ShapeBuilder in project elasticsearch by elastic.

the class GeoShapeQueryBuilderTests method testIgnoreUnmapped.

public void testIgnoreUnmapped() throws IOException {
    ShapeType shapeType = ShapeType.randomType(random());
    ShapeBuilder shape = RandomShapeGenerator.createShapeWithin(random(), null, shapeType);
    final GeoShapeQueryBuilder queryBuilder = new GeoShapeQueryBuilder("unmapped", shape);
    queryBuilder.ignoreUnmapped(true);
    Query query = queryBuilder.toQuery(createShardContext());
    assertThat(query, notNullValue());
    assertThat(query, instanceOf(MatchNoDocsQuery.class));
    final GeoShapeQueryBuilder failingQueryBuilder = new GeoShapeQueryBuilder("unmapped", shape);
    failingQueryBuilder.ignoreUnmapped(false);
    QueryShardException e = expectThrows(QueryShardException.class, () -> failingQueryBuilder.toQuery(createShardContext()));
    assertThat(e.getMessage(), containsString("failed to find geo_shape field [unmapped]"));
}
Also used : ShapeBuilder(org.elasticsearch.common.geo.builders.ShapeBuilder) Query(org.apache.lucene.search.Query) MatchNoDocsQuery(org.apache.lucene.search.MatchNoDocsQuery) ConstantScoreQuery(org.apache.lucene.search.ConstantScoreQuery) BooleanQuery(org.apache.lucene.search.BooleanQuery) MatchNoDocsQuery(org.apache.lucene.search.MatchNoDocsQuery) ShapeType(org.elasticsearch.test.geo.RandomShapeGenerator.ShapeType)

Example 7 with ShapeBuilder

use of org.elasticsearch.common.geo.builders.ShapeBuilder in project elasticsearch by elastic.

the class GeoShapeFieldMapper method parse.

@Override
public Mapper parse(ParseContext context) throws IOException {
    try {
        Shape shape = context.parseExternalValue(Shape.class);
        if (shape == null) {
            ShapeBuilder shapeBuilder = ShapeBuilder.parse(context.parser(), this);
            if (shapeBuilder == null) {
                return null;
            }
            shape = shapeBuilder.build();
        }
        if (fieldType().pointsOnly() && !(shape instanceof Point)) {
            throw new MapperParsingException("[{" + fieldType().name() + "}] is configured for points only but a " + ((shape instanceof JtsGeometry) ? ((JtsGeometry) shape).getGeom().getGeometryType() : shape.getClass()) + " was found");
        }
        Field[] fields = fieldType().defaultStrategy().createIndexableFields(shape);
        if (fields == null || fields.length == 0) {
            return null;
        }
        for (Field field : fields) {
            if (!customBoost() && fieldType.boost() != 1f && Version.indexCreated(context.indexSettings()).before(Version.V_5_0_0_alpha1)) {
                field.setBoost(fieldType().boost());
            }
            context.doc().add(field);
        }
    } catch (Exception e) {
        throw new MapperParsingException("failed to parse [" + fieldType().name() + "]", e);
    }
    return null;
}
Also used : IndexableField(org.apache.lucene.index.IndexableField) Field(org.apache.lucene.document.Field) ShapeBuilder(org.elasticsearch.common.geo.builders.ShapeBuilder) Shape(org.locationtech.spatial4j.shape.Shape) JtsGeometry(org.locationtech.spatial4j.shape.jts.JtsGeometry) Point(org.locationtech.spatial4j.shape.Point) QueryShardException(org.elasticsearch.index.query.QueryShardException) IOException(java.io.IOException)

Example 8 with ShapeBuilder

use of org.elasticsearch.common.geo.builders.ShapeBuilder in project elasticsearch by elastic.

the class GeoShapeQueryBuilder method doRewrite.

@Override
protected QueryBuilder doRewrite(QueryRewriteContext queryShardContext) throws IOException {
    if (this.shape == null) {
        GetRequest getRequest = new GetRequest(indexedShapeIndex, indexedShapeType, indexedShapeId);
        ShapeBuilder shape = fetch(queryShardContext.getClient(), getRequest, indexedShapePath);
        return new GeoShapeQueryBuilder(this.fieldName, shape).relation(relation).strategy(strategy);
    }
    return this;
}
Also used : ShapeBuilder(org.elasticsearch.common.geo.builders.ShapeBuilder) GetRequest(org.elasticsearch.action.get.GetRequest)

Example 9 with ShapeBuilder

use of org.elasticsearch.common.geo.builders.ShapeBuilder in project elasticsearch by elastic.

the class GeoShapeQueryBuilder method fromXContent.

public static GeoShapeQueryBuilder fromXContent(QueryParseContext parseContext) throws IOException {
    XContentParser parser = parseContext.parser();
    String fieldName = null;
    ShapeRelation shapeRelation = null;
    SpatialStrategy strategy = null;
    ShapeBuilder shape = null;
    String id = null;
    String type = null;
    String index = null;
    String shapePath = null;
    XContentParser.Token token;
    String currentFieldName = null;
    float boost = AbstractQueryBuilder.DEFAULT_BOOST;
    String queryName = null;
    boolean ignoreUnmapped = DEFAULT_IGNORE_UNMAPPED;
    while ((token = parser.nextToken()) != XContentParser.Token.END_OBJECT) {
        if (token == XContentParser.Token.FIELD_NAME) {
            currentFieldName = parser.currentName();
        } else if (token == XContentParser.Token.START_OBJECT) {
            if (fieldName != null) {
                throw new ParsingException(parser.getTokenLocation(), "[" + GeoShapeQueryBuilder.NAME + "] point specified twice. [" + currentFieldName + "]");
            }
            fieldName = currentFieldName;
            while ((token = parser.nextToken()) != XContentParser.Token.END_OBJECT) {
                if (token == XContentParser.Token.FIELD_NAME) {
                    currentFieldName = parser.currentName();
                    token = parser.nextToken();
                    if (SHAPE_FIELD.match(currentFieldName)) {
                        shape = ShapeBuilder.parse(parser);
                    } else if (STRATEGY_FIELD.match(currentFieldName)) {
                        String strategyName = parser.text();
                        strategy = SpatialStrategy.fromString(strategyName);
                        if (strategy == null) {
                            throw new ParsingException(parser.getTokenLocation(), "Unknown strategy [" + strategyName + " ]");
                        }
                    } else if (RELATION_FIELD.match(currentFieldName)) {
                        shapeRelation = ShapeRelation.getRelationByName(parser.text());
                        if (shapeRelation == null) {
                            throw new ParsingException(parser.getTokenLocation(), "Unknown shape operation [" + parser.text() + " ]");
                        }
                    } else if (INDEXED_SHAPE_FIELD.match(currentFieldName)) {
                        while ((token = parser.nextToken()) != XContentParser.Token.END_OBJECT) {
                            if (token == XContentParser.Token.FIELD_NAME) {
                                currentFieldName = parser.currentName();
                            } else if (token.isValue()) {
                                if (SHAPE_ID_FIELD.match(currentFieldName)) {
                                    id = parser.text();
                                } else if (SHAPE_TYPE_FIELD.match(currentFieldName)) {
                                    type = parser.text();
                                } else if (SHAPE_INDEX_FIELD.match(currentFieldName)) {
                                    index = parser.text();
                                } else if (SHAPE_PATH_FIELD.match(currentFieldName)) {
                                    shapePath = parser.text();
                                }
                            } else {
                                throw new ParsingException(parser.getTokenLocation(), "[" + GeoShapeQueryBuilder.NAME + "] unknown token [" + token + "] after [" + currentFieldName + "]");
                            }
                        }
                    } else {
                        throw new ParsingException(parser.getTokenLocation(), "[" + GeoShapeQueryBuilder.NAME + "] query does not support [" + currentFieldName + "]");
                    }
                }
            }
        } else if (token.isValue()) {
            if (AbstractQueryBuilder.BOOST_FIELD.match(currentFieldName)) {
                boost = parser.floatValue();
            } else if (AbstractQueryBuilder.NAME_FIELD.match(currentFieldName)) {
                queryName = parser.text();
            } else if (IGNORE_UNMAPPED_FIELD.match(currentFieldName)) {
                ignoreUnmapped = parser.booleanValue();
            } else {
                throw new ParsingException(parser.getTokenLocation(), "[" + GeoShapeQueryBuilder.NAME + "] query does not support [" + currentFieldName + "]");
            }
        }
    }
    GeoShapeQueryBuilder builder;
    if (shape != null) {
        builder = new GeoShapeQueryBuilder(fieldName, shape);
    } else {
        builder = new GeoShapeQueryBuilder(fieldName, id, type);
    }
    if (index != null) {
        builder.indexedShapeIndex(index);
    }
    if (shapePath != null) {
        builder.indexedShapePath(shapePath);
    }
    if (shapeRelation != null) {
        builder.relation(shapeRelation);
    }
    if (strategy != null) {
        builder.strategy(strategy);
    }
    if (queryName != null) {
        builder.queryName(queryName);
    }
    builder.boost(boost);
    builder.ignoreUnmapped(ignoreUnmapped);
    return builder;
}
Also used : ShapeRelation(org.elasticsearch.common.geo.ShapeRelation) ShapeBuilder(org.elasticsearch.common.geo.builders.ShapeBuilder) ParsingException(org.elasticsearch.common.ParsingException) SpatialStrategy(org.elasticsearch.common.geo.SpatialStrategy) XContentParser(org.elasticsearch.common.xcontent.XContentParser)

Example 10 with ShapeBuilder

use of org.elasticsearch.common.geo.builders.ShapeBuilder in project elasticsearch by elastic.

the class GeoShapeQueryBuilder method doToQuery.

@Override
protected Query doToQuery(QueryShardContext context) {
    if (shape == null) {
        throw new UnsupportedOperationException("query must be rewritten first");
    }
    final ShapeBuilder shapeToQuery = shape;
    final MappedFieldType fieldType = context.fieldMapper(fieldName);
    if (fieldType == null) {
        if (ignoreUnmapped) {
            return new MatchNoDocsQuery();
        } else {
            throw new QueryShardException(context, "failed to find geo_shape field [" + fieldName + "]");
        }
    }
    // TODO: This isn't the nicest way to check this
    if (!(fieldType instanceof GeoShapeFieldMapper.GeoShapeFieldType)) {
        throw new QueryShardException(context, "Field [" + fieldName + "] is not a geo_shape");
    }
    final GeoShapeFieldMapper.GeoShapeFieldType shapeFieldType = (GeoShapeFieldMapper.GeoShapeFieldType) fieldType;
    PrefixTreeStrategy strategy = shapeFieldType.defaultStrategy();
    if (this.strategy != null) {
        strategy = shapeFieldType.resolveStrategy(this.strategy);
    }
    Query query;
    if (strategy instanceof RecursivePrefixTreeStrategy && relation == ShapeRelation.DISJOINT) {
        // this strategy doesn't support disjoint anymore: but it did
        // before, including creating lucene fieldcache (!)
        // 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(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(shapeToQuery, relation)));
    }
    return query;
}
Also used : BooleanQuery(org.apache.lucene.search.BooleanQuery) ShapeBuilder(org.elasticsearch.common.geo.builders.ShapeBuilder) Query(org.apache.lucene.search.Query) MatchNoDocsQuery(org.apache.lucene.search.MatchNoDocsQuery) ConstantScoreQuery(org.apache.lucene.search.ConstantScoreQuery) BooleanQuery(org.apache.lucene.search.BooleanQuery) MatchNoDocsQuery(org.apache.lucene.search.MatchNoDocsQuery) XContentBuilder(org.elasticsearch.common.xcontent.XContentBuilder) ShapeBuilder(org.elasticsearch.common.geo.builders.ShapeBuilder) GeoShapeFieldMapper(org.elasticsearch.index.mapper.GeoShapeFieldMapper) MappedFieldType(org.elasticsearch.index.mapper.MappedFieldType) RecursivePrefixTreeStrategy(org.apache.lucene.spatial.prefix.RecursivePrefixTreeStrategy) ConstantScoreQuery(org.apache.lucene.search.ConstantScoreQuery) PrefixTreeStrategy(org.apache.lucene.spatial.prefix.PrefixTreeStrategy) RecursivePrefixTreeStrategy(org.apache.lucene.spatial.prefix.RecursivePrefixTreeStrategy)

Aggregations

ShapeBuilder (org.elasticsearch.common.geo.builders.ShapeBuilder)20 Coordinate (com.vividsolutions.jts.geom.Coordinate)6 SearchResponse (org.elasticsearch.action.search.SearchResponse)6 ElasticsearchAssertions.assertSearchResponse (org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertSearchResponse)6 XContentBuilder (org.elasticsearch.common.xcontent.XContentBuilder)4 Matchers.containsString (org.hamcrest.Matchers.containsString)4 GeometryCollectionBuilder (org.elasticsearch.common.geo.builders.GeometryCollectionBuilder)3 BooleanQuery (org.apache.lucene.search.BooleanQuery)2 ConstantScoreQuery (org.apache.lucene.search.ConstantScoreQuery)2 MatchNoDocsQuery (org.apache.lucene.search.MatchNoDocsQuery)2 Query (org.apache.lucene.search.Query)2 SpatialStrategy (org.elasticsearch.common.geo.SpatialStrategy)2 GeoShapeQueryBuilder (org.elasticsearch.index.query.GeoShapeQueryBuilder)2 ShapeType (org.elasticsearch.test.geo.RandomShapeGenerator.ShapeType)2 Point (org.locationtech.spatial4j.shape.Point)2 JtsGeometry (org.locationtech.spatial4j.shape.jts.JtsGeometry)2 IOException (java.io.IOException)1 ArrayList (java.util.ArrayList)1 Field (org.apache.lucene.document.Field)1 IndexableField (org.apache.lucene.index.IndexableField)1