您的位置:首页 > 编程语言 > PHP开发

laravel如何查询elasticsearch的数据

2016-01-14 17:50 841 查看
使用elasticsearch-php,地址:https://github.com/elastic/elasticsearch-php

在laravel目录下的composer.json中,require里加入"elasticsearch/elasticsearch": "~2.0",例如:

"require": {

        "php": ">=5.5.9",

        "laravel/framework": "5.2.*",
        "elasticsearch/elasticsearch": "~2.0"

    }

然后用composer安装即可,安装后vendor目录下就有了elasticsearch目录。默认操作的是本地的elasticsearch,既localhost:9200,如果需要访问远程服务器上的es,那么在ClientBuilder.php里修改getDefaultHost方法里返回的host地址即可。例如:

    private function getDefaultHost()

    {

        //return ['localhost:9200'];    //this is default

        return ['42.96.163.231:9200'];      //modify by author

    }

在程序中的调用:

<?php

namespace App\Http\Controllers;

use App\Http\Controllers\Controller;

use Elasticsearch\ClientBuilder;

class ElasticSearch extends Controller

{

    public function getData()

    {

        $client = ClientBuilder::create()->build();

        $params = [

            'index' => 'elasticsearch',

            'type' => 'goods',

            'body' => [

                'query' => [

                    'match' => [

                        'title' => '车'

                    ]

                ]

            ]

        ];

        $response = $client->search($params);

        print_r($response['hits']['hits']);

    }

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  laravel elasticsearc