5

I am trying to set up a Solr Search View with Search API Solr and Search API location, witch is sorted by proximity an can be filtered by proximity. To achieve that,

  • I created an search index, which indexes a geofield as latitude/longitude (under admin/config/search/search_api/index/INDEX_NAME/fields):enter image description here
  • I created a View for the mentioned Search index and was able to add a proximity filter as contextual filter:enter image description here
    • It is also possible to add the indexed Lat/Lon field to the field section of the View and set up a formatter showing the distance to a certain geolocation: enter image description here

But there wasn't any field I could add to the sort section or the filter section of the View, that filters or sorts by proximity. What do I have to do, to filter and sort a Search API Solr View by proximity?

1

If you want to filter based on some user selection, you can have PHP code which gets the value and filters result based on that.

Please check this http://murtuza88.blogspot.in/2014/11/use-drupal-location-module-to-filter.html for detail steps on how to filter view based on user selection.

  • This does not work for a Search API view as it does not have the proximity field mentioned in the article above. Additionally you would have to use PHP filter module, what is not recommendable... – user5950 Jan 29 '16 at 13:55
0

You're wanting to sort the results by an arbitrary (unique values not stored in the database) values (distance). As said above, the most straightforward way to do that would be with php. The Views API is documented here and leads me to think that hook_views_post_execute() is the function you would need.

<?php
function hook_views_post_execute(&$view) {
  // If there are more than 100 results, show a message that encourages the user
  // to change the filter settings.
  // (This action could be performed later in the execution process, but not
  // earlier.)
  if ($view->total_rows > 100) {
    drupal_set_message(t('You have more than 100 hits. Use the filter settings to narrow down your list.'));
  }


  if ($view->name == 'job_distribution') {
    foreach ($view->result as $result) {
      // figure out the distance value
      // save it to the result array
    }
    // sort the result based on that value
    // maybe use http://php.net/manual/en/function.array-multisort.php
  }
}
?>

Good luck!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.