douguachi0056 2015-01-18 13:48
浏览 77
已采纳

将原始的elasticsearch-php库集成到Laravel 4.2中?

I am first day to Laravel and i need to intgerate original elasticsearch-php library. 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.

Basically i want to use it that way:

$client = new ElasticSearch\Client();

Please help.

  • 写回答

1条回答 默认 最新

  • dongzan0108 2015-01-19 04:09
    关注

    Add the following lines to your composer.json

    "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

    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.

    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);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 对于相关问题的求解与代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料
  • ¥15 使用R语言marginaleffects包进行边际效应图绘制