Search in sources :

Example 31 with SpatialArgs

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

the class AbstractSpatialFieldType method getRangeQuery.

@Override
public Query getRangeQuery(QParser parser, SchemaField field, String part1, String part2, boolean minInclusive, boolean maxInclusive) {
    if (!minInclusive || !maxInclusive)
        throw new SolrException(SolrException.ErrorCode.BAD_REQUEST, "Both sides of spatial range query must be inclusive: " + field.getName());
    Point p1 = SpatialUtils.parsePointSolrException(part1, ctx);
    Point p2 = SpatialUtils.parsePointSolrException(part2, ctx);
    Rectangle bbox = ctx.makeRectangle(p1, p2);
    SpatialArgs spatialArgs = new SpatialArgs(SpatialOperation.Intersects, bbox);
    //won't score by default
    return getQueryFromSpatialArgs(parser, field, spatialArgs);
}
Also used : SpatialArgs(org.apache.lucene.spatial.query.SpatialArgs) Rectangle(org.locationtech.spatial4j.shape.Rectangle) Point(org.locationtech.spatial4j.shape.Point) SolrException(org.apache.solr.common.SolrException)

Example 32 with SpatialArgs

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

the class DateRangeField method getRangeQuery.

@Override
public Query getRangeQuery(QParser parser, SchemaField field, String startStr, String endStr, boolean minInclusive, boolean maxInclusive) {
    if (parser == null) {
        //null when invoked by SimpleFacets.  But getQueryFromSpatialArgs expects to get localParams.
        final SolrRequestInfo requestInfo = SolrRequestInfo.getRequestInfo();
        parser = new QParser("", null, requestInfo.getReq().getParams(), requestInfo.getReq()) {

            @Override
            public Query parse() throws SyntaxError {
                throw new IllegalStateException();
            }
        };
    }
    Calendar startCal;
    if (startStr == null) {
        startCal = tree.newCal();
    } else {
        startCal = parseCalendar(startStr);
        if (!minInclusive) {
            startCal.add(Calendar.MILLISECOND, 1);
        }
    }
    Calendar endCal;
    if (endStr == null) {
        endCal = tree.newCal();
    } else {
        endCal = parseCalendar(endStr);
        if (!maxInclusive) {
            endCal.add(Calendar.MILLISECOND, -1);
        }
    }
    Shape shape = tree.toRangeShape(tree.toShape(startCal), tree.toShape(endCal));
    SpatialArgs spatialArgs = new SpatialArgs(SpatialOperation.Intersects, shape);
    return getQueryFromSpatialArgs(parser, field, spatialArgs);
}
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) Query(org.apache.lucene.search.Query) SyntaxError(org.apache.solr.search.SyntaxError) Calendar(java.util.Calendar) QParser(org.apache.solr.search.QParser) SolrRequestInfo(org.apache.solr.request.SolrRequestInfo)

Example 33 with SpatialArgs

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

the class GeoNamesQueryLuceneIndex method createSpatialQuery.

private Query createSpatialQuery(Point shapeCenter, int radiusInKm) {
    final SpatialPrefixTree grid = new GeohashPrefixTree(SPATIAL_CONTEXT, GeoNamesLuceneConstants.GEOHASH_LEVELS);
    final SpatialStrategy strategy = new RecursivePrefixTreeStrategy(grid, GeoNamesLuceneConstants.GEO_FIELD);
    // Create a spatial filter that will select the documents that are in the specified
    // search radius around the metacard's center.
    final double searchRadiusDegrees = radiusInKm * DistanceUtils.KM_TO_DEG;
    final SpatialArgs args = new SpatialArgs(SpatialOperation.Intersects, SPATIAL_CONTEXT.getShapeFactory().circle(shapeCenter, searchRadiusDegrees));
    return strategy.makeQuery(args);
}
Also used : SpatialArgs(org.apache.lucene.spatial.query.SpatialArgs) RecursivePrefixTreeStrategy(org.apache.lucene.spatial.prefix.RecursivePrefixTreeStrategy) SpatialPrefixTree(org.apache.lucene.spatial.prefix.tree.SpatialPrefixTree) SpatialStrategy(org.apache.lucene.spatial.SpatialStrategy) GeohashPrefixTree(org.apache.lucene.spatial.prefix.tree.GeohashPrefixTree)

Example 34 with SpatialArgs

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

the class LuceneExample method example1.

@Test
public void example1() throws Exception {
    Directory dir = FSDirectory.open(path.toPath());
    Analyzer analyzer = new StandardAnalyzer();
    IndexWriterConfig iwc = new IndexWriterConfig(analyzer);
    iwc.setOpenMode(IndexWriterConfig.OpenMode.CREATE_OR_APPEND);
    IndexWriter writer = new IndexWriter(dir, iwc);
    indexDocs(writer, "doc1", ImmutableMap.of("name", "The laborious work of John Doe as we know it", "city", "Blumenkamp", "location", Geoshape.point(51.687882, 6.612053), "time", 1000342034));
    indexDocs(writer, "doc2", ImmutableMap.of("name", "Life as we know it or not", "city", "Essen", "location", Geoshape.point(51.787882, 6.712053), "time", 1000342034 - 500));
    indexDocs(writer, "doc3", ImmutableMap.of("name", "Berlin - poor but sexy and a display of the extraordinary", "city", "Berlin", "location", Geoshape.circle(52.509535, 13.425293, 50), "time", 1000342034 + 2000));
    writer.close();
    // Search
    IndexReader reader = DirectoryReader.open(FSDirectory.open(path.toPath()));
    IndexSearcher searcher = new IndexSearcher(reader);
    // Auesee
    BooleanQuery.Builder filter = new BooleanQuery.Builder();
    SpatialArgs args = new SpatialArgs(SpatialOperation.Intersects, Geoshape.circle(51.666167, 6.58905, 450).getShape());
    filter.add(LongPoint.newRangeQuery("time", 1000342034, 1000342034), BooleanClause.Occur.MUST);
    filter.add(new PrefixQuery(new Term("city_str", "B")), BooleanClause.Occur.MUST);
    BooleanQuery.Builder qb = new BooleanQuery.Builder();
    qb.add(new MatchAllDocsQuery(), BooleanClause.Occur.SHOULD);
    qb.add(filter.build(), BooleanClause.Occur.FILTER);
    TopDocs docs = searcher.search(qb.build(), MAX_RESULT);
    if (docs.totalHits.value >= MAX_RESULT)
        throw new RuntimeException("Max results exceeded: " + MAX_RESULT);
    Set<String> result = getResults(searcher, docs);
    System.out.println(result);
}
Also used : IndexSearcher(org.apache.lucene.search.IndexSearcher) BooleanQuery(org.apache.lucene.search.BooleanQuery) SpatialArgs(org.apache.lucene.spatial.query.SpatialArgs) Term(org.apache.lucene.index.Term) Analyzer(org.apache.lucene.analysis.Analyzer) StandardAnalyzer(org.apache.lucene.analysis.standard.StandardAnalyzer) MatchAllDocsQuery(org.apache.lucene.search.MatchAllDocsQuery) TopDocs(org.apache.lucene.search.TopDocs) IndexWriter(org.apache.lucene.index.IndexWriter) PrefixQuery(org.apache.lucene.search.PrefixQuery) StandardAnalyzer(org.apache.lucene.analysis.standard.StandardAnalyzer) IndexReader(org.apache.lucene.index.IndexReader) Directory(org.apache.lucene.store.Directory) FSDirectory(org.apache.lucene.store.FSDirectory) IndexWriterConfig(org.apache.lucene.index.IndexWriterConfig) Test(org.junit.jupiter.api.Test)

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