我是第一天到Laravel,我需要放大原来的elasticsearch库。https://github.com/elasticsearch/elasticsearch-php
我已经下载了它的作曲家,但不知道如何使它正确的方式与拉拉维尔。
基本上,我想用这样的方式:
$client =新的ElasticSearch\Client();
请帮帮忙。
发布于 2015-01-19 04:09:02
将下列行添加到您的composer.json
"shift31/laravel-elasticsearch": "1.0.*@dev" "elasticsearch/elasticsearch": "~1.0"
按照https://github.com/shift31/laravel-elasticsearch#usage的其余安装说明执行
为了更好地衡量,我提供了一些初学者样板代码,供您使用该库保存数据。
FYI,我使用我的环境来区分索引(生产还是测试)。您可以使用其他方法,例如config.php值。
创建映射
$params = array();
$params['index'] = \App::environment();
//params' type and array body's 2nd element should be of the same name.
$params['type'] = 'car';
$params['body']['car'] = ['properties' =>
[
'id' => [
'type' => 'long'
],
'name' => [
'type' => 'string'
],
'engine' => [
'type' => 'string'
]
];
$client = new Elasticsearch\Client();
$client->indices()->putMapping($params);插入文档
$params = array();
$params['index'] = \App::environment();
$params['type'] = 'car';
$car = \CarModel::find($data['id']);
if(count($car))
{
$params['id'] = $car->id;
//Elasticsearch doesn't accept Carbon's default string value. Use the below function to convert it in an acceptable format.
$params['timestamp'] = $car->updated_at->toIso8601String();
// toArray will give you the attributes of the model AND its relations. This is the bothersome part where you will get more data than what you need.
$params['body']['car'] = $car->toArray();
\Es::index($params);
}更新文档
$params = array();
$params['index'] = \App::environment();
$params['type'] = 'car';
$car = \CarModel::find($data['id']);
if(count($car))
{
$params['id'] = $car->id;
$params['body']['doc']['car'] = $car->toArray();
\Es::update($params);
}删除文档
$params = array();
$params['index'] = \App::environment();
$params['type'] = 'car';
$params['id'] = 1;
$deleteResult = $client->delete($params);https://stackoverflow.com/questions/28010386
复制相似问题