If you need to do the solr distance search based on the city or the zipcode, latitude and longitude needs to be passed to the solr in query filter query (fq). With geofilt sfiled describes the indexed field which holds the geo location coordinates.
In sfield solr field which contains the latitude+longitude (comma separated) information needs to be passed.
In this example locs_field_chapter_geofield holds such information.
In the hook_search_api_solr_query_alter with filter query (fq) geofilt
and pt (latitude+longitude comma separate), d (radius in km or miles)
and score (type search km/miles) solr is hit for the distnace search.
function modulename_search_api_solr_query_alter(\Solarium\Core\Query\QueryInterface $solarium_query, \Drupal\search_api\Query\QueryInterface $query) {
$zip = (isset($_REQUEST['zip'])) ? trim(strtolower($_REQUEST['zip'])) : '';
$state = (isset($_REQUEST['state'])) ? trim(strtolower($_REQUEST['state'])) : '';
$radius = (isset($_REQUEST['radius'])) ? $_REQUEST['radius'] : 5;
$radius_type = (isset($_REQUEST['radius_type'])) ? $_REQUEST['radius_type'] : 'miles';
$lat_lng = '';
if ($zip) {
$lat_lng = modulename_get_latitue_longitude($zip);
}
if (empty($state) && !empty($lat_lng)) {
$solarium_query->addParam('pt', $lat_lng);
$solarium_query->addParam('d', $radius);
$solarium_query->addParam('score', $radius_type);
$solarium_query->addParam('fq', '{!geofilt sfield=locs_field_chapter_geofield}');
}
}
}
This function returns the geolocation latitude+longitude (comma separated) data.
function modulename_get_latitue_longitude($zip) { $geolocation = \Drupal::service('geolocation.core'); $geocoderManager = $geolocation->getGeocoderManager(); $geocoder = $geocoderManager->getGeocoder('google_geocoding_api'); $geocode = $geocoder->geocode($zip); $lat_lng = ''; if (isset($geocode['location'])) { $lat_lng = $geocode['location']['lat'] . ',' . $geocode['location']['lng']; } return $lat_lng; }