Skip to content

Commit

Permalink
HSEARCH-1242 Improve details on the spatial feature: default field name
Browse files Browse the repository at this point in the history
  • Loading branch information
Sanne committed Jan 11, 2013
1 parent e60d3dc commit ebbb3f3
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 6 deletions.
Expand Up @@ -106,6 +106,12 @@
* @author Sanne Grinovero
*/
public abstract class AbstractDocumentBuilder<T> {

/**
* Prefix used to generate field names for a default {@link Spatial} annotation
*/
public static final String COORDINATES_DEFAULT_FIELD = "_hibernate_default_coordinates";

private static final Log log = LoggerFactory.make();
private static final StringBridge NULL_EMBEDDED_STRING_BRIDGE = new DefaultStringBridge();
private static final String EMPTY = "";
Expand Down Expand Up @@ -983,7 +989,7 @@ private void bindSpatialAnnotation(String prefix, PropertiesMetadata propertiesM
fieldName = prefix + ann.name();
}
else {
fieldName = clazz.getName();
fieldName = COORDINATES_DEFAULT_FIELD;
}

if ( spatialNames.contains( ann.name() ) ) {
Expand Down
Expand Up @@ -20,11 +20,33 @@
*/
package org.hibernate.search.query.dsl;

import org.hibernate.search.annotations.Spatial;

/**
* @experimental This API might change in minor versions
*
* @author Emmanuel Bernard <emmanuel@hibernate.org>
*/
public interface SpatialContext extends QueryCustomization<SpatialContext> {

//TODO score by proximity

/**
* Used to create Spatial Queries on the default coordinates of
* an entity. This is the one to use when {@link Spatial} is being used
* without defining a custom value for {@link Spatial#name()}.
* @return
*/
SpatialMatchingContext onDefaultCoordinates();

/**
* An entity can have multiple {@link Spatial} annotations defining
* different sets of coordinates.
* Each non-default Spatial instance has a name to identify it,
* use this method to pick one of these non-default coordinate fields.
* @param field The name of the set of coordinates to target for the query
* @return
*/
SpatialMatchingContext onCoordinates(String field);

}
Expand Up @@ -29,6 +29,7 @@
* @author Emmanuel Bernard <emmanuel@hibernate.org>
*/
public class ConnectedSpatialContext implements SpatialContext {

private final QueryBuildingContext queryContext;
private final QueryCustomizer queryCustomizer;
private final SpatialQueryContext spatialContext;
Expand All @@ -45,7 +46,13 @@ public ConnectedSpatialContext(QueryBuildingContext context, ConnectedQueryBuild

@Override
public SpatialMatchingContext onCoordinates(String field) {
spatialContext.setCoordinatesField(field);
spatialContext.setCoordinatesField( field );
return new ConnectedSpatialMatchingContext( queryContext, queryCustomizer, spatialContext, queryBuilder );
}

@Override
public SpatialMatchingContext onDefaultCoordinates() {
spatialContext.setDefaultCoordinatesField();
return new ConnectedSpatialMatchingContext( queryContext, queryCustomizer, spatialContext, queryBuilder );
}

Expand Down
Expand Up @@ -20,6 +20,7 @@
*/
package org.hibernate.search.query.dsl.impl;

import org.hibernate.search.engine.spi.AbstractDocumentBuilder;
import org.hibernate.search.query.dsl.Unit;
import org.hibernate.search.spatial.Coordinates;

Expand All @@ -40,6 +41,10 @@ public void setCoordinatesField(String coordinatesField) {
this.coordinatesField = coordinatesField;
}

public void setDefaultCoordinatesField() {
this.coordinatesField = AbstractDocumentBuilder.COORDINATES_DEFAULT_FIELD;
}

public double getRadiusDistance() {
return radiusDistance;
}
Expand Down
Expand Up @@ -347,14 +347,14 @@ public void testSpatialAnnotationWithSubAnnotationsLevelRangeMode() throws Excep
final QueryBuilder builder = fullTextSession.getSearchFactory()
.buildQueryBuilder().forEntity( UserRange.class ).get();

org.apache.lucene.search.Query luceneQuery = builder.spatial().onCoordinates( UserRange.class.getName() )
org.apache.lucene.search.Query luceneQuery = builder.spatial().onDefaultCoordinates()
.within( 50, Unit.KM ).ofLatitude( centerLatitude ).andLongitude( centerLongitude ).createQuery();

org.hibernate.Query hibQuery = fullTextSession.createFullTextQuery( luceneQuery, UserRange.class );
List results = hibQuery.list();
Assert.assertEquals( 0, results.size() );

org.apache.lucene.search.Query luceneQuery2 = builder.spatial().onCoordinates( UserRange.class.getName() )
org.apache.lucene.search.Query luceneQuery2 = builder.spatial().onDefaultCoordinates()
.within( 51, Unit.KM ).ofLatitude( centerLatitude ).andLongitude( centerLongitude ).createQuery();

org.hibernate.Query hibQuery2 = fullTextSession.createFullTextQuery( luceneQuery2, UserRange.class );
Expand All @@ -369,7 +369,7 @@ public void testSpatialAnnotationWithSubAnnotationsLevelRangeMode() throws Excep
fullTextSession.close();
}

public void testSpatiaslAnnotation() throws Exception {
public void testSpatialsAnnotation() throws Exception {
UserEx user = new UserEx( 1, 24.0d, 32.0d, 11.9d, 27.4d );
FullTextSession fullTextSession = Search.getFullTextSession( openSession() );

Expand All @@ -382,7 +382,7 @@ public void testSpatiaslAnnotation() throws Exception {
final QueryBuilder builder = fullTextSession.getSearchFactory()
.buildQueryBuilder().forEntity( UserEx.class ).get();

org.apache.lucene.search.Query luceneQuery = builder.spatial().onCoordinates( UserEx.class.getName() )
org.apache.lucene.search.Query luceneQuery = builder.spatial().onDefaultCoordinates()
.within( 100.0d, Unit.KM ).ofLatitude( 24.0d ).andLongitude( 31.5d ).createQuery();

org.hibernate.Query hibQuery = fullTextSession.createFullTextQuery( luceneQuery, UserEx.class );
Expand Down

0 comments on commit ebbb3f3

Please sign in to comment.