DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports Events Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones AWS Cloud
by AWS Developer Relations
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones
AWS Cloud
by AWS Developer Relations
  1. DZone
  2. Coding
  3. Java
  4. Spatial Search with Hibernate+Lucene

Spatial Search with Hibernate+Lucene

samyem tuladhar user avatar by
samyem tuladhar
·
Mar. 04, 09 · Interview
Like (0)
Save
Tweet
Share
25.28K Views

Join the DZone community and get the full member experience.

Join For Free

Lucene is a powerful full-text indexing and query library. It powers many popular web sites like Sourceforge and Wikipedia. With its popularity, Lucene has seen a variety of contributions from developers to add functionality like web crawlers with Solr and machine learning with Mahout.

One of the interesting developments is the addition of the Lucene-spatial module which provides power to perform queries on geo-coded data - for instance, find all coffee shops within 5 miles of your office. In this article, I will attempt to show some of the key points that will enable a typical Hibernate based application to add this piece of functionality.

Let's say we have a Hibernate model as follows:

@Entity
@Table(name = "coffee_shop")
@Indexed
public class CoffeeShop{
private int id;
private String name;
private String address;
private double lat;
private double lng;

//get/set accessors ...

}

This class is using the Hibernate Search's @Indexed annotation to indicate that this class is indexed by Lucene. Each coffee shop has  latitude and longitude information. If you know the address, it is generally fairly easy to derive latitude/longitude information with either online geo-coding services from the likes of Google Maps or offline processing with tools such as JGeocoder or its Perl equivalent.

The key to encoding the geo-information with Lucene-spatial is to index the geohash and the cartesian tier (Explained at http://wiki.apache.org/lucene-java/SpatialSearch). To do so with Hibernate search, you need to create a field-bridge, as follows, in your hibernate model class. 

	@Transient 
@Field
@FieldBridge(impl=CartesianTierFieldBridgeImpl.class)
public LatLng getLatLng(){
if(latitude==null || longitude==null){
return null;
}
LatLng latLng=new FloatLatLng(latitude,longitude);
return latLng;
}

The field bridge allows you to inject behavior into Hibernate Search to define how the fields should be indexed as text. The field bridge is implemented as:

import java.util.LinkedList;
import java.util.List;

import org.apache.lucene.document.Document;
import org.apache.lucene.document.Field;
import org.apache.lucene.spatial.geometry.LatLng;
import org.apache.lucene.spatial.tier.projections.CartesianTierPlotter;
import org.apache.lucene.spatial.tier.projections.IProjector;
import org.apache.lucene.spatial.tier.projections.SinusoidalProjector;
import org.apache.solr.util.NumberUtils;
import org.hibernate.search.bridge.FieldBridge;
import org.hibernate.search.bridge.LuceneOptions;

/**
* Cartesian Tier Plotter to work with hibernate search
*
*
*/
public class CartesianTierFieldBridgeImpl implements FieldBridge {
private static List<CartesianTierPlotter> ctps = new LinkedList<CartesianTierPlotter>();
private static IProjector project = new SinusoidalProjector();
static {
setUpPlotter(2, 15);
}

private static void setUpPlotter(int base, int top) {

for (; base <= top; base++) {
ctps.add(new CartesianTierPlotter(base, project, CartesianTierPlotter.DEFALT_FIELD_PREFIX));
}
}

@Override
public void set(String name, Object value, Document document, LuceneOptions luceneOptions) {
LatLng latLng = (LatLng) value;
if(latLng==null) return;
for (int i = 0; i < ctps.size(); i++) {
CartesianTierPlotter ctp = ctps.get(i);
document.add(new Field(ctp.getTierFieldName(),
NumberUtils.double2sortableStr(ctp.getTierBoxId(latLng.getLat(), latLng.getLng())), Field.Store.YES,
Field.Index.NOT_ANALYZED_NO_NORMS));
}
}

}

This field bridge allows the latitude/longitude information to be stored with the cartesian tier plotter supplied with Lucene-spatial package.

The other key piece is to map the geohash in your Hibernate model:

	@Transient
@Field(index = Index.UN_TOKENIZED, store = Store.YES, name = "geohash")
public String getGeohash() {
if (latitude == null || longitude == null)
return null;
return GeoHashUtils.encode(latitude, longitude);
}

Once your Hibernate model is equipped to index the cartesian tier IDs and geohash, you can perform spatial queries, like:

DistanceQueryBuilder builder = new DistanceQueryBuilder(lat, lng, radiusMiles, 
"geoHash", CartesianTierPlotter.DEFALT_FIELD_PREFIX, true);
searcher.search(builder.getQuery());

 which will find all instances within radiusMiles of latitude and longitude passed to it.

You can download the latest Lucene-spatial modules directly from Lucene's CI site at http://hudson.zones.apache.org/hudson/job/Lucene-trunk/lastSuccessfulBuild/artifact/maven_artifacts/lucene/lucene-spatial/2.4-SNAPSHOT/

Enjoy your spatial searching!


Contact me at samyem[at]gmail.com if you have any comments on this article.
Hibernate

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • Journey to Event Driven, Part 1: Why Event-First Programming Changes Everything
  • Fixing Bottlenecks in Your Microservices App Flows
  • A Beginner's Guide to Infrastructure as Code
  • Keep Your Application Secrets Secret

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends: