将原来的elasticsearch-php库集成到Laravel 4.2?
Integrate original elasticsearch-php library into Laravel 4.2?
我是 Laravel 的第一天,我需要整合原始的 elasticsearch-php 库。
https://github.com/elasticsearch/elasticsearch-php
我已经通过 composer 下载了它,但不知道如何使用 Laravel 使其正常工作。
基本上我想这样使用它:
$client = new ElasticSearch\Client();
请帮忙。
将以下行添加到您的 composer.json
"shift31/laravel-elasticsearch": "1.0.*@dev"
"elasticsearch/elasticsearch": "~1.0"
按照 https://github.com/shift31/laravel-elasticsearch#usage
中的其余安装说明进行操作
为了更好的衡量,我提供了一些入门样板代码供您使用该库保存数据。
仅供参考,我使用我的环境来区分索引(生产或测试平台)。您可以使用其他方法,例如 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);
我是 Laravel 的第一天,我需要整合原始的 elasticsearch-php 库。 https://github.com/elasticsearch/elasticsearch-php
我已经通过 composer 下载了它,但不知道如何使用 Laravel 使其正常工作。
基本上我想这样使用它:
$client = new ElasticSearch\Client();
请帮忙。
将以下行添加到您的 composer.json
"shift31/laravel-elasticsearch": "1.0.*@dev"
"elasticsearch/elasticsearch": "~1.0"
按照 https://github.com/shift31/laravel-elasticsearch#usage
中的其余安装说明进行操作为了更好的衡量,我提供了一些入门样板代码供您使用该库保存数据。
仅供参考,我使用我的环境来区分索引(生产或测试平台)。您可以使用其他方法,例如 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);