[英]Integrate original elasticsearch-php library into Laravel 4.2?
I am first day to Laravel and i need to intgerate original elasticsearch-php library. 我是Laravel的第一天,我需要整合原始的elasticsearch-php库。 https://github.com/elasticsearch/elasticsearch-php
https://github.com/elasticsearch/elasticsearch-php
I've downloaded it via composer but don't know how to make it work the right way with Laravel. 我已经通过作曲家下载了它,但不知道如何使它与Laravel一起正常工作。
Basically i want to use it that way: 基本上我想以这种方式使用它:
$client = new ElasticSearch\\Client(); $ client =新的ElasticSearch \\ Client();
Please help. 请帮忙。
Add the following lines to your composer.json
composer.json
添加到您的composer.json
"shift31/laravel-elasticsearch": "1.0.*@dev"
"elasticsearch/elasticsearch": "~1.0"
"shift31/laravel-elasticsearch": "1.0.*@dev"
"elasticsearch/elasticsearch": "~1.0"
"shift31/laravel-elasticsearch": "1.0.*@dev"
"elasticsearch/elasticsearch": "~1.0"
Follow the rest of the install instructions at https://github.com/shift31/laravel-elasticsearch#usage 请按照https://github.com/shift31/laravel-elasticsearch#usage中的其余安装说明进行操作
For good measure, i am providing some starter boilerplate code for you to save your data using that library. 为了提供良好的效果,我提供了一些入门示例代码供您使用该库保存数据。
FYI, I use my environment to differentiate between indexes (production or testbed). 仅供参考,我使用我的环境来区分索引(生产或测试平台)。 You may use other methods, such as a config.php value.
您可以使用其他方法,例如config.php值。
Create mapping 创建映射
$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);
Insert document 插入文件
$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);
}
Update Document 更新文件
$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);
}
Delete Document 删除文件
$params = array();
$params['index'] = \App::environment();
$params['type'] = 'car';
$params['id'] = 1;
$deleteResult = $client->delete($params);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.