Is it possible to fetch an Algolia object any other way than by objectID?

So my goal is to be able to update a piece of data in my app and also update the algolia object, at the same time. The issue is that I want to create a function that can fetch an Algolia object based off it’s name and school ID, and then retrieve it’s objectID, but you can only get objects by objectID. When an object is added to my index, a random generated objectID is given to it by a function I created, i won’t be able to know the objectID every time, so I wanted to know if there’s a workaround where I can get the objectID of a Algolia object without using the method getObject(withID:). Thanks


So I figured out this work around by simply creating a variable that is a random number with 8 digits, and implementing it in the objectID of the record before it it added, as well as creating a new field in my Firestore document with the same variable. To my surprise, they successfully match up each time. I then used a function to make a query of that field, get it as a String, and place it in a textfield in a specific viewController so I can use it as a stored value for later. I can now use that stored algoliaObjectID as a parameter in the index.partialUpdateObject() method, and it’s dynamic to every single piece of data which is awesome. The big issue now is the value: JSON parameter in the method. I want the value of the value parameter to be dynamic as well but it only takes in JSON and can’t figure out how I can work around that as well. If anybody knows and can help, that would be great. Thanks

The value can be dynamic like this: Partial Update Objects | Indexing | Method | API Reference | Algolia Documentation

const myValue = 123;

  myValue: myValue,
  objectID: 'myID'

is that what you were looking for @dantesauder?

By the way, you can also use the firebase id for the document as the objectID when indexing to algolia (if I remember it correctly, it’s, as long as you add that value as the objectID when indexing!

Hey man, this isn’t in Swift, I need it in Swift, the methods are completely different across both languages.

there’s a language switcher on the documentation I linked @dantesauder: Partial Update Objects | Indexing | Method | API Reference | Algolia Documentation

let value = "California"

index.partialUpdateObject(withID: "myID", with: .update(attribute: "state", value)) { result in
  if case .success(let response) = result {
    print("Response: \(response)")

Yeah that’s why I came, here I already checked the docs, they didn’t really help. Plus the code you sent me wouldn’t work in my case, it’s static and not really dynamic, my value is in a textfield and I can’t figure out a way to convert whatever is in the textfield to JSON so I was wondering if you knew a workaround of some sort.

You can convert any string value to JSON using its convenient initializer:

let textFieldValue = myTextField.text ?? ""
let jsonValue = JSON(textFieldValue)

Then you can use the jsonValue in the partialUpdate method mentioned above.