Partial Update Object not creating object if it doesn't exist after clearing the database

According to the documentation:

Partial Update Ojects#

The Partial Update Objects method requires an objectID .

  • If the objectID exists, the attributes will be replaced
  • If the objectID is specified but does not exist, the record is created
  • If the objectID is not specified, the method returns an error

But I’m not observing this behavior. I’ve cleared up my database, and trying to create objects via the partial update method:

async function algoliaUpdate(data: any, indexName: string) {
  const ALGOLIA_ID = functions.config().algolia.app_id;
  const ALGOLIA_ADMIN_KEY = functions.config().algolia.api_key;
  const client = algoliasearch.default(ALGOLIA_ID, ALGOLIA_ADMIN_KEY);
  const index = client.initIndex(indexName);
  try {
    const result = await index.partialUpdateObject(data);
    console.log(`Partially updated object Algolia: ${result.objectID}`);
  } catch(error) {
    console.log(`Could not partially update Algolia: ${error}`);
  }
}

Weird enough, no error is through, I can see a result.objectID, but when I check algolia’s index it is still empty, only when I use the add function it adds to the database. Any idea what could be going on? Is this a known bug?

Hi there! I think the issue might be that you are not setting createIfNotExists to true. (The default is false for Java, JavaScript, and .NET). When false , a partial update on a nonexistent object will be ignored (but no error will be sent back).

You can set this using requestOptions, a second optional parameter. E.g.,

index.partialUpdateObject({objectID: 'aNewId'}, {createIfNotExists: true}).then(console.log)

Indeed, without setting this, I ran into the same issue you were. Hope this helps!

Thank you very much, I’ll give it a try. In my case I was using Node.js, so makes sense according to what you mentioned. I think they should update this though, it is quite confusing, specially the fact that it raises no error and it given returns the objectID although it hasn’t been created.

Thanks for your feedback. Indeed, I can see how this can be confusing. Passed on the feedback to our product team!