我们是否可以在Algolia的单个索引上放置多个排序/自定义排名,而不为每个排序创建多个副本索引。
让我们举个例子来理解,假设我的应用程序是SalesManagement
,我创建了3个副本MobileSale
、LaptopSale
和PhoneSale
,然后在每个副本中,我分别对type = 'mobile',type = 'laptop' and type = 'phone'
进行自定义排序。
我只想创建一个索引,并通过在代码中使用IndexSetting
发送不同的设置来在编译时进行排序,这样我就不需要创建多个索引
使用IndexSetting
的ranking
属性,它对我的有效
private readonly SearchClient algoliaClient;
private readonly SearchIndex feedIndex;
public AlgoliaService( )
{
algoliaClient = new SearchClient("app-id", "api-key");
SearchIndex = algoliaClient.InitIndex(_defaultSettings.Algolia.FeedIndex);
algoliaClient.InitIndex("user-index-name");
}
public async Task SetCustomSortingAttributes(string sortBy)
{
if (sortBy != null)
{
List<string> sortingAttributesList = new List<string>();
StringBuilder sortAttribute = new StringBuilder();
if (sortBy == "most_viewed")
{
sortAttribute.Append("desc(most_viewed)");
}
else if (sortBy == "entityUniqueViews" )
{
sortAttribute.Append("desc(entityUniqueViews)");
}
else if (sortBy == "latest")
{
sortAttribute.Append("desc(latest)");
}
else if (sortBy == "price_asc")
{
sortAttribute.Append("asc(price)");
}
else
{
sortAttribute.Append("desc(price)");
}
sortingAttributesList.Add(sortAttribute.ToString());
IndexSettings settings = new IndexSettings
{
Ranking = sortingAttributesList
};
var setIndexSetting = await feedIndex.SetSettingsAsync(settings);
}
}