如何根据给定值对我的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)"
],
]
答案 0 :(得分:1)
您可以使用地理搜索功能根据用户的位置对结果进行排序。以下是实施该指南的指南: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