Search in sources :

Example 1 with SpatialStrategy

use of org.elasticsearch.common.geo.SpatialStrategy 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 2 with SpatialStrategy

use of org.elasticsearch.common.geo.SpatialStrategy in project elasticsearch by elastic.

the class GeoShapeQueryBuilderTests method doCreateTestQueryBuilder.

@Override
protected GeoShapeQueryBuilder doCreateTestQueryBuilder() {
    ShapeType shapeType = ShapeType.randomType(random());
    ShapeBuilder shape = RandomShapeGenerator.createShapeWithin(random(), null, shapeType);
    GeoShapeQueryBuilder builder;
    clearShapeFields();
    if (randomBoolean()) {
        builder = new GeoShapeQueryBuilder(GEO_SHAPE_FIELD_NAME, shape);
    } else {
        indexedShapeToReturn = shape;
        indexedShapeId = randomAsciiOfLengthBetween(3, 20);
        indexedShapeType = randomAsciiOfLengthBetween(3, 20);
        builder = new GeoShapeQueryBuilder(GEO_SHAPE_FIELD_NAME, indexedShapeId, indexedShapeType);
        if (randomBoolean()) {
            indexedShapeIndex = randomAsciiOfLengthBetween(3, 20);
            builder.indexedShapeIndex(indexedShapeIndex);
        }
        if (randomBoolean()) {
            indexedShapePath = randomAsciiOfLengthBetween(3, 20);
            builder.indexedShapePath(indexedShapePath);
        }
    }
    if (randomBoolean()) {
        SpatialStrategy strategy = randomFrom(SpatialStrategy.values());
        // we try to avoid that combination
        while (shapeType == ShapeType.MULTILINESTRING && strategy == SpatialStrategy.TERM) {
            strategy = randomFrom(SpatialStrategy.values());
        }
        builder.strategy(strategy);
        if (strategy != SpatialStrategy.TERM) {
            builder.relation(randomFrom(ShapeRelation.values()));
        }
    }
    if (randomBoolean()) {
        builder.ignoreUnmapped(randomBoolean());
    }
    return builder;
}
Also used : ShapeBuilder(org.elasticsearch.common.geo.builders.ShapeBuilder) ShapeType(org.elasticsearch.test.geo.RandomShapeGenerator.ShapeType) SpatialStrategy(org.elasticsearch.common.geo.SpatialStrategy)

Aggregations

SpatialStrategy (org.elasticsearch.common.geo.SpatialStrategy)2 ShapeBuilder (org.elasticsearch.common.geo.builders.ShapeBuilder)2 ParsingException (org.elasticsearch.common.ParsingException)1 ShapeRelation (org.elasticsearch.common.geo.ShapeRelation)1 XContentParser (org.elasticsearch.common.xcontent.XContentParser)1 ShapeType (org.elasticsearch.test.geo.RandomShapeGenerator.ShapeType)1