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 有没有整苹果智能分拣线上图像数据
  • ¥20 有没有人会这个东西的
  • ¥15 cfx考虑调整“enforce system memory limit”参数的设置
  • ¥30 航迹分离,航迹增强,误差分析
  • ¥15 Chrome Manifest扩展引用Ajax-hook库拦截请求失败
  • ¥15 用Ros中的Topic通讯方式控制小乌龟的速度,走矩形;编写订阅器代码
  • ¥15 LLM accuracy检测
  • ¥15 pycharm添加远程解释器报错
  • ¥15 如何让子窗口鼠标滚动独立,不要传递消息给主窗口
  • ¥15 如何能达到用ping0.cc检测成这样?如图