When using the $client->update($params); method to update a nested type field, rather than appending to it, it's replacing whatever is in there.
In the API for the application I'm working on, I have:
$data = [
'note_link_id' => $link_id,
'user_id' => $this->flexi_auth->get_user_id(),
'creation' => date('Y-m-d\TH:i:s'),
'modification' => date('Y-m-d\TH:i:s'),
'from_asset' => $from,
'to_asset' => $to
];
$parameters = array(
'index' => "asset",
'type' => $this->getAssetType( $from_note ),
'id' => $from_note->note_id
);
$this->elasticsearch_model->addAssetLinkData( $data, $parameters );
... which, when sent to the model:
public function addAssetLinkData ($data, $params) {
try {
if (!isset($params['index']) || !isset($params['type']))
die ("Unable to add Elastic index for an Asset Link. Either index or type is not defined.");
$client = $this->_client;
$params['body']['doc']['links'] = [
$data
];
$responses = $client->update($params);
} catch (Exception $e) {
//die($e->getMessage());
}
}
... replaces any existing objects instead of appending to the links nested type.
I'm using much the same code, and the same variables, elsewhere in the application without problems.
I've tried innumerable variations of code in the method, to coax the array into appending the objects, but nothing has worked.
I've also tried a trimmed down simplified version, and I get the same problem.
So either I'm doing something wrong, or the default behaviour of the PHP method is to replace the objects in the nested type array, rather than appending to it.
Mac OS X El Capitan 10.11.6
5.6.30
According to CHANGELOG.md 5.2.0
5.3.0
It has to be said that the API documentation isn't optimal.
In the end, I found the answer almost by accident — I was _guessing_ what JavaScript function would work within Painless, got an error, Googled it, and found a StackOverflow article that answered the question that's alluded and frustrated me for almost two weeks.
So, in JSON:
{
"script": {
"lang": "painless",
"inline": "ctx._source.links.add(params.data)",
"params": {
"data": {
"note_link_id": 1,
"user_id": 11,
"creation": "2017-06-0712:12:12",
"modification": "2017-06-07T12:12:12",
"from_asset": 2,
"to_asset": 3
}
}
}
}
... and the equivalent in PHP:
$data = [
'script' => [
'lang' => "painless",
'inline' => "ctx._source.links.add(params.data)",
'params' => [
'data' => [
'note_link_id' => $link_id,
'user_id' => $this->flexi_auth->get_user_id(),
'creation' => date('Y-m-d\TH:i:s'),
'modification' => date('Y-m-d\TH:i:s'),
'from_asset' => $from,
'to_asset' => $to,
'comment' => strip_tags( $comment )
]
]
]
];
I _urge_ someone to amend either the Update, or Nested datatype API pages with the inline script example.
I've tried with similar queries, but received
{"type":"illegal_argument_exception",
"reason":"dynamic method [java.util.LinkedHashMap, add/1] not found"}
any ideas why this is happening?
Most helpful comment
It has to be said that the API documentation isn't optimal.
In the end, I found the answer almost by accident — I was _guessing_ what JavaScript function would work within Painless, got an error, Googled it, and found a StackOverflow article that answered the question that's alluded and frustrated me for almost two weeks.
So, in JSON:
... and the equivalent in PHP:
I _urge_ someone to amend either the Update, or Nested datatype API pages with the inline script example.