Algolia:如何根据价值对搜索进行排名?

Algolia: how to rank search based on a value?

我如何根据给定值对我的 algolia 记录进行排名? 该值是基于用户的 ip 位置的动态值。

我希望 SC 状态在列表中显示在第一位,我该怎么做?

例如我有一个记录

[
 {
   "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"}
   ......
 }, 
]

我的搜索代码是这样写的

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

我的索引设置

[
 'searchableAttributes' => [
  'general_name',
  'name',
  'category',
  'address.city',
  'address.state',
  'address.zip_code',
  ],
 'customRanking' => [
   "desc(address.state)",
   "desc(general_name)"
  ],
]

您可以使用 geo-search 功能根据用户的位置对结果进行排序。这是实施它的指南:https://www.algolia.com/doc/guides/geo-search/geo-search-overview/#filter-and-sort-around-a-location

API可以直接提供ip,会自动转换成对应的坐标:https://www.algolia.com/doc/guides/geo-search/geo-search-overview/#by-looking-at-the-ip-address

要实现它,您需要将您的 address.state 字段转换为 lat/lng 坐标以使其工作。您可以查看此 SO 线程以找到一个数据库来执行此操作:Latitude and Longitude based on Zip Code