-->
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.  [ 4 posts ] 
Author Message
 Post subject: Hibernate Search and Nearest Neighbor
PostPosted: Wed Sep 16, 2015 10:35 am 
Newbie

Joined: Wed Sep 16, 2015 10:30 am
Posts: 2
I'm currently investigating using Hibernate Search (spatial) for some queries. I've seen a ton of code/references to "distance from" - but one of my requirements is a "Nearest Neighbor" search. Basically, I need a "what is the closest 10 stores from my current location" type query. I've not seen this referenced from my google-foo - so I thought I post it here. Is this type of query possible with hibernate search or do I need to leverage Oracle's spatial functionality?

Thanks for any info!


Chris


Top
 Profile  
 
 Post subject: Re: Hibernate Search and Nearest Neighbor
PostPosted: Thu Sep 17, 2015 3:41 am 
Hibernate Team
Hibernate Team

Joined: Sat Jan 24, 2009 12:46 pm
Posts: 388
It's doable by setting the max results number and ordering the results by distance. E.g. like so:

Code:
final QueryBuilder builder = fullTextSession.getSearchFactory()
    .buildQueryBuilder().forEntity( Store.class ).get();

org.apache.lucene.search.Query luceneQuery = builder.spatial().onField( "location" )
    .within( 20.0, Unit.KM ).ofLatitude( centerLatitude ).andLongitude( centerLongitude )
    .createQuery();

org.hibernate.Query hibQuery = fullTextSession.createFullTextQuery( luceneQuery, Store.class )
    .setMaxResults( 10 )
    .setSort( new Sort( new DistanceSortField(centerLatitude, centerLongitude, "location") ) );

List results = hibQuery.list();


You still need to set a radius via within(). Either set it to a sufficiently large value to consider all locations or incrementally increase it in case you don't get back enough results.

Hth,

--Gunnar

_________________
Visit my blog at http://musingsofaprogrammingaddict.blogspot.com/


Top
 Profile  
 
 Post subject: Re: Hibernate Search and Nearest Neighbor
PostPosted: Thu Sep 17, 2015 2:22 pm 
Newbie

Joined: Wed Sep 16, 2015 10:30 am
Posts: 2
Thanks for the tip.

As a followup to that, any clue on performance differences doing "distance within" and your trick for "nearest neighbor" as compared to using Oracle's native geospatial functions (Locator)? Obviously this is largely dependent upon the data - but I'm wondering if there are any metrics on which implementation, in general, would perform these functions faster? The nice thing, I can use both and test the implementations side-by-side. But the advantage of using the Search is that it's database agnostic.

Cheers!

Chris


Top
 Profile  
 
 Post subject: Re: Hibernate Search and Nearest Neighbor
PostPosted: Tue Sep 22, 2015 2:56 am 
Hibernate Team
Hibernate Team

Joined: Sat Jan 24, 2009 12:46 pm
Posts: 388
Hi,

Sorry, I don't have any performance comparison at my hand. If you got something, I'd be very interested to know. Btw. you also may be interested in the "Hibernate Spatial" feature which is part of Hibernate ORM 5, I think it works with Oracle's spatial functionality, too. This used to be a separate project (hibernatespatial.org) but is now included in the ORM code base.

--Gunnar

_________________
Visit my blog at http://musingsofaprogrammingaddict.blogspot.com/


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 4 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.