0

how can i rank my algolia records based on a given value ? the value is dynamic based on the user's ip location.

i want SC state to show first in the list, how can i do that?

for example i have a records

[
 {
   "category":"Emergency Department",
   "address": {"state": "SC"}
   ......
 },
 {
   "category":"Emergency Department",
   "address": {"state": "NC"}
   ......
 }, 
 {
   "category":"Emergency Department",
   "address": {"state": "NC"},
   ......
 }, 
 {
   "category":"Radiology",
   "address": {"state": "WV"}
   ......
 }, 
 {
   "category":"PROVIDENCE HOSPITAL",
   "address": {"state": "TN"}
   ......
 }, 
 {
   "category":"PROVIDENCE HOSPITAL",
   "address": {"state": "TN"}
   ......
 }, 
]

i wrote my search code like this

algoliaIndex.search({
   query: 'Emergency Department' +' '+ 'SC',
 }, function(err, hits) {
  console.log(hits.hits);
});

my index settings

[
 'searchableAttributes' => [
  'general_name',
  'name',
  'category',
  'address.city',
  'address.state',
  'address.zip_code',
  ],
 'customRanking' => [
   "desc(address.state)",
   "desc(general_name)"
  ],
]
Hitori
  • 569
  • 1
  • 5
  • 21

1 Answers1

0

You can use the geo-search feature to sort the results based on the location of the user. Here is a guide to implement it: https://www.algolia.com/doc/guides/geo-search/geo-search-overview/#filter-and-sort-around-a-location

The API allows you directly provide an ip and it'll be automatically converted to the corresponding coordinates: https://www.algolia.com/doc/guides/geo-search/geo-search-overview/#by-looking-at-the-ip-address

To achieve it, you'll need to convert your address.state field to a lat/lng coordinates to make it works. You can have a look at this SO thread to find a database to do it: Latitude and Longitude based on Zip Code

Community
  • 1
  • 1
Nagriar
  • 91
  • 1
  • 1
  • 7