How do I get an array of objectIDs from my search query?

I have the following code:

index.search(query: "Chicken") { result in
if case .success(let response) = result {
print("Algolia Response: \(response)")
}
}

The response is rather complex. How can I just get the array of objectIDs? E.g., ["id12345","id98765"]

If you only need the objectIDs from a search, you can specify it in the attributesToRetrieve to simplify the response:

index.search('query', {
  attributesToRetrieve: [
    "objectID"
  ]
}).then(({ hits }) => {
  console.log(hits);
});

Otherwise, you can perform a map operation to extract the objectID from the records:

index.search('query string').then(({ hits }) => {
  let objectIDs = hits.map(hit => hit.objectID);
  console.log(objectIDs);
});

Out of curiosity, what’s your use case for only needing the objectIDs? If this is for an ops task, you may want to consider using the CLI:

Thank you! Is there a way to access another attribute directly? For example, response.hits[0].objectID also returns an objectID. Is there a way to access one of my data attributes similarly? For example, I’ve seen other applications do it like the following response.hits[0].document["firstName"]