Search in sources :

Example 1 with SpatialArgsParser

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

the class TestTestFramework method testQueries.

@Test
public void testQueries() throws IOException {
    String name = StrategyTestCase.QTEST_Cities_Intersects_BBox;
    InputStream in = getClass().getClassLoader().getResourceAsStream(name);
    SpatialContext ctx = SpatialContext.GEO;
    Iterator<SpatialTestQuery> iter = SpatialTestQuery.getTestQueries(new SpatialArgsParser(), ctx, name, //closes the InputStream
    in);
    List<SpatialTestQuery> tests = new ArrayList<>();
    while (iter.hasNext()) {
        tests.add(iter.next());
    }
    Assert.assertEquals(3, tests.size());
    SpatialTestQuery sf = tests.get(0);
    // assert
    assertEquals(1, sf.ids.size());
    Assert.assertTrue(sf.ids.get(0).equals("G5391959"));
    Assert.assertTrue(sf.args.getShape() instanceof Rectangle);
    assertEquals(SpatialOperation.Intersects, sf.args.getOperation());
}
Also used : SpatialContext(org.locationtech.spatial4j.context.SpatialContext) SpatialArgsParser(org.apache.lucene.spatial.query.SpatialArgsParser) InputStream(java.io.InputStream) ArrayList(java.util.ArrayList) Rectangle(org.locationtech.spatial4j.shape.Rectangle) Test(org.junit.Test)

Example 2 with SpatialArgsParser

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

the class SpatialExample method search.

private void search() throws Exception {
    IndexReader indexReader = DirectoryReader.open(directory);
    IndexSearcher indexSearcher = new IndexSearcher(indexReader);
    Sort idSort = new Sort(new SortField("id", SortField.Type.INT));
    //--Filter by circle (<= distance from a point)
    {
        //Search with circle
        //note: SpatialArgs can be parsed from a string
        SpatialArgs args = new SpatialArgs(SpatialOperation.Intersects, ctx.makeCircle(-80.0, 33.0, DistanceUtils.dist2Degrees(200, DistanceUtils.EARTH_MEAN_RADIUS_KM)));
        Query query = strategy.makeQuery(args);
        TopDocs docs = indexSearcher.search(query, 10, idSort);
        assertDocMatchedIds(indexSearcher, docs, 2);
        //Now, lets get the distance for the 1st doc via computing from stored point value:
        // (this computation is usually not redundant)
        Document doc1 = indexSearcher.doc(docs.scoreDocs[0].doc);
        String doc1Str = doc1.getField(strategy.getFieldName()).stringValue();
        //assume doc1Str is "x y" as written in newSampleDocument()
        int spaceIdx = doc1Str.indexOf(' ');
        double x = Double.parseDouble(doc1Str.substring(0, spaceIdx));
        double y = Double.parseDouble(doc1Str.substring(spaceIdx + 1));
        double doc1DistDEG = ctx.calcDistance(args.getShape().getCenter(), x, y);
        assertEquals(121.6d, DistanceUtils.degrees2Dist(doc1DistDEG, DistanceUtils.EARTH_MEAN_RADIUS_KM), 0.1);
        //or more simply:
        assertEquals(121.6d, doc1DistDEG * DistanceUtils.DEG_TO_KM, 0.1);
    }
    //--Match all, order by distance ascending
    {
        Point pt = ctx.makePoint(60, -50);
        //the distance (in km)
        ValueSource valueSource = strategy.makeDistanceValueSource(pt, DistanceUtils.DEG_TO_KM);
        //false=asc dist
        Sort distSort = new Sort(valueSource.getSortField(false)).rewrite(indexSearcher);
        TopDocs docs = indexSearcher.search(new MatchAllDocsQuery(), 10, distSort);
        assertDocMatchedIds(indexSearcher, docs, 4, 20, 2);
    //To get the distance, we could compute from stored values like earlier.
    // However in this example we sorted on it, and the distance will get
    // computed redundantly.  If the distance is only needed for the top-X
    // search results then that's not a big deal. Alternatively, try wrapping
    // the ValueSource with CachingDoubleValueSource then retrieve the value
    // from the ValueSource now. See LUCENE-4541 for an example.
    }
    //demo arg parsing
    {
        SpatialArgs args = new SpatialArgs(SpatialOperation.Intersects, ctx.makeCircle(-80.0, 33.0, 1));
        SpatialArgs args2 = new SpatialArgsParser().parse("Intersects(BUFFER(POINT(-80 33),1))", ctx);
        assertEquals(args.toString(), args2.toString());
    }
    indexReader.close();
}
Also used : IndexSearcher(org.apache.lucene.search.IndexSearcher) SpatialArgs(org.apache.lucene.spatial.query.SpatialArgs) Query(org.apache.lucene.search.Query) MatchAllDocsQuery(org.apache.lucene.search.MatchAllDocsQuery) SpatialArgsParser(org.apache.lucene.spatial.query.SpatialArgsParser) SortField(org.apache.lucene.search.SortField) Point(org.locationtech.spatial4j.shape.Point) Document(org.apache.lucene.document.Document) MatchAllDocsQuery(org.apache.lucene.search.MatchAllDocsQuery) TopDocs(org.apache.lucene.search.TopDocs) ValueSource(org.apache.lucene.queries.function.ValueSource) IndexReader(org.apache.lucene.index.IndexReader) Sort(org.apache.lucene.search.Sort)

Example 3 with SpatialArgsParser

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

the class TestTermQueryPrefixGridStrategy method testNGramPrefixGridLosAngeles.

@Test
public void testNGramPrefixGridLosAngeles() throws IOException {
    SpatialContext ctx = SpatialContext.GEO;
    TermQueryPrefixTreeStrategy prefixGridStrategy = new TermQueryPrefixTreeStrategy(new QuadPrefixTree(ctx), "geo");
    Shape point = ctx.makePoint(-118.243680, 34.052230);
    Document losAngeles = new Document();
    losAngeles.add(new StringField("name", "Los Angeles", Field.Store.YES));
    for (Field field : prefixGridStrategy.createIndexableFields(point)) {
        losAngeles.add(field);
    }
    //just for diagnostics
    losAngeles.add(new StoredField(prefixGridStrategy.getFieldName(), point.toString()));
    addDocumentsAndCommit(Arrays.asList(losAngeles));
    // This won't work with simple spatial context...
    SpatialArgsParser spatialArgsParser = new SpatialArgsParser();
// TODO... use a non polygon query
//    SpatialArgs spatialArgs = spatialArgsParser.parse(
//        "Intersects(POLYGON((-127.00390625 39.8125,-112.765625 39.98828125,-111.53515625 31.375,-125.94921875 30.14453125,-127.00390625 39.8125)))",
//        new SimpleSpatialContext());
//    Query query = prefixGridStrategy.makeQuery(spatialArgs, fieldInfo);
//    SearchResults searchResults = executeQuery(query, 1);
//    assertEquals(1, searchResults.numFound);
}
Also used : Field(org.apache.lucene.document.Field) StringField(org.apache.lucene.document.StringField) StoredField(org.apache.lucene.document.StoredField) StoredField(org.apache.lucene.document.StoredField) SpatialContext(org.locationtech.spatial4j.context.SpatialContext) Shape(org.locationtech.spatial4j.shape.Shape) QuadPrefixTree(org.apache.lucene.spatial.prefix.tree.QuadPrefixTree) SpatialArgsParser(org.apache.lucene.spatial.query.SpatialArgsParser) StringField(org.apache.lucene.document.StringField) Document(org.apache.lucene.document.Document) Test(org.junit.Test)

Aggregations

SpatialArgsParser (org.apache.lucene.spatial.query.SpatialArgsParser)3 Document (org.apache.lucene.document.Document)2 Test (org.junit.Test)2 SpatialContext (org.locationtech.spatial4j.context.SpatialContext)2 InputStream (java.io.InputStream)1 ArrayList (java.util.ArrayList)1 Field (org.apache.lucene.document.Field)1 StoredField (org.apache.lucene.document.StoredField)1 StringField (org.apache.lucene.document.StringField)1 IndexReader (org.apache.lucene.index.IndexReader)1 ValueSource (org.apache.lucene.queries.function.ValueSource)1 IndexSearcher (org.apache.lucene.search.IndexSearcher)1 MatchAllDocsQuery (org.apache.lucene.search.MatchAllDocsQuery)1 Query (org.apache.lucene.search.Query)1 Sort (org.apache.lucene.search.Sort)1 SortField (org.apache.lucene.search.SortField)1 TopDocs (org.apache.lucene.search.TopDocs)1 QuadPrefixTree (org.apache.lucene.spatial.prefix.tree.QuadPrefixTree)1 SpatialArgs (org.apache.lucene.spatial.query.SpatialArgs)1 Point (org.locationtech.spatial4j.shape.Point)1