org.apache.lucene.spatial.prefix.tree.QuadPrefixTree Java Examples

The following examples show how to use org.apache.lucene.spatial.prefix.tree.QuadPrefixTree. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: RandomSpatialOpFuzzyPrefixTreeTest.java    From lucene-solr with Apache License 2.0 6 votes vote down vote up
private void setupQuadGrid(int maxLevels, boolean packedQuadPrefixTree) {
  //non-geospatial makes this test a little easier (in gridSnap), and using boundary values 2^X raises
  // the prospect of edge conditions we want to test, plus makes for simpler numbers (no decimals).
  SpatialContextFactory factory = new SpatialContextFactory();
  factory.geo = false;
  factory.worldBounds = new RectangleImpl(0, 256, -128, 128, null);
  this.ctx = factory.newSpatialContext();
  //A fairly shallow grid, and default 2.5% distErrPct
  if (maxLevels == -1)
    maxLevels = randomIntBetween(1, 8);//max 64k cells (4^8), also 256*256
  if (packedQuadPrefixTree) {
    this.grid = new PackedQuadPrefixTree(ctx, maxLevels);
  } else {
    this.grid = new QuadPrefixTree(ctx, maxLevels);
  }
  this.strategy = newRPT();
}
 
Example #2
Source File: QueryEqualsHashCodeTest.java    From lucene-solr with Apache License 2.0 6 votes vote down vote up
@Test
public void testEqualsHashCode() {

  switch (random().nextInt(4)) {//0-3
    case 0: predicate = SpatialOperation.Contains; break;
    case 1: predicate = SpatialOperation.IsWithin; break;

    default: predicate = SpatialOperation.Intersects; break;
  }
  final SpatialPrefixTree gridQuad = new QuadPrefixTree(ctx,10);
  final SpatialPrefixTree gridGeohash = new GeohashPrefixTree(ctx,10);

  Collection<SpatialStrategy> strategies = new ArrayList<>();
  RecursivePrefixTreeStrategy recursive_geohash = new RecursivePrefixTreeStrategy(gridGeohash, "recursive_geohash");
  strategies.add(recursive_geohash);
  strategies.add(new TermQueryPrefixTreeStrategy(gridQuad, "termquery_quad"));
  strategies.add(PointVectorStrategy.newInstance(ctx, "pointvector"));
  strategies.add(BBoxStrategy.newInstance(ctx, "bbox"));
  final SerializedDVStrategy serialized = new SerializedDVStrategy(ctx, "serialized");
  strategies.add(serialized);
  strategies.add(new CompositeSpatialStrategy("composite", recursive_geohash, serialized));
  for (SpatialStrategy strategy : strategies) {
    testEqualsHashcode(strategy);
  }
}
 
Example #3
Source File: HeatmapFacetCounterTest.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
@Before
public void setUp() throws Exception {
  super.setUp();
  cellsValidated = cellValidatedNonZero = 0;
  ctx = SpatialContext.GEO;
  shapeFactory = ctx.getShapeFactory();
  grid = new QuadPrefixTree(ctx, randomIntBetween(1, 8));
  strategy = new RecursivePrefixTreeStrategy(grid, getTestClass().getSimpleName());
  if (rarely()) {
    ((PrefixTreeStrategy) strategy).setPointsOnly(true);
  }
}
 
Example #4
Source File: HeatmapFacetCounterTest.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
@Test
public void testLucene7291Dateline() throws IOException {
  grid = new QuadPrefixTree(ctx, 2); // only 2, and we wind up with some big leaf cells
  strategy = new RecursivePrefixTreeStrategy(grid, getTestClass().getSimpleName());
  adoc("0", shapeFactory.rect(-102, -83, 43, 52));
  commit();
  validateHeatmapResultLoop(shapeFactory.rect(179, -179, 62, 63), 2, 100);// HM crosses dateline
}
 
Example #5
Source File: HeatmapFacetCounterTest.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
@Test
public void testQueryCircle() throws IOException {
  //overwrite setUp; non-geo bounds is more straight-forward; otherwise 88,88 would actually be practically north,
  final SpatialContextFactory spatialContextFactory = new SpatialContextFactory();
  spatialContextFactory.geo = false;
  spatialContextFactory.worldBounds = new RectangleImpl(-90, 90, -90, 90, null);
  ctx = spatialContextFactory.newSpatialContext();
  shapeFactory = ctx.getShapeFactory();
  final int LEVEL = 4;
  grid = new QuadPrefixTree(ctx, LEVEL);
  strategy = new RecursivePrefixTreeStrategy(grid, getTestClass().getSimpleName());
  Circle circle = shapeFactory.circle(0, 0, 89);
  adoc("0", shapeFactory.pointXY(88, 88));//top-right, inside bbox of circle but not the circle
  adoc("1", shapeFactory.pointXY(0, 0));//clearly inside; dead center in fact
  commit();
  final HeatmapFacetCounter.Heatmap heatmap = HeatmapFacetCounter.calcFacets(
      (PrefixTreeStrategy) strategy, indexSearcher.getTopReaderContext(), null,
      circle, LEVEL, 1000);
  //assert that only one point is found, not 2
  boolean foundOne = false;
  for (int count : heatmap.counts) {
    switch (count) {
      case 0: break;
      case 1:
        assertFalse(foundOne);//this is the first
        foundOne = true;
        break;
      default:
        fail("counts should be 0 or 1: " + count);
    }
  }
  assertTrue(foundOne);
}
 
Example #6
Source File: JtsPolygonTest.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
/**
 * A PrefixTree pruning optimization gone bad.
 * See <a href="https://issues.apache.org/jira/browse/LUCENE-4770">LUCENE-4770</a>.
 */
@Test
public void testBadPrefixTreePrune() throws Exception {

  Shape area = ctx.readShapeFromWkt("POLYGON((-122.83 48.57, -122.77 48.56, -122.79 48.53, -122.83 48.57))");

  SpatialPrefixTree trie = new QuadPrefixTree(ctx, 12);
  TermQueryPrefixTreeStrategy strategy = new TermQueryPrefixTreeStrategy(trie, "geo");
  Document doc = new Document();
  doc.add(new TextField("id", "1", Store.YES));

  Field[] fields = strategy.createIndexableFields(area, 0.025);
  for (Field field : fields) {
    doc.add(field);
  }
  addDocument(doc);

  Point upperleft = ctx.getShapeFactory().pointXY(-122.88, 48.54);
  Point lowerright = ctx.getShapeFactory().pointXY(-122.82, 48.62);

  Query query = strategy.makeQuery(new SpatialArgs(SpatialOperation.Intersects, ctx.getShapeFactory().rect(upperleft, lowerright)));
  commit();

  TopDocs search = indexSearcher.search(query, 10);
  ScoreDoc[] scoreDocs = search.scoreDocs;
  for (ScoreDoc scoreDoc : scoreDocs) {
    System.out.println(indexSearcher.doc(scoreDoc.doc));
  }

  assertEquals(1, search.totalHits.value);
}
 
Example #7
Source File: TestTermQueryPrefixGridStrategy.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
@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);
    }
    losAngeles.add(new StoredField(prefixGridStrategy.getFieldName(), point.toString()));//just for diagnostics

    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);
  }
 
Example #8
Source File: Geo3dRptTest.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
private void setupGrid() {
  int type = random().nextInt(4);
  if (type == 0) {
    this.grid = new GeohashPrefixTree(ctx, 2);
  } else if (type == 1) {
    this.grid = new QuadPrefixTree(ctx, 5);
  } else {
    int arity = random().nextInt(3) + 1;
    this.grid = new S2PrefixTree(ctx, 5 - arity, arity);
  }
  this.rptStrategy = newRPT();
  this.rptStrategy.setPruneLeafyBranches(random().nextBoolean());
}
 
Example #9
Source File: CompositeStrategyTest.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
private void setupQuadGrid(int maxLevels) {
  //non-geospatial makes this test a little easier (in gridSnap), and using boundary values 2^X raises
  // the prospect of edge conditions we want to test, plus makes for simpler numbers (no decimals).
  SpatialContextFactory factory = new SpatialContextFactory();
  factory.geo = false;
  factory.worldBounds = new RectangleImpl(0, 256, -128, 128, null);
  this.ctx = factory.newSpatialContext();
  //A fairly shallow grid
  if (maxLevels == -1)
    maxLevels = randomIntBetween(1, 8);//max 64k cells (4^8), also 256*256
  this.grid = new QuadPrefixTree(ctx, maxLevels);
  this.rptStrategy = newRPT();
}