简体   繁体   中英

Spatial query using Hibernate Search always returns an empty result set

I'm having issues with a spatial search using hibernate search (5.0.1) onto a mySQL database (5.6).

The problem really is nothing is being returned from the query, I believe I have followed the documentation accurately and made the appropriate changes for JPA.

The longitude and latitude data is correctly in the DB as type DOUBLE and the query parameters should return a large number of objects. I am completely at a loss as to what the problem is, possibly I have missed a simple step somewhere

Here is the querying code...

public List<Dealership>  getAllDealershipsAroundApplicant(  double centerLatitude, double centerLongitude) {

    FullTextEntityManager fullTextEntityManager = Search.getFullTextEntityManager(em)  ;  

    QueryBuilder builder = fullTextEntityManager.getSearchFactory()
      .buildQueryBuilder().forEntity( Dealership.class ).get();

    org.apache.lucene.search.Query luceneQuery = builder.spatial()
              .onDefaultCoordinates()
              .within( 10000, Unit.KM )
              .ofLatitude( 51d )  // HARD CODE
              .andLongitude( 0 )
              .createQuery();

    FullTextQuery hibQuery = fullTextEntityManager.createFullTextQuery(luceneQuery, Dealership.class);

    List<Dealership>  results = (List<Dealership> )hibQuery.getResultList();

    System.out.println( "results: " + results ) ;

    return results;     
}

And the indexed object... (table per class inheritance with ID in the super class)

@Spatial(spatialMode = SpatialMode.RANGE)
@Indexed
@Entity
@Audited(targetAuditMode=RelationTargetAuditMode.AUDITED)
@Table(name = "car_dealership")
@Cache(usage = CacheConcurrencyStrategy.READ_WRITE)
public class Dealership extends CarDBEntity {   


    @Latitude
    @Column(name = "latitude")
    private Double latitude ;

    @Longitude
    @Column(name = "longitude")
    private Double longitude ;


    public Double getLatitude() {
        return latitude;
    }

    public void setLatitude(Double latitude) {
        this.latitude = latitude;
    }

    public Double getLongitude() {
        return longitude;
    }

    public void setLongitude(Double longitude) {
        this.longitude = longitude;
    }
            ...

Other than write these sections of code I have made no other changes to the DB or added in any extra configuration to the hibernate/spring(4.1.1) application.

Any pointers would be gratefully appreciated. Thanks

When introducing Hibernate Search in an existing application - or just with an existing database - you need to rebuild the Lucene index.

Rebuilding the index implies that Hibernate needs to load all of the indexed entities (and potentially their indexed relations) from the database, so that could take a while.

The most efficient way to do that is to use the included utility in Hibernate Search, called the MassIndexer :

fullTextSession.createIndexer().startAndWait();

This method will use efficient forward-only scrolling methods and use a bit of concurrent operations to use all your CPUs for indexing. It's worth reading the MassIndexer reference documentation to find out about its tuning options.

You might also need to rebuild the index if you change the indexed attributes, or the indexing options.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM