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 想问一下树莓派接上显示屏后出现如图所示画面,是什么问题导致的
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line
  • ¥500 火焰左右视图、视差(基于双目相机)
  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号