I read tutorial about indexing documents in Elasticsearch. There is example with bulk indexing. I have question, is it correct when is created array with two key for one item in the loop:
for($i = 0; $i < 100; $i++) {
$params['body'][] = array(
'index' => array(
'_id' => $i
)
);
$params['body'][] = array(
'my_field' => 'my_value',
'second_field' => 'some more values'
);
}
Why there are two initizalizations of array $params['body'][]
in loop?
Must be index setting by the same key like as my_field
?
I mean one case, when all information about index is added in array by one key(index):
$params['body'][] = array(
'index' => array(
'_id' => $i
),
'my_field' => 'my_value',
'second_field' => 'some more values'
);
Also after search query I get error:
Message: Illegal string offset 'match' on line where is:
$query['match']['name'] = $query;
where $query
is string.
I suppose that this error with problem in creation of index, therefore I have begun with this.
My code which adds document in index:
private function addDocument($data = array(), $type)
{
if (!empty($data)) {
foreach ($data as $key => $val) {
$params['body'][] = array(
'index' => array(
'_id' => $key,
'_type' => 'profiles',
'_index' => $this->_typeIndex($type)
)
);
$params['body'][] = (array)$val;
}
$this->client->bulk($params);
}
}
Is it right? Because in search I get error, that described here