Search in sources :

Example 16 with SpatialArgs

use of org.apache.lucene.spatial.query.SpatialArgs in project lucene-solr by apache.

the class AbstractSpatialFieldType method createSpatialQuery.

//--------------------------------------------------------------
// Query Support
//--------------------------------------------------------------
/**
   * Implemented for compatibility with geofilt & bbox query parsers:
   * {@link SpatialQueryable}.
   */
@Override
public Query createSpatialQuery(QParser parser, SpatialOptions options) {
    Point pt = SpatialUtils.parsePointSolrException(options.pointStr, ctx);
    double distDeg = DistanceUtils.dist2Degrees(options.distance, options.radius);
    Shape shape = ctx.makeCircle(pt, distDeg);
    if (options.bbox)
        shape = shape.getBoundingBox();
    SpatialArgs spatialArgs = new SpatialArgs(SpatialOperation.Intersects, shape);
    return getQueryFromSpatialArgs(parser, options.field, spatialArgs);
}
Also used : SpatialArgs(org.apache.lucene.spatial.query.SpatialArgs) Shape(org.locationtech.spatial4j.shape.Shape) Point(org.locationtech.spatial4j.shape.Point)

Example 17 with SpatialArgs

use of org.apache.lucene.spatial.query.SpatialArgs in project lucene-solr by apache.

the class DateRangeField method parseSpatialArgs.

@Override
protected SpatialArgs parseSpatialArgs(QParser parser, String externalVal) {
    //We avoid SpatialArgsParser entirely because it isn't very Solr-friendly
    final Shape shape = parseShape(externalVal);
    final SolrParams localParams = parser.getLocalParams();
    SpatialOperation op = SpatialOperation.Intersects;
    if (localParams != null) {
        String opStr = localParams.get(OP_PARAM);
        if (opStr != null)
            op = SpatialOperation.get(opStr);
    }
    return new SpatialArgs(op, shape);
}
Also used : SpatialArgs(org.apache.lucene.spatial.query.SpatialArgs) NRShape(org.apache.lucene.spatial.prefix.tree.NumberRangePrefixTree.NRShape) Shape(org.locationtech.spatial4j.shape.Shape) UnitNRShape(org.apache.lucene.spatial.prefix.tree.NumberRangePrefixTree.UnitNRShape) SolrParams(org.apache.solr.common.params.SolrParams) SpatialOperation(org.apache.lucene.spatial.query.SpatialOperation)

Example 18 with SpatialArgs

use of org.apache.lucene.spatial.query.SpatialArgs in project lucene-solr by apache.

the class SpatialFileQueryMaker method makeQueryFromShape.

protected Query makeQueryFromShape(Shape shape) {
    SpatialArgs args = new SpatialArgs(operation, shape);
    if (!Double.isNaN(distErrPct))
        args.setDistErrPct(distErrPct);
    Query filterQuery = strategy.makeQuery(args);
    if (score) {
        //wrap with distance computing query
        ValueSource valueSource = strategy.makeDistanceValueSource(shape.getCenter());
        return new BooleanQuery.Builder().add(new FunctionQuery(valueSource), //matches everything and provides score
        BooleanClause.Occur.MUST).add(filterQuery, //filters (score isn't used)
        BooleanClause.Occur.FILTER).build();
    } else {
        // assume constant scoring
        return filterQuery;
    }
}
Also used : BooleanQuery(org.apache.lucene.search.BooleanQuery) SpatialArgs(org.apache.lucene.spatial.query.SpatialArgs) FunctionQuery(org.apache.lucene.queries.function.FunctionQuery) Query(org.apache.lucene.search.Query) FunctionQuery(org.apache.lucene.queries.function.FunctionQuery) BooleanQuery(org.apache.lucene.search.BooleanQuery) ValueSource(org.apache.lucene.queries.function.ValueSource)

Example 19 with SpatialArgs

use of org.apache.lucene.spatial.query.SpatialArgs in project jena by apache.

the class SpatialIndexLucene method query$.

private List<Node> query$(IndexReader indexReader, Shape shape, int limit, SpatialOperation operation) throws IOException {
    if (limit <= 0)
        limit = MAX_N;
    IndexSearcher indexSearcher = new IndexSearcher(indexReader);
    SpatialArgs args = new SpatialArgs(operation, shape);
    args.setDistErr(0.0);
    Query query = strategy.makeQuery(args);
    TopDocs docs = indexSearcher.search(query, limit);
    List<Node> results = new ArrayList<>();
    for (ScoreDoc sd : docs.scoreDocs) {
        Document doc = indexSearcher.doc(sd.doc);
        String[] values = doc.getValues(docDef.getEntityField());
        for (String v : values) {
            Node n = SpatialQueryFuncs.stringToNode(v);
            results.add(n);
        }
    }
    return results;
}
Also used : SpatialArgs(org.apache.lucene.spatial.query.SpatialArgs) Node(org.apache.jena.graph.Node) ArrayList(java.util.ArrayList) Document(org.apache.lucene.document.Document)

Example 20 with SpatialArgs

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

Aggregations

SpatialArgs (org.apache.lucene.spatial.query.SpatialArgs)34 Query (org.apache.lucene.search.Query)16 Shape (org.locationtech.spatial4j.shape.Shape)14 Test (org.junit.Test)12 Point (org.locationtech.spatial4j.shape.Point)10 SpatialOperation (org.apache.lucene.spatial.query.SpatialOperation)6 Document (org.apache.lucene.document.Document)5 MatchAllDocsQuery (org.apache.lucene.search.MatchAllDocsQuery)5 BooleanQuery (org.apache.lucene.search.BooleanQuery)4 TopDocs (org.apache.lucene.search.TopDocs)4 SpatialPrefixTree (org.apache.lucene.spatial.prefix.tree.SpatialPrefixTree)3 LinkedHashSet (java.util.LinkedHashSet)2 Map (java.util.Map)2 Analyzer (org.apache.lucene.analysis.Analyzer)2 StandardAnalyzer (org.apache.lucene.analysis.standard.StandardAnalyzer)2 Field (org.apache.lucene.document.Field)2 TextField (org.apache.lucene.document.TextField)2 IndexReader (org.apache.lucene.index.IndexReader)2 Term (org.apache.lucene.index.Term)2 BooleanFilter (org.apache.lucene.queries.BooleanFilter)2