Algolia:如何根据值对搜索进行排名?
Posted
技术标签:
【中文标题】Algolia:如何根据值对搜索进行排名?【英文标题】:Algolia: how to rank search based on a value? 【发布时间】:2017-02-16 08:50:38 【问题描述】:如何根据给定值对我的 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)"
],
]
【问题讨论】:
【参考方案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
【讨论】:
以上是关于Algolia:如何根据值对搜索进行排名?的主要内容,如果未能解决你的问题,请参考以下文章