“Car sale application” – Spatial Search, adding location data (part 3)

The amount of announcements in our database is so large, that our web site users started to look for another option to filter search results and another way of sorting them. We need to add the functionality, which allows us to operate with localization data related to the cars.

Requirements specification

We would like to add two new functionalities:

  1. Filtering the results in order to display only those announcements, that are located not farther than x kilometres from the given place, where x = 50,100,200,500,1000 km.
  2. Sorting the results using the distance between the given place and the given car’s localization.

In order to face the requirements, we need to use solr’s functionality called “Spatial Search”, that is available in solr distribution from version 3.1. The changes we need to provide are related to schema.xml file modifications and the input data changes, where we have to add the information about the localization of every car. In the end we will create proper requests.

Schema.xml changes

  1. New field types definitions:
    • the first definition is nothing more than another numerical type:
    • the second definition uses the “solr.LatLonType” class, which allows us to index localization data using the dynamic field with suffix “_coordinate”:
  2. New fields definitions:
    • field, that will be used to accumulate the city name data, that is related to every car:
    • “loc” field will be used to index localization data:
    • the dynamic field used internally to accumulate the information provided by the “loc” field:

Input data analysis

In order to present how to modify the input data, let’s take 5 announcements from the cities:

  1. Koszalin
    • latitude: 54.12
    • longitude: 16.11
  2. Białystok
    • latitude: 53.08
    • longitude: 23.09
  3. Szczecin
    • latitude: 53.25
    • longitude: 14.35
  4. Gdańsk
    • latitude: 54.21
    • longitude: 18.40
  5. Warszawa
    • latitude: 52.15
    • longitude: 21.00

We provide the localization data by entering the latitude and longitude separated by the comma in the “loc” field. Our data might look like this:

Let’s create queries

We have our localization data in the index, so all we need right now is to create queries that will satisfy our needs. Let’s imagine, that we are searching for announcements when being in Białystok city, which is located about 200 km away from the Warszawa city, about 400 km away from the Gdańsk city, about 550 km away from the Koszalin city and about 650 km away from the Szczecin city.

To execute the first point from the requirements specification, we add the special filter query to our request:


  • sfield – the name of the field, where we have our localization data indexed.
  • pt – the localization of the starting point, it is the Białystok city in our case.
  • d – the distance used to narrow the search results. By using the 50,100,200,500,1000 values we can satisfy all our needs.


  1. Query:
  2. Search results:

That’s great, we don’t have any announcements from the Koszalin, Gdańsk or Szczecin city, as these cities are located farther than 200 km from the Białystok city.

To execute the second point from the requirements specification, we use the possibility to sort the search results by using the geodist function. The query would look like this:

The example of sorting the search results using the distance, starting from the Białystok city:

  1. Query:
  2. Search results:

That’s correct! Mission accomplished.

The end

Once more we are up to our website users expectations. This time we have added the functionalities, which allow our users to filter and sort the search results using the localization and distance data. Full success!

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.