使用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']);
}
}