Apache Solr Proximity Search and sort by Distance

I wrote a custom module to perform a search of an index I created using Search API and Search API Solr Search. The search is a proximity search that finds the closest locations using a bounding box, but I can’t figure out how to SORT the results I get back by the closest. Here is my module code in entirety:

<?php  function s_test() {    $index      = search_api_index_load("articles");   $query      = new SearchApiQuery($index);    $lat        = 41.920110;   $lon        = -87.680433;    //get min/max lat and long   $check_radius = 25; // radius of bounding circle in miles   $earth_radius = 3960;  // earth's mean radius, miles    // first-cut bounding box (in degrees)   $max_lat = $lat + rad2deg($check_radius/$earth_radius);   $min_lat = $lat - rad2deg($check_radius/$earth_radius);    // compensate for degrees longitude getting smaller with increasing latitude   $max_lon = $lon + rad2deg($check_radius/$earth_radius/cos(deg2rad($lat)));   $min_lon = $lon - rad2deg($check_radius/$earth_radius/cos(deg2rad($lat)));    // set the bounding box   $filter = $query->createFilter('AND');   $filter->condition('latitude', $max_lat, '<=');   $filter->condition('latitude', $min_lat, '>=');   $filter->condition('longitude', $max_lon, '<=');   $filter->condition('longitude', $min_lon, '>=');   $query->filter($filter);    // run query   $data       = $query->execute();   $results    = $data['results'];    print_r($results); }  function s_index() {   $index      = search_api_index_load("articles");    $index->reindex();   search_api_index_items($index);   print '<pre>';   print_r($index);   print '</pre>'; }  function test_query_menu() {   //create the search page   $items['test-search'] = array(     'title' => t('Search'),     'page callback' => 's_test',     'access callback' => TRUE,     'type' => MENU_NORMAL_ITEM   );   //create a re-index page   $items['test-index'] = array(     'title' => t('Index'),     'page callback' => 's_index',     'access callback' => TRUE,     'type' => MENU_NORMAL_ITEM   );   return $items; }  /**  * Implements hook_entity_property_info_alter().  */ function test_query_entity_property_info_alter(&$info) {   $info['node']['properties']['latitude'] = array(     'type' => 'decimal',     'label' => t('Latitude'),     'sanitized' => TRUE,     'getter callback' => 'test_query_search_api_property_latitude_getter_callback',   );   $info['node']['properties']['longitude'] = array(     'type' => 'decimal',     'label' => t('Longitude'),     'sanitized' => TRUE,     'getter callback' => 'test_query_search_api_property_longitude_getter_callback',   );   $info['node']['properties']['latlongpair'] = array(     'type' => 'string',     'label' => t('LatLong Pair'),     'sanitized' => TRUE,     'getter callback' => 'test_query_search_api_property_latlongpair_getter_callback',   );   $info['node']['properties']['logo_uri'] = array(     'type' => 'string',     'label' => t('LOGO URI'),     'sanitized' => TRUE,     'getter callback' => 'test_query_search_api_property_logo_uri_getter_callback',   ); }  /**  * Getter callback for latitude.  */ function test_query_search_api_property_latitude_getter_callback($item) {   return $item->field_address[LANGUAGE_NONE][0]['latitude'];  }  /**  * Getter callback for longitude.  */ function test_query_search_api_property_longitude_getter_callback($item) {   return $item->field_address[LANGUAGE_NONE][0]['longitude'];  }  /**  * Getter callback for latitude/longitude pair.  */ function test_query_search_api_property_latlongpair_getter_callback($item) {   return $item->field_address[LANGUAGE_NONE][0]['latitude'] . ',' . $item->field_address[LANGUAGE_NONE][0]['longitude'];  }  /**  * Getter callback for logo uri.  */ function test_query_search_api_property_logo_uri_getter_callback($item) {   return $item->field_image[LANGUAGE_NONE][0]['uri'];  } 

Basically I exposed my latitude and longitude to Solr and used those to set the bounding box and this works find to return results close to me, but I don’t see any field in the results set that would allow me to sort by the node with the closest location.

I also tried installing the Search API Location module, but I still can’t find anything in the results set to sort by.

I’m using Location and Location CCK module to get the lat/long.

If anyone has experience/insight with this that would be groovy.

This article was republished from its original source.
Call Us: 1(800)730-2416

Pixeldust is a 20-year-old web development agency specializing in Drupal and WordPress and working with clients all over the country. With our best in class capabilities, we work with small businesses and fortune 500 companies alike. Give us a call at 1(800)730-2416 and let’s talk about your project.

FREE Drupal SEO Audit

Test your site below to see which issues need to be fixed. We will fix them and optimize your Drupal site 100% for Google and Bing. (Allow 30-60 seconds to gather data.)

Powered by

Apache Solr Proximity Search and sort by Distance

On-Site Drupal SEO Master Setup

We make sure your site is 100% optimized (and stays that way) for the best SEO results.

With Pixeldust On-site (or On-page) SEO we make changes to your site’s structure and performance to make it easier for search engines to see and understand your site’s content. Search engines use algorithms to rank sites by degrees of relevance. Our on-site optimization ensures your site is configured to provide information in a way that meets Google and Bing standards for optimal indexing.

This service includes:

  • Pathauto install and configuration for SEO-friendly URLs.
  • Meta Tags install and configuration with dynamic tokens for meta titles and descriptions for all content types.
  • Install and fix all issues on the SEO checklist module.
  • Install and configure XML sitemap module and submit sitemaps.
  • Install and configure Google Analytics Module.
  • Install and configure Yoast.
  • Install and configure the Advanced Aggregation module to improve performance by minifying and merging CSS and JS.
  • Install and configure Schema.org Metatag.
  • Configure robots.txt.
  • Google Search Console setup snd configuration.
  • Find & Fix H1 tags.
  • Find and fix duplicate/missing meta descriptions.
  • Find and fix duplicate title tags.
  • Improve title, meta tags, and site descriptions.
  • Optimize images for better search engine optimization. Automate where possible.
  • Find and fix the missing alt and title tag for all images. Automate where possible.
  • The project takes 1 week to complete.