我想将此表添加到algolia:
+-------+------------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-------+------------------+------+-----+---------+----------------+
| data | json | NO | | NULL | |
| id | int(10) unsigned | NO | PRI | NULL | auto_increment |
+-------+------------------+------+-----+---------+----------------+
此表中只有一行,数据内容为:
[
{
"id":"7cc481e3eab24b4ef9ccc945c00f7784",
"desc":"shirts desc",
"name":"Shirts",
"slug":"shirts",
"image":"7cc481e3eab24b4ef9ccc945c00f7784.jpeg",
"categories":[
]
},
{
"id":"67d188521fa8531dd3e2044814cb942342",
"desc":"some desc",
"name":"Jackets",
"slug":"jackets",
"image":"67d188521fa8531dd3e2044814cb942342.jpeg",
"categories":[
{
"id":"671a83b87369ee6773774c0d6d4455e2",
"desc":"sealed desc",
"name":"Sealedq",
"slug":"sealedq",
"image":""
}
]
},
{
"id":"ad03b6be35714f7bb7f2ecc82d512c79",
"desc":"some desc",
"name":"Trousers",
"slug":"trousers",
"image":"ad03b6be35714f7bb7f2ecc82d512c79.jpg"
},
{
"id":"ea265cbc18db7ad6b3a3013af3070890",
"desc":"some desc",
"name":"Sweaters",
"slug":"sweaters",
"image":"ea265cbc18db7ad6b3a3013af3070890.jpeg"
}
]
这些都适用于Laravel 5.2, 当我将这些数据发送给algolia时,它只会创建一条记录并将objectID设置为table的id。
如何只将数据字段发送到algolia并将数据字段的id设置为objectID?
我使用此代码进行测试,它对我有用,但我认为这不是正确的方法,也许我应该使用同义词。
$client = new AlgoliaSearch\Client("AppID", "AdminKey");
$index = $client->initIndex('catalog');
$results = Model::select('data')->first();
$results = json_decode($results->data, true);
if ($results)
{
$batch = array();
// iterate over results and send them by batch of 10000 elements
foreach ($results as $row)
{
// select the identifier of this row
$row['objectID'] = $row['id'];
array_push($batch, $row);
}
return $index->saveObjects($batch);
}
答案 0 :(得分:4)
如何只将数据字段发送到algolia并将数据字段的id设置为objectID?
我使用此代码进行测试,它对我有用,但我认为这不是正确的方法,也许我应该使用同义词。
解析您身边的数据内容并将其添加到对象中是正确的方法。我不知道您正在使用的PHP框架,但您可以执行以下操作:
$batch = array();
$rows = Model::select('*')->all();
foreach ($rows as $row) {
$row['objectID'] = $row['id']
$row['data'] = json_decode($row['data'], true);
array_push($batch, $row);
}
$index->saveObjects($batch);
同义词用于其他内容。您可以阅读更多here