Algolia DevCon
Oct. 2–3 2024, virtual.
Api clients / JavaScript / V3 / Methods

Get Objects | JavaScript API Client V3 (Deprecated)

Deprecated content
This documentation is for a deprecated version of JavaScript API client. Some features and settings may be missing or their usage may have changed. Refer to the documentation for the latest version of JavaScript API client for up-to-date information.
Required API Key: any key with the search ACL
Method signature
index.getObjects(array objectIds, callback)
index.getObjects(array objectIds, array attributesToRetrieve, callback)

// get a single object
index.getObject(string objectId, callback)
index.getObject(string objectId, array attributesToRetrieve, callback)

// multipleGetObjects is not implemented

About this method

Get one or more objects using their objectIDs.

There are three methods you can use to retrieve your objects:

  1. The getObject method lets you retrieve a single object from a specified index.
  2. The getObjects method lets you retrieve multiple objects from a specified index.
  3. The multipleGetObjects method lets you retrieve multiple objects from multiple indices within the same app. This method is called from the client.

With getObject and getObjects, you can also specify which attributes to retrieve. This list applies to all objects, and defaults to all attributes.

Objects are returned in the order in which they were requested.

Examples

Retrieve a set of objects with their objectIDs

1
$index->getObjects(['myId1', 'myId2']);

Retrieve a set of objects with only a subset of their attributes

Optionally you can specify a comma separated list of attributes you want to retrieve.

1
2
3
$index->getObjects(['myId1', 'myId2'], [
  'attributesToRetrieve' => ['firstname', 'lastname']
]);

Note: This will return an array of objects for all objects found; for those not found, it will return a null.

Retrieve only one object

1
2
3
4
5
6
7
8
// Retrieves all attributes
$index->getObject('myId');

// Retrieves only firstname and lastname attributes
$index->getObject('myId', [
  'attributeToRetrieve' => ['firstname', 'lastname'],
]);

If the object exists it will be returned as is. Otherwise the function will return an error and not null like getObjects.

Retrieve several objects across several indices

1
2
3
4
5
6
7
8
9
10
$client->multipleGetObjects(array(
  [
    "indexName" => "index1",
    "objectID" => "myId1"
  ],
  [
    "indexName" => "index2",
    "objectID" => "myId2"
  ]
));

Retrieve a set of objects and send extra HTTP headers

1
2
3
4
5
$objectIDs = [/* objectIDs */];

$index->getObjects($objectIDs, [
  'X-Forwarded-For' => '94.228.178.246'
];

Parameters

objectIDs
type: list
Required

List of objectIDs to retrieve.

attributesToRetrieve
type: string|array
default: ""
Optional

Comma separated list of attributes to retrieve.

By default, all retrievable attributes are returned.

requestOptions
type: key/value mapping
default: No requestOptions
Optional

A mapping of [`requestOptions`](/doc/api-client/getting-started/request-options/) to send along with the query.

objectID
type: integer|string
Required

The objectID of the object to get.

multipleObjects
type: list of multipleObject
true (for multipleGetObjects)

A list of mappings that represent specific objects in specific indices.

multipleObjects ➔ multipleObject

The multipleObject parameter must have exactly two key/value mappings:

  • indexName to specify the index in which to search (as a string).
  • objectId to specify the objectID of the record to retrieve (as a string).

Check out this example for reference.

Response

In this section we document the JSON response returned by the API. Each language will encapsulate this response inside objects specific to the language and/or the implementation. So the actual type in your language might differ from what is documented.

JSON format

1
2
3
4
5
6
7
8
{
  "results": [
    {
      "objectID": "1182729442",
      "name": "product 1"
    }
  ]
}

Here is an example where we try to get two objects. The first one doesn’t exist (null is returned) but the second one does (the object is returned). If an object does not exist, null will be return instead.

1
2
3
4
5
6
7
8
9
10
{
  "results": [
    null,
    {
      "objectID": "1182729442",
      "name": "product 1"
    }
  ],
  "message": "ObjectID 1182729442 does not exist."
}
results
list

List of the retrieved objects.

Did you find this page helpful?