使用Algolia C#

时间:2017-12-15 04:57:44

标签: c# algolia

我有一个属性,我想添加为faceting:ApprovalFL。当我在Algolia仪表板中添加facet时,它工作正常,直到我必须重新索引,最终由于某种原因在仪表板中删除我的facet。我在想,也许在我的代码中添加属性可以解决这个问题。我在此处找到了有关此步骤的文档:https://www.algolia.com/doc/api-reference/api-parameters/attributesForFaceting/

这是他们提供的C#示例:

index.SetSettings(
  JObject.Parse(@"{""attributesForFaceting"":[""author"",""filterOnly(category)"",""searchable(publisher)""]}")
);

如果我理解正确,此代码需要在我的后端重建索引代码中找到(在我的AdminController.cs中找到):

public async Task<ActionResult> ReIndexData()
{
    var algoliaArtistModels = Tools.BuildAlgoliaArtistModels(EntityDataAccess.GetAllAccountInfoes());
    var algoliaUnaffiliatedArtistModels = Tools.BuildAlgoliaArtistModels(EntityDataAccess.GetAllUnaffiliatedAccountInfo());
    var algoliaSongModels = Tools.BuildAlgoliaSongModels(EntityDataAccess.GetAllAcceptedSongs());
    var artistIndexHelper = HttpContext.Application.Get("ArtistIndexHelper") as IndexHelper<ArtistAlgoliaModel>;
    var unaffiliatedArtistIndexHelper = HttpContext.Application.Get("UnaffiliatedArtist") as IndexHelper<ArtistAlgoliaModel>;
    var songIndexHelper = HttpContext.Application.Get("SongIndexHelper") as IndexHelper<SongAlgoliaModel>;
    await artistIndexHelper.OverwriteIndexAsync(algoliaArtistModels);
    await unaffiliatedArtistIndexHelper.OverwriteIndexAsync(algoliaUnaffiliatedArtistModels);
    await songIndexHelper.OverwriteIndexAsync(algoliaSongModels);
    return View("AlgoliaReIndexData");
}

但是,我不认为我将index.setSettings放在这个代码块中,在我的后端代码中为faceting设置此属性的最佳方法是什么? ApprovalFL属性存储在我的歌曲索引中。

可能它应该放在Tools.cs

中的某个地方
public static SongAlgoliaModel BuildAlgoliaSongModel(Song song)
        {
            var model = new SongAlgoliaModel();
            var album = EntityDataAccess.GetAlbumByID(song.AlbumID);
            model.AccountImageURL = album.AccountInfo.ImageURL;
            model.AccountInfoID = album.AccountInfoID;
            model.AccountType = album.AccountInfo.CreatorFL == true ? "Creator" : "Artist";
            model.AlbumID = song.AlbumID;
            model.AlbumName = album.AlbumName;
            model.ApprovalFL = song.ApprovalFL;
            model.Artist = album.AccountInfo.DisplayName;
            model.BPM = song.BPM;
            model.Duration = song.Duration;
            model.FeaturedArtist = song.Artist;
            model.FreeFL = album.FreeFL;
            model.ImageURL = album.ImageURL;
            model.iTunesURL = album.iTunesURL;
            model.LabelName = album.LabelName;
            model.LicenseFL = album.LicenseFL;
            model.SongID = song.SongID;
            model.Title = song.Title;
            model.UploadDate = song.UploadDate;
            model.URL = song.URL;
            model.UserID = album.AccountInfo.UserID;

            return model;
        }
        public static List<SongAlgoliaModel> BuildAlgoliaSongModels(AccountInfo accountInfo)
        {
            var list = new List<SongAlgoliaModel>();
            var songs = EntityDataAccess.GetSongsByUserID(accountInfo.UserID).Where(x => x.ApprovalFL == true).ToList();
            foreach(var item in songs)
            {
                var model = new SongAlgoliaModel();
                model.AccountImageURL = item.Album.AccountInfo.ImageURL;
                model.AccountInfoID = item.Album.AccountInfoID;
                model.AccountType = item.Album.AccountInfo.CreatorFL == true ? "Creator" : "Artist";
                model.AlbumID = item.AlbumID;
                model.AlbumName = item.Album.AlbumName;
                model.ApprovalFL = item.ApprovalFL;
                model.Artist = item.Album.AccountInfo.DisplayName;
                model.BPM = item.BPM;
                model.Duration = item.Duration;
                model.FeaturedArtist = item.Artist;
                model.FreeFL = item.Album.FreeFL;
                model.ImageURL = item.Album.ImageURL;
                model.iTunesURL = item.Album.iTunesURL;
                model.LabelName = item.Album.LabelName;
                model.LicenseFL = item.Album.LicenseFL;
                model.SongID = item.SongID;
                model.Title = item.Title;
                model.UploadDate = item.UploadDate;
                model.URL = item.URL;
                model.UserID = item.Album.AccountInfo.UserID;
                list.Add(model);
            }
            return list;
        }

2 个答案:

答案 0 :(得分:2)

对于那些使用C#.NET的人来说,就像将它添加到Global.asax.cs一样简单,例如对我而言:

var songIndexHelper = new IndexHelper<SongAlgoliaModel>(algoliaClient, "Song", "SongID");
songIndexHelper.SetSettings(JObject.Parse(@"{""attributesForFaceting"":[""ApprovalFL""]}"));

答案 1 :(得分:0)

为什么你必须重新索引?最佳做法是最初只设置索引。