getObjects multi-index

Hello :slight_smile:

With my AlgoliaClient, I can do multi-indexes searches:

client.search([
	{query: "abc", "indexName": "books"},
	{query: "abc", "indexName": "authors"}
]);

But I can’t do the same with getObjects, what I want to do is:

client.getObjects([
	{"indexName": "books", "objectID": "book1"},
	{"indexName": "books", "objectID": "book2"},
	{"indexName": "authors", "objectID": "author1"}
]);

But today, what I have to do is:

client.initIndex("books").getObjects(["book1", "book2"]);
client.initIndex("authors").getObjects(["author1"]);

Do we have any way to do multiple get objects in one single request?
Thank you,
Arnaud.

1 Like

Hi @Alefort, according to the REST API Documentation the getObjects endpoint does support multiple indexes lookup:

Path: /1/indexes/*/objects
HTTP Verb: POST
Required API Key ACL: search

Description:
Retrieve multiple objects, potentially from different indices, in a single API call.

The POST body must contain:

requests (array of objects): list of objects to retrieve. Each object is identified by:
    indexName (string): name of the index containing the object
    objectID (string): ID of the object within that index
    attributesToRetrieve (array of strings) (optional): List of attributes to retrieve. By default, all retrievable attributes are returned.

So you should be able to get objects from different indices by using just a client, but as this use case seems pretty rare I think most API Clients didn’t implement it until now.

I can’t tell from your code, but what client are you using?
I shared your feedback with the team handling all clients, so they will consider it in their next sprint. To move your specific case faster, you might want to open a specific issue on your language’s client’s GitHub repository.

As a workaround, you can do a direct HTTP POST call following the REST Doc example:

curl -X POST \
     -H "X-Algolia-API-Key: ${API_KEY}" \
     -H "X-Algolia-Application-Id: ${APPLICATION_ID}" \
     --data-binary '{ "requests": [
                      { "indexName": "index1", "objectID": "obj1" },
                      { "indexName": "index1", "objectID": "obj2" }
                    ] }' \
    "https://${APPLICATION_ID}-dsn.algolia.net/1/indexes/*/objects"

Let me know if this helps!

Yes it’s help a lot, thank you. I currently use the Javascript client ( https://github.com/algolia/algoliasearch-client-javascript ), the original one and the AngularJS variant. I’ll try to open the issue :slight_smile:

1 Like

This endpoint will be added to the API clients, as well as this one, to index in multiple indices:

1 Like