Single document indexingedit

When indexing a document, you can either provide an ID or let elasticsearch generate one for you.


Providing an ID value.

$params = array();
$params['body']  = array('testField' => 'abc');

$params['index'] = 'my_index';
$params['type']  = 'my_type';
$params['id']    = 'my_id';

// Document will be indexed to my_index/my_type/my_id
$ret = $client->index($params);


Omitting an ID value.

$params = array();
$params['body']  = array('testField' => 'abc');

$params['index'] = 'my_index';
$params['type']  = 'my_type';

// Document will be indexed to my_index/my_type/<autogenerated_id>
$ret = $client->index($params);


Like most of the other APIs, there are a number of other parameters that can be specified. They are specified in the parameter array just like index or type. For example, let’s set the routing and timestamp of this new document:

Additional parameters.

$params = array();
$params['body']  = array('testField' => 'xyz');

$params['index']     = 'my_index';
$params['type']      = 'my_type';
$params['routing']   = 'company_xyz';
$params['timestamp'] = strtotime("-1d");

$ret = $client->index($params);