Search in sources :

Example 1 with PrefixTreeStrategy

use of org.apache.lucene.spatial.prefix.PrefixTreeStrategy in project crate by crate.

the class MatchPredicate method geoMatch.

private Query geoMatch(LuceneQueryBuilder.Context context, List<Symbol> arguments, Object queryTerm) {
    Map fields = (Map) ((Literal) arguments.get(0)).value();
    String fieldName = (String) fields.keySet().iterator().next();
    MappedFieldType fieldType = context.queryShardContext().getMapperService().fullName(fieldName);
    GeoShapeFieldMapper.GeoShapeFieldType geoShapeFieldType = (GeoShapeFieldMapper.GeoShapeFieldType) fieldType;
    String matchType = (String) ((Input) arguments.get(2)).value();
    @SuppressWarnings("unchecked") Shape shape = GeoJSONUtils.map2Shape((Map<String, Object>) queryTerm);
    ShapeRelation relation = ShapeRelation.getRelationByName(matchType);
    assert relation != null : "invalid matchType: " + matchType;
    PrefixTreeStrategy prefixTreeStrategy = geoShapeFieldType.defaultStrategy();
    if (relation == ShapeRelation.DISJOINT) {
        /**
         * See {@link org.elasticsearch.index.query.GeoShapeQueryParser}:
         */
        // 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 = new TermRangeQuery(fieldName, null, null, true, true);
        Query intersects = prefixTreeStrategy.makeQuery(getArgs(shape, ShapeRelation.INTERSECTS));
        bool.add(exists, BooleanClause.Occur.MUST);
        bool.add(intersects, BooleanClause.Occur.MUST_NOT);
        return new ConstantScoreQuery(bool.build());
    }
    SpatialArgs spatialArgs = getArgs(shape, relation);
    return prefixTreeStrategy.makeQuery(spatialArgs);
}
Also used : ShapeRelation(org.elasticsearch.common.geo.ShapeRelation) BooleanQuery(org.apache.lucene.search.BooleanQuery) SpatialArgs(org.apache.lucene.spatial.query.SpatialArgs) Shape(org.locationtech.spatial4j.shape.Shape) Query(org.apache.lucene.search.Query) ConstantScoreQuery(org.apache.lucene.search.ConstantScoreQuery) FunctionToQuery(io.crate.lucene.FunctionToQuery) BooleanQuery(org.apache.lucene.search.BooleanQuery) BoostQuery(org.apache.lucene.search.BoostQuery) TermRangeQuery(org.apache.lucene.search.TermRangeQuery) LuceneQueryBuilder(io.crate.lucene.LuceneQueryBuilder) TermRangeQuery(org.apache.lucene.search.TermRangeQuery) GeoShapeFieldMapper(org.elasticsearch.index.mapper.GeoShapeFieldMapper) MappedFieldType(org.elasticsearch.index.mapper.MappedFieldType) ConstantScoreQuery(org.apache.lucene.search.ConstantScoreQuery) Map(java.util.Map) PrefixTreeStrategy(org.apache.lucene.spatial.prefix.PrefixTreeStrategy)

Example 2 with PrefixTreeStrategy

use of org.apache.lucene.spatial.prefix.PrefixTreeStrategy in project janusgraph by JanusGraph.

the class LuceneIndex method getSpatialStrategy.

private SpatialStrategy getSpatialStrategy(String key, KeyInformation ki) {
    SpatialStrategy strategy = spatial.get(key);
    final Mapping mapping = Mapping.getMapping(ki);
    final int maxLevels = ParameterType.INDEX_GEO_MAX_LEVELS.findParameter(ki.getParameters(), DEFAULT_GEO_MAX_LEVELS);
    final double distErrorPct = ParameterType.INDEX_GEO_DIST_ERROR_PCT.findParameter(ki.getParameters(), DEFAULT_GEO_DIST_ERROR_PCT);
    if (strategy == null) {
        synchronized (spatial) {
            if (!spatial.containsKey(key)) {
                // strategy = new RecursivePrefixTreeStrategy(grid, key);
                if (mapping == Mapping.DEFAULT) {
                    strategy = PointVectorStrategy.newInstance(ctx, key);
                } else {
                    final SpatialPrefixTree grid = new QuadPrefixTree(ctx, maxLevels);
                    strategy = new RecursivePrefixTreeStrategy(grid, key);
                    ((PrefixTreeStrategy) strategy).setDistErrPct(distErrorPct);
                }
                spatial.put(key, strategy);
            } else
                return spatial.get(key);
        }
    }
    return strategy;
}
Also used : QuadPrefixTree(org.apache.lucene.spatial.prefix.tree.QuadPrefixTree) RecursivePrefixTreeStrategy(org.apache.lucene.spatial.prefix.RecursivePrefixTreeStrategy) Mapping(org.janusgraph.core.schema.Mapping) SpatialPrefixTree(org.apache.lucene.spatial.prefix.tree.SpatialPrefixTree) SpatialStrategy(org.apache.lucene.spatial.SpatialStrategy) DoublePoint(org.apache.lucene.document.DoublePoint) LongPoint(org.apache.lucene.document.LongPoint) IntPoint(org.apache.lucene.document.IntPoint) PrefixTreeStrategy(org.apache.lucene.spatial.prefix.PrefixTreeStrategy) RecursivePrefixTreeStrategy(org.apache.lucene.spatial.prefix.RecursivePrefixTreeStrategy)

Example 3 with PrefixTreeStrategy

use of org.apache.lucene.spatial.prefix.PrefixTreeStrategy 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)

Example 4 with PrefixTreeStrategy

use of org.apache.lucene.spatial.prefix.PrefixTreeStrategy in project elasticsearch by elastic.

the class GeoShapeFieldMapperTests method testGeoShapeMapperMerge.

public void testGeoShapeMapperMerge() throws Exception {
    String stage1Mapping = XContentFactory.jsonBuilder().startObject().startObject("type").startObject("properties").startObject("shape").field("type", "geo_shape").field("tree", "geohash").field("strategy", "recursive").field("precision", "1m").field("tree_levels", 8).field("distance_error_pct", 0.01).field("orientation", "ccw").endObject().endObject().endObject().endObject().string();
    MapperService mapperService = createIndex("test").mapperService();
    DocumentMapper docMapper = mapperService.merge("type", new CompressedXContent(stage1Mapping), MapperService.MergeReason.MAPPING_UPDATE, false);
    String stage2Mapping = XContentFactory.jsonBuilder().startObject().startObject("type").startObject("properties").startObject("shape").field("type", "geo_shape").field("tree", "quadtree").field("strategy", "term").field("precision", "1km").field("tree_levels", 26).field("distance_error_pct", 26).field("orientation", "cw").endObject().endObject().endObject().endObject().string();
    try {
        mapperService.merge("type", new CompressedXContent(stage2Mapping), MapperService.MergeReason.MAPPING_UPDATE, false);
        fail();
    } catch (IllegalArgumentException e) {
        assertThat(e.getMessage(), containsString("mapper [shape] has different [strategy]"));
        assertThat(e.getMessage(), containsString("mapper [shape] has different [tree]"));
        assertThat(e.getMessage(), containsString("mapper [shape] has different [tree_levels]"));
        assertThat(e.getMessage(), containsString("mapper [shape] has different [precision]"));
    }
    // verify nothing changed
    FieldMapper fieldMapper = docMapper.mappers().getMapper("shape");
    assertThat(fieldMapper, instanceOf(GeoShapeFieldMapper.class));
    GeoShapeFieldMapper geoShapeFieldMapper = (GeoShapeFieldMapper) fieldMapper;
    PrefixTreeStrategy strategy = geoShapeFieldMapper.fieldType().defaultStrategy();
    assertThat(strategy, instanceOf(RecursivePrefixTreeStrategy.class));
    assertThat(strategy.getGrid(), instanceOf(GeohashPrefixTree.class));
    assertThat(strategy.getDistErrPct(), equalTo(0.01));
    assertThat(strategy.getGrid().getMaxLevels(), equalTo(GeoUtils.geoHashLevelsForPrecision(1d)));
    assertThat(geoShapeFieldMapper.fieldType().orientation(), equalTo(ShapeBuilder.Orientation.CCW));
    // correct mapping
    stage2Mapping = XContentFactory.jsonBuilder().startObject().startObject("type").startObject("properties").startObject("shape").field("type", "geo_shape").field("precision", "1m").field("tree_levels", 8).field("distance_error_pct", 0.001).field("orientation", "cw").endObject().endObject().endObject().endObject().string();
    docMapper = mapperService.merge("type", new CompressedXContent(stage2Mapping), MapperService.MergeReason.MAPPING_UPDATE, false);
    fieldMapper = docMapper.mappers().getMapper("shape");
    assertThat(fieldMapper, instanceOf(GeoShapeFieldMapper.class));
    geoShapeFieldMapper = (GeoShapeFieldMapper) fieldMapper;
    strategy = geoShapeFieldMapper.fieldType().defaultStrategy();
    assertThat(strategy, instanceOf(RecursivePrefixTreeStrategy.class));
    assertThat(strategy.getGrid(), instanceOf(GeohashPrefixTree.class));
    assertThat(strategy.getDistErrPct(), equalTo(0.001));
    assertThat(strategy.getGrid().getMaxLevels(), equalTo(GeoUtils.geoHashLevelsForPrecision(1d)));
    assertThat(geoShapeFieldMapper.fieldType().orientation(), equalTo(ShapeBuilder.Orientation.CW));
}
Also used : CompressedXContent(org.elasticsearch.common.compress.CompressedXContent) RecursivePrefixTreeStrategy(org.apache.lucene.spatial.prefix.RecursivePrefixTreeStrategy) Matchers.containsString(org.hamcrest.Matchers.containsString) PrefixTreeStrategy(org.apache.lucene.spatial.prefix.PrefixTreeStrategy) RecursivePrefixTreeStrategy(org.apache.lucene.spatial.prefix.RecursivePrefixTreeStrategy) GeohashPrefixTree(org.apache.lucene.spatial.prefix.tree.GeohashPrefixTree)

Example 5 with PrefixTreeStrategy

use of org.apache.lucene.spatial.prefix.PrefixTreeStrategy in project elasticsearch by elastic.

the class GeoShapeFieldMapperTests method testDefaultConfiguration.

public void testDefaultConfiguration() throws IOException {
    String mapping = XContentFactory.jsonBuilder().startObject().startObject("type1").startObject("properties").startObject("location").field("type", "geo_shape").endObject().endObject().endObject().endObject().string();
    DocumentMapper defaultMapper = createIndex("test").mapperService().documentMapperParser().parse("type1", new CompressedXContent(mapping));
    FieldMapper fieldMapper = defaultMapper.mappers().getMapper("location");
    assertThat(fieldMapper, instanceOf(GeoShapeFieldMapper.class));
    GeoShapeFieldMapper geoShapeFieldMapper = (GeoShapeFieldMapper) fieldMapper;
    PrefixTreeStrategy strategy = geoShapeFieldMapper.fieldType().defaultStrategy();
    assertThat(strategy.getDistErrPct(), equalTo(0.025d));
    assertThat(strategy.getGrid(), instanceOf(GeohashPrefixTree.class));
    assertThat(strategy.getGrid().getMaxLevels(), equalTo(GeoShapeFieldMapper.Defaults.GEOHASH_LEVELS));
    assertThat(geoShapeFieldMapper.fieldType().orientation(), equalTo(GeoShapeFieldMapper.Defaults.ORIENTATION));
}
Also used : CompressedXContent(org.elasticsearch.common.compress.CompressedXContent) Matchers.containsString(org.hamcrest.Matchers.containsString) PrefixTreeStrategy(org.apache.lucene.spatial.prefix.PrefixTreeStrategy) RecursivePrefixTreeStrategy(org.apache.lucene.spatial.prefix.RecursivePrefixTreeStrategy) GeohashPrefixTree(org.apache.lucene.spatial.prefix.tree.GeohashPrefixTree)

Aggregations

PrefixTreeStrategy (org.apache.lucene.spatial.prefix.PrefixTreeStrategy)9 RecursivePrefixTreeStrategy (org.apache.lucene.spatial.prefix.RecursivePrefixTreeStrategy)7 CompressedXContent (org.elasticsearch.common.compress.CompressedXContent)5 Matchers.containsString (org.hamcrest.Matchers.containsString)5 GeohashPrefixTree (org.apache.lucene.spatial.prefix.tree.GeohashPrefixTree)4 BooleanQuery (org.apache.lucene.search.BooleanQuery)2 ConstantScoreQuery (org.apache.lucene.search.ConstantScoreQuery)2 Query (org.apache.lucene.search.Query)2 QuadPrefixTree (org.apache.lucene.spatial.prefix.tree.QuadPrefixTree)2 SpatialArgs (org.apache.lucene.spatial.query.SpatialArgs)2 GeoShapeFieldMapper (org.elasticsearch.index.mapper.GeoShapeFieldMapper)2 MappedFieldType (org.elasticsearch.index.mapper.MappedFieldType)2 Shape (org.locationtech.spatial4j.shape.Shape)2 FunctionToQuery (io.crate.lucene.FunctionToQuery)1 LuceneQueryBuilder (io.crate.lucene.LuceneQueryBuilder)1 Map (java.util.Map)1 DoublePoint (org.apache.lucene.document.DoublePoint)1 IntPoint (org.apache.lucene.document.IntPoint)1 LongPoint (org.apache.lucene.document.LongPoint)1 BoostQuery (org.apache.lucene.search.BoostQuery)1