Java Code Examples for org.apache.lucene.spatial.query.SpatialArgs#getShape()

The following examples show how to use org.apache.lucene.spatial.query.SpatialArgs#getShape() . 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: RecursivePrefixTreeStrategy.java    From lucene-solr with Apache License 2.0 6 votes vote down vote up
@Override
public Query makeQuery(SpatialArgs args) {
  final SpatialOperation op = args.getOperation();

  Shape shape = args.getShape();
  int detailLevel = grid.getLevelForDistance(args.resolveDistErr(ctx, distErrPct));

  if (op == SpatialOperation.Intersects) {
    if (isGridAlignedShape(args.getShape())) {
      return makeGridShapeIntersectsQuery(args.getShape());
    }
    return new IntersectsPrefixTreeQuery(
        shape, getFieldName(), grid, detailLevel, prefixGridScanLevel);
  } else if (op == SpatialOperation.IsWithin) {
    return new WithinPrefixTreeQuery(
        shape, getFieldName(), grid, detailLevel, prefixGridScanLevel,
        -1);//-1 flag is slower but ensures correct results
  } else if (op == SpatialOperation.Contains) {
    return new ContainsPrefixTreeQuery(shape, getFieldName(), grid, detailLevel,
        multiOverlappingIndexedShapes);
  }
  throw new UnsupportedSpatialOperation(op);
}
 
Example 2
Source File: BBoxStrategy.java    From lucene-solr with Apache License 2.0 6 votes vote down vote up
@Override
public Query makeQuery(SpatialArgs args) {
  Shape shape = args.getShape();
  if (!(shape instanceof Rectangle))
    throw new UnsupportedOperationException("Can only query by Rectangle, not " + shape);

  Rectangle bbox = (Rectangle) shape;
  Query spatial;

  // Useful for understanding Relations:
  // http://edndoc.esri.com/arcsde/9.1/general_topics/understand_spatial_relations.htm
  SpatialOperation op = args.getOperation();
       if( op == SpatialOperation.BBoxIntersects ) spatial = makeIntersects(bbox);
  else if( op == SpatialOperation.BBoxWithin     ) spatial = makeWithin(bbox);
  else if( op == SpatialOperation.Contains       ) spatial = makeContains(bbox);
  else if( op == SpatialOperation.Intersects     ) spatial = makeIntersects(bbox);
  else if( op == SpatialOperation.IsEqualTo      ) spatial = makeEquals(bbox);
  else if( op == SpatialOperation.IsDisjointTo   ) spatial = makeDisjoint(bbox);
  else if( op == SpatialOperation.IsWithin       ) spatial = makeWithin(bbox);
  else { //no Overlaps support yet
      throw new UnsupportedSpatialOperation(op);
  }
  return new ConstantScoreQuery(spatial);
}
 
Example 3
Source File: BBoxStrategy.java    From lucene-solr with Apache License 2.0 6 votes vote down vote up
@Override
public Query makeQuery(SpatialArgs args) {
  Shape shape = args.getShape();
  if (!(shape instanceof Rectangle))
    throw new UnsupportedOperationException("Can only query by Rectangle, not " + shape);

  Rectangle bbox = (Rectangle) shape;
  Query spatial;

  // Useful for understanding Relations:
  // http://edndoc.esri.com/arcsde/9.1/general_topics/understand_spatial_relations.htm
  SpatialOperation op = args.getOperation();
       if( op == SpatialOperation.BBoxIntersects ) spatial = makeIntersects(bbox);
  else if( op == SpatialOperation.BBoxWithin     ) spatial = makeWithin(bbox);
  else if( op == SpatialOperation.Contains       ) spatial = makeContains(bbox);
  else if( op == SpatialOperation.Intersects     ) spatial = makeIntersects(bbox);
  else if( op == SpatialOperation.IsEqualTo      ) spatial = makeEquals(bbox);
  else if( op == SpatialOperation.IsDisjointTo   ) spatial = makeDisjoint(bbox);
  else if( op == SpatialOperation.IsWithin       ) spatial = makeWithin(bbox);
  else { //no Overlaps support yet
      throw new UnsupportedSpatialOperation(op);
  }
  return new ConstantScoreQuery(spatial);
}
 
Example 4
Source File: LatLonPointSpatialField.java    From lucene-solr with Apache License 2.0 6 votes vote down vote up
@Override
public Query makeQuery(SpatialArgs args) {
  if (args.getOperation() != SpatialOperation.Intersects) {
    throw new UnsupportedSpatialOperation(args.getOperation());
  }
  Shape shape = args.getShape();
  if (indexed && docValues) {
    return new IndexOrDocValuesQuery(makeQueryFromIndex(shape), makeQueryFromDocValues(shape));
  } else if (indexed) {
    return makeQueryFromIndex(shape);
  } else if (docValues) {
    return makeQueryFromDocValues(shape);
  } else {
    throw new SolrException(SolrException.ErrorCode.BAD_REQUEST,
        getFieldName() + " needs indexed (preferred) or docValues to support search");
  }
}
 
Example 5
Source File: TermQueryPrefixTreeStrategy.java    From incubator-retired-blur with Apache License 2.0 6 votes vote down vote up
@Override
public Filter makeFilter(SpatialArgs args) {
  final SpatialOperation op = args.getOperation();
  if (op != SpatialOperation.Intersects)
    throw new UnsupportedSpatialOperation(op);

  Shape shape = args.getShape();
  int detailLevel = grid.getLevelForDistance(args.resolveDistErr(ctx, distErrPct));
  List<Cell> cells = grid.getCells(shape, detailLevel, false,// no parents
      true);// simplify
  BytesRef[] terms = new BytesRef[cells.size()];
  int i = 0;
  for (Cell cell : cells) {
    terms[i++] = new BytesRef(cell.getTokenString());
  }
  return new TermsFilter(getFieldName(), terms);
}
 
Example 6
Source File: SerializedDVStrategy.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
/**
 * Returns a Query that should be used in a random-access fashion.
 * Use in another manner will be SLOW.
 */
@Override
public Query makeQuery(SpatialArgs args) {
  ShapeValuesSource shapeValueSource = makeShapeValueSource();
  ShapeValuesPredicate predicateValueSource = new ShapeValuesPredicate(shapeValueSource, args.getOperation(), args.getShape());
  return new PredicateValueSourceQuery(predicateValueSource);
}
 
Example 7
Source File: BBoxField.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
@Override
protected DoubleValuesSource getValueSourceFromSpatialArgs(QParser parser, SchemaField field, SpatialArgs spatialArgs, String scoreParam, BBoxStrategy strategy) {
  if (scoreParam == null) {
    return null;
  }

  switch (scoreParam) {
    //TODO move these to superclass after LUCENE-5804 ?
    case OVERLAP_RATIO:
      double queryTargetProportion = 0.25;//Suggested default; weights towards target area

      String v = parser.getParam(PARAM_QUERY_TARGET_PROPORTION);
      if (v != null)
        queryTargetProportion = Double.parseDouble(v);

      double minSideLength = 0.0;
      v = parser.getParam(PARAM_MIN_SIDE_LENGTH);
      if (v != null)
        minSideLength = Double.parseDouble(v);

      return new BBoxOverlapRatioValueSource(
          strategy.makeShapeValueSource(), ctx.isGeo(),
          (Rectangle) spatialArgs.getShape(),
          queryTargetProportion, minSideLength);

    case AREA:
      return new ShapeAreaValueSource(strategy.makeShapeValueSource(), ctx, ctx.isGeo(),
          distanceUnits.multiplierFromDegreesToThisUnit() * distanceUnits.multiplierFromDegreesToThisUnit());

    case AREA2D:
      return new ShapeAreaValueSource(strategy.makeShapeValueSource(), ctx, false,
          distanceUnits.multiplierFromDegreesToThisUnit() * distanceUnits.multiplierFromDegreesToThisUnit());

    default:
      return super.getValueSourceFromSpatialArgs(parser, field, spatialArgs, scoreParam, strategy);
  }
}
 
Example 8
Source File: RecursivePrefixTreeStrategy.java    From incubator-retired-blur with Apache License 2.0 5 votes vote down vote up
@Override
public Filter makeFilter(SpatialArgs args) {
  final SpatialOperation op = args.getOperation();
  if (op == SpatialOperation.IsDisjointTo)
    return new DisjointSpatialFilter(this, args, getFieldName());

  Shape shape = args.getShape();
  int detailLevel = grid.getLevelForDistance(args.resolveDistErr(ctx, distErrPct));
  final boolean hasIndexedLeaves = true;

  if (op == SpatialOperation.Intersects) {
    return new IntersectsPrefixTreeFilter(shape, getFieldName(), grid, detailLevel, prefixGridScanLevel,
        hasIndexedLeaves);
  } else if (op == SpatialOperation.IsWithin) {
    return new WithinPrefixTreeFilter(shape, getFieldName(), grid, detailLevel, prefixGridScanLevel, -1);// -1
                                                                                                         // flag
                                                                                                         // is
                                                                                                         // slower
                                                                                                         // but
                                                                                                         // ensures
                                                                                                         // correct
                                                                                                         // results
  } else if (op == SpatialOperation.Contains) {
    return new ContainsPrefixTreeFilter(shape, getFieldName(), grid, detailLevel);
  }
  throw new UnsupportedSpatialOperation(op);
}
 
Example 9
Source File: TermQueryPrefixTreeStrategy.java    From lucene-solr with Apache License 2.0 4 votes vote down vote up
@Override
public Query makeQuery(SpatialArgs args) {
  final SpatialOperation op = args.getOperation();
  if (op != SpatialOperation.Intersects)
    throw new UnsupportedSpatialOperation(op);

  Shape shape = args.getShape();
  int detailLevel = grid.getLevelForDistance(args.resolveDistErr(ctx, distErrPct));

  //--get a List of BytesRef for each term we want (no parents, no leaf bytes))
  final int GUESS_NUM_TERMS;
  if (shape instanceof Point)
    GUESS_NUM_TERMS = detailLevel;//perfect guess
  else
    GUESS_NUM_TERMS = 4096;//should this be a method on SpatialPrefixTree?

  BytesRefBuilder masterBytes = new BytesRefBuilder();//shared byte array for all terms
  List<BytesRef> terms = new ArrayList<>(GUESS_NUM_TERMS);

  CellIterator cells = grid.getTreeCellIterator(shape, detailLevel);
  while (cells.hasNext()) {
    Cell cell = cells.next();
    if (!cell.isLeaf())
      continue;
    BytesRef term = cell.getTokenBytesNoLeaf(null);//null because we want a new BytesRef
    //We copy out the bytes because it may be re-used across the iteration. This also gives us the opportunity
    // to use one contiguous block of memory for the bytes of all terms we need.
    masterBytes.grow(masterBytes.length() + term.length);
    masterBytes.append(term);
    term.bytes = null;//don't need; will reset later
    term.offset = masterBytes.length() - term.length;
    terms.add(term);
  }
  //doing this now because if we did earlier, it's possible the bytes needed to grow()
  for (BytesRef byteRef : terms) {
    byteRef.bytes = masterBytes.bytes();
  }
  //unfortunately TermsQuery will needlessly sort & dedupe
  //TODO an automatonQuery might be faster?
  return new TermInSetQuery(getFieldName(), terms);
}