Add objects

Method signature
$index->addObjects(objects)
$index->addObjects(objects, requestOptions)
Method signature
index.add_objects(objects)
index.add_objects(objects, requestOptions)
Method signature
index.addObjects(objects, callback)
Method signature
index.add_objects(objects)
index.add_objects(objects, request_options=requestOptions)
Method signature
index.addObjects(objects, completionHandler)
index.addObjects(objects, requestOptions: requestOptions, completionHandler)
Method signature
index.addObjectsAsync(objects, null)
index.addObjectsAsync(objects, requestOptions, null)
Method signature
index.AddObjects(objects)
index.AddObjects(objects, "myID", requestOptions)
Method signature
index.addObjects(objects)
index.addObjects(objects, requestOptions)
Method signature
index.AddObjects(objects)
index.AddObjectsWithRequestOptions(objects, requestOptions)
Method signature
index into "index1" objects objects
index into "index1" objects objects options requestOptions

About this method

Add new objects (records = objects) to an index.

Required API Key ACL: addObject

Batching records allows you to reduce the number of network calls required for multiple operations. But note that each index object counts as a single indexing operation.

Examples

Add objects with automatic objectID assignments

<?php
$res = $index->addObjects(
  [
    [
      'firstname' => 'Jimmie',
      'lastname'  => 'Barninger'
    ],
    [
      'firstname' => 'Warren',
      'lastname'  => 'Speach'
    ]
  ]
);
res = index.add_objects([{
  firstname: 'Jimmie',
  lastname:  'Barninger'
}, {
  firstname: 'Warren',
  lastname:  'Speach'
}])
var objects = [{
  firstname: 'Jimmie',
  lastname: 'Barninger'
}, {
  firstname: 'Warren',
  lastname: 'Speach'
}];

index.addObjects(objects, function(err, content) {
  console.log(content);
});
res = index.add_objects([
    {"firstname": "Jimmie", "lastname": "Barninger"},
    {"firstname": "Warren", "lastname": "Speach"}
])
let obj1 = ["firstname": "Jimmie", "lastname": "Barninger"]
let obj2 = ["firstname": "Warren", "lastname": "Speach"]
index.addObjects([obj1, obj2], completionHandler: { (content, error) -> Void in
    if error == nil {
        print("Object IDs: \(content!)")
    }
})
List<JSONObject> array = new ArrayList<JSONObject>();

array.add(
  new JSONObject().put("firstname", "Jimmie").put("lastname", "Barninger")
);
array.add(
  new JSONObject().put("firstname", "Warren").put("lastname", "Speach")
);

index.addObjectsAsync(new JSONArray(array), null);
List<JObject> objs = new List<JObject>();

objs.Add(JObject.Parse(
  @"{""firstname"":""Jimmie"", ""lastname"":""Barninger""}"
));

objs.Add(JObject.Parse(
  @"{""firstname"":""Warren"", ""lastname"":""Speach""}"
));

var res = index.AddObjects(objs);

// Asynchronous
// var res = await index.AddObjectsAsync(objs);

System.Diagnostics.Debug.WriteLine(res);
//Sync & Async version

index.addObjects(Arrays.asList(
  new Contact().setFirstName("Jimmie").setLastName("Barninger"),
  new Contact().setFirstName("Warren").setLastName("Speach")
));
objects := []algoliasearch.Object{
  {"firstname": "Jimmie", "lastname": "Barninger"},
  {"firstname": "Ray", "lastname": "Charles"},
}

res, err := index.AddObjects(objects)
client.execute {
  index into "index1" objects Seq(
    Contact("Jimmie", "Barninger"),
    Contact("Warren", "Speach")
  )
}

Add objects with manual objectID assignments

<?php
$index->addObjects(
  [
    [
      'objectID' => 'myID1',
      'firstname' => 'Jimmie',
      'lastname'  => 'Barninger'
    ],
    [
      'objectID' => 'myID2',
      'firstname' => 'Warren',
      'lastname'  => 'Speach'
    ]
  ]
);
res = index.save_objects([{
  firstname: 'Jimmie',
  lastname:  'Barninger',
  objectID:  'myID1'
}, {
  firstname: 'Warren',
  lastname:  'Speach',
  objectID:  'myID2'
}])
var objects = [{
  objectID: 'myID1',
  firstname: 'Jimmie',
  lastname: 'Barninger'
}, {
  objectID: 'myID2',
  firstname: 'Warren',
  lastname: 'Speach'
}];

index.addObjects(objects, function(err, content) {
  console.log(content);
});
res = index.add_objects([
    {"objectID": "myID1", "firstname": "Jimmie", "lastname": "Barninger"},
    {"objectID": "myID2", "firstname": "Warren", "lastname": "Speach"}
])
let obj1 = ["objectID": "myID1", "firstname": "Jimmie", "lastname": "Barninger"]
let obj2 = ["objectID": "myID2", "firstname": "Warren", "lastname": "Speach"]
index.addObjects([obj1, obj2], completionHandler: { (content, error) -> Void in
  if error == nil {
      print("Object IDs: \(content!)")
  }
})
List<JSONObject> array = new ArrayList<JSONObject>();

array.add(
  new JSONObject().put("objectID", "myID1")
    .put("firstname", "Jimmie").put("lastname", "Barninger")
);
array.add(
  new JSONObject().put("objectID", "myID2")
    .put("firstname", "Warren").put("lastname", "Speach")
);

index.addObjectsAsync(new JSONArray(array), null);
List<JObject> objs = new List<JObject>();

objs.Add(JObject.Parse(
  @"{""objectID"":""myID1"",
    ""firstname"":""Jimmie"",
    ""lastname"":""Barninger""}"
));

objs.Add(JObject.Parse(
  @"{""objectID"":""myID2"",
    ""firstname"":""Warren"",
    ""lastname"":""Speach""}"
));

var res = index.AddObjects(objs);

// Asynchronous
// var res = await index.AddObjectsAsync(objs);

System.Diagnostics.Debug.WriteLine(res);
//Sync & Async version

index.addObjects(Arrays.asList(
  new Contact().setObjectID("myID1").setFirstName("Jimmie").setLastName("Barninger"),
  new Contact().setObjectID("myID2").setFirstName("Warren").setLastName("Speach")
));
objects := []algoliasearch.Object{
  {"objectID": "myID1", "firstname": "Jimmie", "lastname": "Barninger"},
  {"objectID": "myID2", "firstname": "Ray", "lastname": "Charles"},
}

res, err := index.AddObjects(objects)
client.execute {
  index into "index1" objects Seq(
    Contact("myID1", "Jimmie", "Barninger"),
    Contact("myID2", "Warren", "Speach")
  )
}

Add a single object

<?php
$index->addObject(
  [
    'objectID' => 'myID',
    'firstname' => 'Jimmie',
    'lastname'  => 'Barninger'
  ]
);
res = index.add_object({
  firstname: 'Jimmie',
  lastname:  'Barninger'
}, 'myID')

puts 'ObjectID=' + res['objectID']
index.addObject({
  objectID: 'myId',
  firstname: 'Jimmie',
  lastname: 'Barninger'
}, function(err, content) {
  console.log('objectID=' + content.objectID);
});
res = index.add_object({"firstname": "Jimmie", "lastname": "Barninger"}, "myID")
print "ObjectID=%s" % res["objectID"]
let newObject = [
  "objectID": "myID",
  "firstname": "Jimmie",
  "lastname": "Barninger"
]

index.addObject(
  newObject,
  completionHandler: { (content, error) -> Void in
    if error == nil {
      if let objectID = content!["objectID"] as? String {
          print("Object ID: \(objectID)")
      }
    }
  }
)
JSONObject object = new JSONObject()
    .put("firstname", "Jimmie")
    .put("lastname", "Barninger");

index.addObjectAsync(object, "myID", null);
var res = index.AddObject(JObject.Parse(
  @"{""firstname"":""Jimmie"", ""lastname"":""Barninger""}"
));

// Asynchronous
// var res = await index.AddObjectAsync(JObject.Parse(
//   @"{""firstname"":""Jimmie"", ""lastname"":""Barninger""}"
// ));

System.Diagnostics.Debug.WriteLine("objectID=" + res["objectID"]);
// Sync & Async version
index.addObject(
  new Contact()
    .setFirstName("Jimmie")
    .setLastname("Barninger")
    .setObjectID("objectID")
  )
);
object := algoliasearch.Object{
  "objectID":  "myID",
  "firstname": "Jimmie",
  "lastname":  "Barninger",
}
res, err := index.AddObject(object)
client.execute {
  index into "contacts" objectId "myID" `object` Contact(
    "Jimmie",
    "Barninger",
    93,
    "California Paint"
  )
}

Add objects and send extra http headers

<?php
$extraHeaders = [
  'X-FORWARDED-FOR' => '94.228.178.246'
];

$objects = [/* objects */];
$res = $index->addObjects($objects, 'objectID', $extraHeaders);
objects = []

extra_headers = {
  'X-FORWARDED-FOR': '94.228.178.246'
}

res = index.add_objects(objects, extra_headers)
client.setExtraHeader('X-FORWARDED-FOR', '94.228.178.246');
index.addObjects([{
  objectID: 'myId',
  firstname: 'Jimmie',
  lastname: 'Barninger'
}], function(err, content) {
  console.log('objectID=' + content.objectID);
});
request_options = RequestOptions({'algoliaUserID': 'user123'})
res = index.add_object(
    {"firstname": "Jimmie", "lastname": "Barninger"},
    "myID",
    request_options=request_options
)
let requestOptions = RequestOptions()
requestOptions.headers["X-Algolia-User-ID"] = "user123"

let obj1 = ["firstname": "Jimmie", "lastname": "Barninger"]
let obj2 = ["firstname": "Warren", "lastname": "Speach"]

index.addObjects(
  [obj1, obj2],
  requestOptions: requestOptions,
  completionHandler: { (content, error) -> Void in
    if error == nil {
      print("Object IDs: \(content!)")
    }
  }
)
List<JSONObject> array = new ArrayList<JSONObject>();

array.add(
  new JSONObject().put("firstname", "Jimmie").put("lastname", "Barninger")
);
array.add(
  new JSONObject().put("firstname", "Warren").put("lastname", "Speach")
);

index.addObjectsAsync(
  new JSONArray(array),
  new RequestOptions().setHeader("X-Algolia-User-ID", "94.228.178.246"),
  null
);
RequestOptions requestOptions = new RequestOptions();
requestOptions.AddExtraHeader("X-Algolia-User-ID", "user123");

List<JObject> objs = new List<JObject>();

objs.Add(JObject.Parse(
  @"{""objectID"":""myID1"",
    ""firstname"":""Jimmie"",
    ""lastname"":""Barninger""}"
));

objs.Add(JObject.Parse(
  @"{""objectID"":""myID2"",
    ""firstname"":""Warren"",
    ""lastname"":""Speach""}"
));

var res = index.AddObjects(objs, requestOptions);

// Asynchronous
// var res = await index.AddObjectsAsync(objs, requestOptions);
// Sync & Async version
index.addObjects(
  Arrays.asList(
    new Contact().setFirstName("Jimmie").setLastName("Barninger"),
    new Contact().setFirstName("Warren").setLastName("Speach")
  ),
  new RequestOptions().addExtraHeader("X-Algolia-User-ID", "user123")
);
objects := []algoliasearch.Object{
  {"firstname": "Jimmie", "lastname": "Barninger"},
  {"firstname": "Ray", "lastname": "Charles"},
}

opts := &algoliasearch.RequestOptions{
  ExtraHeaders: map[string]string{
    "X-Algolia-User-ID": "userID2",
  },
}

res, err := index.AddObjectsWithRequestOptions(objects, opts)
client.execute {
  index into "index1" objects Seq(
    Contact("Jimmie", "Barninger"),
    Contact("Warren", "Speach")
  ) options RequestOptions(
    extraHeaders = Some(Map("X-Algolia-User-ID" => "user123"))
  )
}

Parameters

objects
type: list of object
Required
requestOptions
type: key value mapping
Optional
default: No request options

A list of request options to send along with the query.


objects ➔ object

The object is schemaless so it can be any set of key/value pairs.

Only the objectID has special considerations.

The value you provide for objectIDs can be an integer or a string but will in any case converted to string by the engine.

You can create the ID yourself or algolia will generate one. Which means that you are not required to send an objectID.

  1. If you send an objectID:
    • If the objectID does not already exist in the index, the record will be created
    • If the objectID already exists, the record will be replaced (same functionality as updating object)
  2. If you do not send an objectID:
    • Algolia will automatically create an objectID that you will be able to access in the response.

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

{
  "objectIDs": [
    "myObjectID1",
    "myObjectID2"
  ],
  "taskID": 678,
}
objectIDs
list

List of objectIDs of the inserted objects.

taskID
integer

The taskID used with the waitTask method.

Php small white
PHP
Ruby small white
Ruby
Javascript small white
JavaScript
Python small white
Python
Swift small white
iOS
Android small white
Android
Csharp small white
C#
Java small white
Java
Go small white
Go
Scala small white
Scala
Php small white
PHP
Ruby small white
Ruby
Javascript small white
JavaScript
Python small white
Python
Swift small white
iOS
Android small white
Android
Csharp small white
C#
Java small white
Java
Go small white
Go
Scala small white
Scala