Retrieving Location Data with Spring Solr

Location searching is an awesome Solr feature that you don't hear much about. In a future post we'll cover how to query by location using Spring Solr. Today we'll get things started by looking at how we handle location data retrieved with Spring Solr.

As you know I like to jump to the end and view our output, so below you see the Console output when we run NixMash Spring Solr as a Spring Boot app. We're displaying products with their store locations as well as products which have no associated store locations. (“Canon PowerShot” has location Longitude and Latitude info, for example, while “Solr, the Enterprise Search Server” does not.)

Solr Location Field

Before getting into the Java code, here's a reminder of what the Solr Location field looks like in the Collection's schema.xml file. Field “store” is of Solr type “location.” An example of it's data would be 45.196, -93.903 for longitude and latitude.

The Product Model

We begin with the Product Model object, which is as deep as we'll need to go in the application layer structure. No changes will be required in the Service or Repository layers. Prior to Spring Boot 1.3 we could have used a GeoLocation object to retrieve the Solr Location field directly. Since we're working in Spring Boot 1.3 we'll retrieve the “store” location field as a String and then to a Point.

We want to process the location value as a Spring Framework Point object to take advantage of its various functions on the client side. To avoid null values we'll assign the Product.point negative values if the Solr “store” field is null.

Here is the Spring Package info for our Point and GeoConverters.

Displaying the Location as a Point Object

We could simply output the location as a String, but we want to use the features in a Spring Point object. To do so we'll use getPoint().getX() and .getPoint.getY(). We'll use our negative point value logic when formatting the product output.

And again, our console output.

The @Test

We'll wrap things up by simply testing that a product with an empty location produces a negative Point value.

Source Code Notes for this Post

All source code discussed in this post can be found in my NixMash Spring GitHub repo and viewed online here.

Posted January 12, 2016 02:36 PM EST

More Like This Post