NCache makes Lucene distributed and scalable by implementing the Lucene API on top of NCache’s Distributed architecture. Starting from NCache, NCache has support for another Lucene feature: GeoSpatial Indexes. Let’s see how to use GeoSpatial Indexes with Distributed Lucene.
Full-Text Searching with Distributed Lucene is divided into two phases: indexing and searching. In the indexing phase, an analyzer creates indexes from some text. Then, the searching phase only uses those indexes.
Since we’re working with GeoSpatial Indexes, we want to index longitude and latitude coordinates in our documents and then search data based on those stored locations.
NCache Details NCache Documentation Edition Comparison
How to use GeoSpatial Indexes with Distributed Lucene
To index documents with geo-spatial coordinates and perform location-based searching, Distributed Lucene uses Spatial4n, “a GeoSpatial Library for .NET.”
Before starting our sample application, we should have NCache installed, and a Distributed Lucene cache already created. To learn how to configure a Distributed Lucene cache, check Create a Distributed Lucene with Persistence Cache.
1. Index Some Landmarks
Let’s use GeoSpatial Indexes to store our favorite landmarks from our past travels.
First, let’s create a Console application and install the NuGet package Lucene.Net.Spatial.NCache
.
In the, Program.cs
file, let’s index some landmarks around Paris. Each landmark has a name, longitude, and latitude. Something like this,
If you’re familiar with Full-Text Indexing with Distributed Lucene, then creating GeoSpatial Indexes is also quite similar. We have to open an NCache directory, create a writer, and add documents to the writer.
But, instead of using analyzers to index text, we need a SpatialStrategy
. A strategy turns points and shapes into indexable fields.
To index our landmarks, we used RecursivePrefixTreeStrategy
. This strategy supports searching for non-point shapes. To create it, we used a Geohash-based tree and a field name. We will use the same field name later to create documents.
Distributed Lucene introduces a new type of document: SpatialDocument
. It’s a Lucene document with some shapes attached. Like, a point, rectangle, or circle.
This is the ToSpatialDocument()
extension method we used to convert our landmarks into SpatialDocument
,
Notice that we stored the landmark name in a string field and a string representation of the landmark location in another field named after the SpatialStrategy
field name. Then, we created a SpatialDocument
with a regular Lucene document and a point for the landmark location.
To create points, we used the SpatialContext.GEO
factory instead of using constructors directly.
2. Search Closest Landmarks
Now that we have indexed our favorite landmarks, let’s find five of them, 30 kilometers around the Paris airport.
Let’s update the Program.cs
file to include a SearchAround()
method,
Instead of searching for documents that contain some keywords, with GeoSpatial Indexes, we perform searches based on their positions.
To find all landmarks close to the airport, we passed to the Search()
method, a filter, count, and sort order. We created the filter from the same SpatialStrategy
that we used before. And we wrote a SpatialArg
to query all documents inside a circle, centered at a starting point with a given radius in kilometers. Like this,
Then, we created a response object from each found document and calculated the distance from the starting point. Like this,
Notice that we parsed our landmark locations and used the CalcDistance()
with the starting point and found our document position.
These are five indexed landmarks closest to the airport:

Figure 1: Closest landmarks to Paris Airport
NCache Details Download NCache Full-Text Indexing with NCache Lucene
Conclusion
To implement GeoSpatial indexes for Distributed Lucene, we need SpatialDocument
instead of a plain Document
.
Since NCache implements Lucene.NET API, we can scale our Lucene code with NCache by changing a few lines of code and by following a few naming conventions.
To learn about other recent NCache features, check What’s New in NCache? For more details about GeoSpatial Indexes with Distributed Lucene, check Distributed Lucene Geo-Spatial API.
To follow along with the code, we wrote in this post, check my NCache Demo repository on GitHub.