Updating JSON using PartialUpdateObject without direct access to objectID

it’s my understanding that to use PartialUpdateObject in Algolia, I need to know the objectID. Source

However, in my situation, I do not directly know the objectID right off the bat. Let me explain…

I have a music site where an artist can submit music and this JSON filled with song info gets stored into an index in Algolia. Now the artist can also update their profile (to change their artist name and so on). I have two separate indices. One called Song, which contains all song information, and Artist which contains artist information. When the artist edits his profile to change his artist name, I am trying to make this change reflect across his Artist account JSON and all the songs that were uploaded by him/her. If the JSON wasn’t updated on the song side, it would still display his/her old name…not good!

Here’s what the Artist JSON looks like:

{
  "AccountInfoID": 94,
  "AccountType": "Artist",
  "DisplayName": "Unlike Pluto",
  "ImageURL": "https://ucarecdn.com/81abba2d-7966-48ac-abab-79a2ecc97811/",
  "UserID": "",
  "objectID": "94"
}

Here is what the Song JSON looks like. Also note how the one constant between the two json files is the AccountInfoID, this links the song to the artist, in this case Unlike Pluto. The objectID for Song is different. And if I were to upload another song, all the following values would be different except AccountInfoID (just to clarify):

{
  "ApprovalFL": true,
  "FreeFL": true,
  "LicenseFL": true,
  "AccountInfoID": 94,
  "AlbumID": 117,
  "SongID": 105,
  "BPM": 92,
  "AccountImageURL": "https://ucarecdn.com/81abba2d-7966-48ac-abab-79a2ecc97811/",
  "AccountType": "Artist",
  "AlbumName": "Let It Bleed",
  "Artist": "Unlike Pluto",
  "FeaturedArtist": "Cristina Gatti",
  "ImageURL": "https://ucarecdn.com/d60c9302-eacf-4050-920d-9d54ce93cd46/",
  "iTunesURL": null,
  "LabelName": "Lowly Palace",
  "Title": "Let It Bleed",
  "URL": "https://ucarecdn.com/b75e6e30-7082-49e5-b5d5-93d23e74af21/",
  "UserID": "",
  "UploadDate": "2017-12-16T01:55:22.81",
  "Duration": "2017-12-16T00:03:20",
  "objectID": "105"
}

To update the Artist JSON, it’s no big deal:

dynamic registerArtistObjs = new JObject();
registerArtistObjs.DisplayName = accountInfo.DisplayName;
registerArtistObjs.ImageURL = accountInfo.ImageURL;
registerArtistObjs.objectID = accountInfo.AccountInfoID;
var artistIndexHelper = HttpContext.Application.Get("ArtistIndexHelper") as IndexHelper<ArtistAlgoliaModel>;
artistIndexHelper.PartialUpdateObject(registerArtistObjs);

However, the song update is trickier as I need to be able to find all songs that were uploaded by that artist. So as mentioned above, I would need to find all songs with AccountInfoID: 94 so that I could get the objectID and update Artist in the song JSON. What is the best approach to doing that or is there an easier way that I am missing?

Here’s where I’m at with the code to update the Artist name in the Song index, but I have no clue on how to obtain all the objectID values from the Song index that contain, for example, AccountInfo: 94:

dynamic registerSongObjs = new JObject();
registerSongObjs.Artist = accountInfo.DisplayName;
registerSongObjs.objectID = ??????????Possibly Multiple values?????????
var songIndexHelper = HttpContext.Application.Get("SongIndexHelper") as IndexHelper<SongAlgoliaModel>;
songIndexHelper.PartialUpdateObject(registerSongObjs);

Hi Jordan,

A possible solution to your issue would be to use the browse method to be able to search all of your index. In your specific example, you could do the following:

var browser = index.browseAll({ query: '', filters: 'AccountInfoID=94' });

browser.on('result', function onResult(content) {
  // update result with partialUpdateObject
});

browser.on('error', function onError(err) {
  throw err;
});

You can learn more about the browseAll method here.

However, I am unclear about your setup.

Are you using Algolia as your primary source of truth instead of a database? If so, I would strongly recommend having a database that would hold all the data and propagate changes to Algolia. That way, determining which songs belong to which artist would become much simpler.

If you are using a database, and what troubles you is finding a way to know the objectID of Songs and Artists, I wanted to let you know that you can set objectIDs arbitrarily, so they could match your keys in the database if you want them to.

I hope this solves your issue.
Have a great day!

thanks for the help! I have a database that stores all song information and then pushes to algolia :slight_smile: I ended up being able to figure out how to access my objectID by doing the following:

dynamic registerSongObjs = new JObject();
var songs = EntityDataAccess.GetSongsByAccountInfoID(accountInfo.AccountInfoID);
foreach (var item in songs)
{
registerSongObjs.Artist = accountInfo.DisplayName;
registerSongObjs.objectID = item.SongID;
songIndexHelper.PartialUpdateObject(registerSongObjs);
}