use of org.apache.lucene.spatial.query.SpatialArgs in project lucene-solr by apache.
the class TestPointVectorStrategy method testFieldOptions.
@Test
public void testFieldOptions() throws IOException, ParseException {
// It's not stored; test it isn't.
this.strategy = PointVectorStrategy.newInstance(ctx, getClass().getSimpleName());
adoc("99", "POINT(-5.0 8.2)");
commit();
SearchResults results = executeQuery(new MatchAllDocsQuery(), 1);
Document document = results.results.get(0).document;
assertNull("not stored", document.getField(strategy.getFieldName() + PointVectorStrategy.SUFFIX_X));
assertNull("not stored", document.getField(strategy.getFieldName() + PointVectorStrategy.SUFFIX_Y));
deleteAll();
// Now we mark it stored. We also disable pointvalues...
FieldType fieldType = new FieldType(PointVectorStrategy.DEFAULT_FIELDTYPE);
fieldType.setStored(true);
//disable point values
fieldType.setDimensions(0, 0);
this.strategy = new PointVectorStrategy(ctx, getClass().getSimpleName(), fieldType);
adoc("99", "POINT(-5.0 8.2)");
commit();
results = executeQuery(new MatchAllDocsQuery(), 1);
document = results.results.get(0).document;
assertEquals("stored", -5.0, document.getField(strategy.getFieldName() + PointVectorStrategy.SUFFIX_X).numericValue());
assertEquals("stored", 8.2, document.getField(strategy.getFieldName() + PointVectorStrategy.SUFFIX_Y).numericValue());
// Test a query fails without point values
expectThrows(UnsupportedOperationException.class, () -> {
SpatialArgs args = new SpatialArgs(SpatialOperation.Intersects, ctx.makeRectangle(-10.0, 10.0, -5.0, 5.0));
this.strategy.makeQuery(args);
});
}
use of org.apache.lucene.spatial.query.SpatialArgs in project lucene-solr by apache.
the class StrategyTestCase method testOperation.
protected void testOperation(Shape indexedShape, SpatialOperation operation, Shape queryShape, boolean match) throws IOException {
assertTrue("Faulty test", operation.evaluate(indexedShape, queryShape) == match || indexedShape.equals(queryShape) && (operation == SpatialOperation.Contains || operation == SpatialOperation.IsWithin));
adoc("0", indexedShape);
commit();
Query query = strategy.makeQuery(new SpatialArgs(operation, queryShape));
SearchResults got = executeQuery(query, 1);
assert got.numFound <= 1 : "unclean test env";
if ((got.numFound == 1) != match)
fail(operation + " I:" + indexedShape + " Q:" + queryShape);
//clean up after ourselves
deleteAll();
}
use of org.apache.lucene.spatial.query.SpatialArgs 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();
}
use of org.apache.lucene.spatial.query.SpatialArgs in project lucene-solr by apache.
the class PortedSolr3Test method _checkHits.
private void _checkHits(boolean bbox, Point pt, double distKM, int assertNumFound, int... assertIds) {
SpatialOperation op = SpatialOperation.Intersects;
double distDEG = DistanceUtils.dist2Degrees(distKM, DistanceUtils.EARTH_MEAN_RADIUS_KM);
Shape shape = ctx.makeCircle(pt, distDEG);
if (bbox)
shape = shape.getBoundingBox();
SpatialArgs args = new SpatialArgs(op, shape);
//args.setDistPrecision(0.025);
Query query = strategy.makeQuery(args);
SearchResults results = executeQuery(query, 100);
assertEquals("" + shape, assertNumFound, results.numFound);
if (assertIds != null) {
Set<Integer> resultIds = new HashSet<>();
for (SearchResult result : results.results) {
resultIds.add(Integer.valueOf(result.document.get("id")));
}
for (int assertId : assertIds) {
assertTrue("has " + assertId, resultIds.contains(assertId));
}
}
}
use of org.apache.lucene.spatial.query.SpatialArgs in project lucene-solr by apache.
the class CompositeSpatialStrategy method makeQuery.
@Override
public Query makeQuery(SpatialArgs args) {
final SpatialOperation pred = args.getOperation();
if (pred == SpatialOperation.BBoxIntersects || pred == SpatialOperation.BBoxWithin) {
throw new UnsupportedSpatialOperation(pred);
}
if (pred == SpatialOperation.IsDisjointTo) {
// update class docs when it's added.
throw new UnsupportedSpatialOperation(pred);
}
final ShapePredicateValueSource predicateValueSource = new ShapePredicateValueSource(geometryStrategy.makeShapeValueSource(), pred, args.getShape());
//System.out.println("PredOpt: " + optimizePredicates);
if (pred == SpatialOperation.Intersects && optimizePredicates) {
// We have a smart Intersects impl
final SpatialPrefixTree grid = indexStrategy.getGrid();
//default to max precision
final int detailLevel = grid.getLevelForDistance(args.resolveDistErr(ctx, 0.0));
return new IntersectsRPTVerifyQuery(args.getShape(), indexStrategy.getFieldName(), grid, detailLevel, indexStrategy.getPrefixGridScanLevel(), predicateValueSource);
} else {
//The general path; all index matches get verified
SpatialArgs indexArgs;
if (pred == SpatialOperation.Contains) {
// note: we could map IsWithin as well but it's pretty darned slow since it touches all world grids
indexArgs = args;
} else {
//TODO add args.clone method with new predicate? Or simply make non-final?
indexArgs = new SpatialArgs(SpatialOperation.Intersects, args.getShape());
indexArgs.setDistErr(args.getDistErr());
indexArgs.setDistErrPct(args.getDistErrPct());
}
if (indexArgs.getDistErr() == null && indexArgs.getDistErrPct() == null) {
indexArgs.setDistErrPct(0.10);
}
final Query indexQuery = indexStrategy.makeQuery(indexArgs);
return new CompositeVerifyQuery(indexQuery, predicateValueSource);
}
}
Aggregations