The Search API Location module adds support for indexing location values provided by the Geofield module and subsequently filtering or sorting on them, provided the service class supports this. Currently, only the Search API Solr module is known to support this feature.

Search API Location adds a new data type, "Latitude/longitude" to the list of fields in the Search API index. We can use this to index the Lat/Long Pair property of Geofield fields. First, we have to add the location field to the list of fields to index. In this video, I will show you how to do that, edit the Search View and add a filter criteria for location.

First, let's install and enable the Search API Location module.

Install module

Enable module

  • Go to "Modules" (admin/modules) and enable:
    • Search API location
    • Search API location views
  • (Save Configuration)

Configure Search API Location

  • Go to "Configuration => Search and Metadata => Search API" (admin/config/search/search_api)
  • Click the dropdown next to "Node Index"
  • Select "Fields"
  • Click "Add related fields"
  • Select "Location"
  • (Add fields)
  • [x] Location » LatLong Pair (LatLong Pair has the data that Search API can use.)
    • Type: Latitude/longitude (This will let the Solr knows that we want to treat this as geolocation data)
  • (Save changes)

Since we have added the Latlong Pair in the fields to be indexed, we have to re-index the items in the server.

Index node

  • Click "View" tab
  • (Index now)

Now that we have added a location field and refreshed the index on the server, let's edit our search view to add a location filter.

Edit Views

  • Go to "Structure => Views => Search View" (admin/structure/views/view/search_view)

Filter Criteria

  • Click "Add"
  • [x] Location: LatLong Pair (indexed)
  • (Apply(all displays))
  • [x] Expose this filter to visitors, to allow them to change it
  • Label: Location
  • [x] Expose operator
  • Input method: Geocoder Input (This method is more user friendly than the raw input method.)
  • Input method settings
    • Handler: Google Geocoder (This will let us enter an address that Google understands, like “Wichita, KS” instead of having to use latitude and longitude.
  • (Apply(all displays))
  • (Save) (Save the view)

Now when we go back to our home page, we can search based on location. Select "5KM" in the location field and enter "Wichita, KS" in the location text box then click "Search". This will display content tagged within 5KM of Wichita, KS.