/** * Returns a Lucene query to match documents by distance to a center. * * @param center center of the search discus * @param radius distance max to center in km * @param coordinatesField name of the Lucene Field implementing Coordinates * @param approximationQuery an approximation of the distance query * (i.e. a query returning all the results returned by the distance query, * but also some false positives). * WARNING: when passing {@code null}, every single document will be scanned * (time/resource consuming!) * @return Lucene Query to be used in a search * * @see org.apache.lucene.search.Query * @see org.hibernate.search.spatial.Coordinates */ public static Query buildDistanceQuery(Query approximationQuery, Coordinates center, double radius, String coordinatesField) { return new DistanceQuery( approximationQuery, center, radius, coordinatesField ); }
private static JsonObject convertDistanceQuery(DistanceQuery query) { JsonObject distanceQuery = JsonBuilder.object() .add( "geo_distance", JsonBuilder.object() .addProperty( "distance", query.getRadius() + "km" ) .add( query.getCoordinatesField(), JsonBuilder.object() .addProperty( "lat", query.getCenter().getLatitude() ) .addProperty( "lon", query.getCenter().getLongitude() ) ) ).build(); distanceQuery = wrapQueryForNestedIfRequired( query.getCoordinatesField(), distanceQuery ); // we only implement the approximation optimization when we use the hash method as Elasticsearch // automatically optimize the geo_distance query with a bounding box filter so we don't need to do it // ourselves when we use the range method. Query approximationQuery = query.getApproximationQuery(); if ( approximationQuery instanceof SpatialHashQuery ) { distanceQuery = JsonBuilder.object() .add( "bool", JsonBuilder.object() .add( "must", distanceQuery ) .add( "filter", convertSpatialHashFilter( (SpatialHashQuery) approximationQuery ) ) ).build(); } return distanceQuery; }
@Override public boolean equals(Object obj) { if ( obj == this ) { return true; } if ( obj instanceof DistanceQuery ) { DistanceQuery other = (DistanceQuery) obj; return Float.floatToIntBits( getBoost() ) == Float.floatToIntBits( other.getBoost() ) && approximationQuery.equals( other.approximationQuery ) && center.equals( other.center ) && radius == other.radius && Objects.equals( coordinatesField, other.coordinatesField ) && Objects.equals( latitudeField, other.latitudeField ) && Objects.equals( longitudeField, other.longitudeField ); } return false; }
private static JsonObject convertDistanceQuery(DistanceQuery query) { JsonObject distanceQuery = JsonBuilder.object() .add( "geo_distance", JsonBuilder.object() .addProperty( "distance", query.getRadius() + "km" ) .add( query.getCoordinatesField(), JsonBuilder.object() .addProperty( "lat", query.getCenter().getLatitude() ) .addProperty( "lon", query.getCenter().getLongitude() ) ) ).build(); distanceQuery = wrapQueryForNestedIfRequired( query.getCoordinatesField(), distanceQuery ); // we only implement the approximation optimization when we use the hash method as Elasticsearch // automatically optimize the geo_distance query with a bounding box filter so we don't need to do it // ourselves when we use the range method. Query approximationQuery = query.getApproximationQuery(); if ( approximationQuery instanceof SpatialHashQuery ) { distanceQuery = JsonBuilder.object() .add( "bool", JsonBuilder.object() .add( "must", distanceQuery ) .add( "filter", convertSpatialHashFilter( (SpatialHashQuery) approximationQuery ) ) ).build(); } return distanceQuery; }
@Override public boolean equals(Object obj) { if ( obj == this ) { return true; } if ( obj instanceof DistanceQuery ) { DistanceQuery other = (DistanceQuery) obj; return Float.floatToIntBits( getBoost() ) == Float.floatToIntBits( other.getBoost() ) && approximationQuery.equals( other.approximationQuery ) && center.equals( other.center ) && radius == other.radius && Objects.equals( coordinatesField, other.coordinatesField ) && Objects.equals( latitudeField, other.latitudeField ) && Objects.equals( longitudeField, other.longitudeField ); } return false; }
/** * Returns a Lucene query to match documents by distance to a center. * * @param center center of the search discus * @param radius distance max to center in km * @param coordinatesField name of the Lucene Field implementing Coordinates * @param approximationQuery an approximation of the distance query * (i.e. a query returning all the results returned by the distance query, * but also some false positives). * WARNING: when passing {@code null}, every single document will be scanned * (time/resource consuming!) * @return Lucene Query to be used in a search * * @see org.apache.lucene.search.Query * @see org.hibernate.search.spatial.Coordinates */ public static Query buildDistanceQuery(Query approximationQuery, Coordinates center, double radius, String coordinatesField) { return new DistanceQuery( approximationQuery, center, radius, coordinatesField ); }
/** * Returns a Lucene query to match documents by distance to a center. * * @param center center of the search discus * @param radius distance max to center in km * @param latitudeField name of the Lucene Field hosting latitude * @param longitudeField name of the Lucene Field hosting longitude * @param approximationQuery an approximation of the distance query * (i.e. a query returning all the results returned by the distance query, * but also some false positives). * WARNING: when passing {@code null}, every single document will be scanned * (time/resource consuming!) * @return Lucene Query to be used in a search * * @see org.apache.lucene.search.Query * @see org.hibernate.search.spatial.Coordinates */ public static Query buildDistanceQuery(Query approximationQuery, Coordinates center, double radius, String latitudeField, String longitudeField) { return new DistanceQuery( approximationQuery, center, radius, latitudeField, longitudeField ); }
/** * Returns a Lucene query to match documents by distance to a center. * * @param center center of the search discus * @param radius distance max to center in km * @param latitudeField name of the Lucene Field hosting latitude * @param longitudeField name of the Lucene Field hosting longitude * @param approximationQuery an approximation of the distance query * (i.e. a query returning all the results returned by the distance query, * but also some false positives). * WARNING: when passing {@code null}, every single document will be scanned * (time/resource consuming!) * @return Lucene Query to be used in a search * * @see org.apache.lucene.search.Query * @see org.hibernate.search.spatial.Coordinates */ public static Query buildDistanceQuery(Query approximationQuery, Coordinates center, double radius, String latitudeField, String longitudeField) { return new DistanceQuery( approximationQuery, center, radius, latitudeField, longitudeField ); }
@Override public Query rewrite(IndexReader reader) throws IOException { Query superRewritten = super.rewrite( reader ); if ( superRewritten != this ) { return superRewritten; } Query rewrittenApproximationQuery = approximationQuery.rewrite( reader ); if ( rewrittenApproximationQuery != approximationQuery ) { DistanceQuery clone = new DistanceQuery( rewrittenApproximationQuery, this.center, this.radius, this.coordinatesField, this.latitudeField, this.longitudeField ); return clone; } return this; }
@Override public Query rewrite(IndexReader reader) throws IOException { Query superRewritten = super.rewrite( reader ); if ( superRewritten != this ) { return superRewritten; } Query rewrittenApproximationQuery = approximationQuery.rewrite( reader ); if ( rewrittenApproximationQuery != approximationQuery ) { DistanceQuery clone = new DistanceQuery( rewrittenApproximationQuery, this.center, this.radius, this.coordinatesField, this.latitudeField, this.longitudeField ); return clone; } return this; }