-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 5 posts ] 
Author Message
 Post subject: Geo-location search
PostPosted: Sun Aug 01, 2010 4:58 am 
Newbie

Joined: Fri May 14, 2010 8:33 am
Posts: 8
Hi,

I'd like to be able to search entities and sort them by distance to a given coordinate.

I'm using hibernate search as the search engine, which works great.
What would be the best way to add geo-local search capabilities?

Best regards,
Fred


Top
 Profile  
 
 Post subject: Re: Geo-location search
PostPosted: Mon Aug 02, 2010 3:45 am 
Hibernate Team
Hibernate Team

Joined: Fri Oct 05, 2007 4:47 pm
Posts: 2536
Location: Third rock from the Sun
Hi,
that highly depends on your requirements.
In fact the approaches can be so different that Hibernate Search isn't providing specific helpers yet, but neither makes it harder for you as the Lucene API is not hidden. The main problem for integration is currently that Lucene didn't merge all needed patches yet: https://issues.apache.org/jira/browse/LUCENE-2152

The common approach is to encode location information with the document, in the format most suited for your problem (look into FieldBridge or ClassBridge for this), and then use a two step filter, one to remove all documents which are in other areas, one to really calculate the distances.
see http://blog.jteam.nl/2009/08/03/geo-location-search-with-solr-and-lucene/

Let me know if you need some changes in Hibernate Search's core to use the multi-threaded filtering described, feedback is highly appreciated so that we can design it at best in next version - assuming Lucene will merge this soon. Of course contributions are always welcome.

_________________
Sanne
http://in.relation.to/


Top
 Profile  
 
 Post subject: Re: Geo-location search
PostPosted: Mon Aug 02, 2010 11:00 am 
Newbie

Joined: Fri May 14, 2010 8:33 am
Posts: 8
Hi Sanne,

Thank you for your reply, again :)

I will get started with this info right away. Thanks!!


Top
 Profile  
 
 Post subject: Re: Geo-location search
PostPosted: Mon Aug 16, 2010 4:20 am 
Newbie

Joined: Fri May 14, 2010 8:33 am
Posts: 8
Hi, i've got this to work. Maybe this info can be useful to others so I post it here...

I had to implement a 'FieldBridge' that transforms the coordinates to a sortable String.
In the org.apache.solr.util.NumberUtils class there is a method that does this.

The implementation of the FieldBridge is below...

Code:
import org.apache.solr.util.NumberUtils;
import org.hibernate.search.bridge.StringBridge;

/**
*
* @author Fred
*/
public class GeoCoordinateFieldBridge implements StringBridge {

    public String objectToString(Object object) {
        if(object != null && object instanceof Double)
        {
            Double coordinate = (Double)object;
            return NumberUtils.double2sortableStr(coordinate);
        }else
        {
            return "";
        }

    }
}


You then have to apply this 'bridge' to the latitude and longitude fields:

Code:
@Column
    @Field(index=Index.UN_TOKENIZED, store=Store.YES)
    @FieldBridge(impl = GeoCoordinateFieldBridge.class)
    private Double latitude;


To index the entities nothing changes.

To launch the search, I used the code that can be found in the README file of the locallucene distribution.

Best regards,
Fred


Top
 Profile  
 
 Post subject: Re: Geo-location search
PostPosted: Thu Aug 19, 2010 5:01 pm 
Hibernate Team
Hibernate Team

Joined: Fri Oct 05, 2007 4:47 pm
Posts: 2536
Location: Third rock from the Sun
nice and simple, thanks for posting it.
maybe if the check "instanceof Double" you might want to throw and exception to make sure people don't place it on the wrong place, or you might prefer to log a warning.

_________________
Sanne
http://in.relation.to/


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 5 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.