1、es安装
先安装 elasticsearch-php (这个对php的版本有要求,推荐composer安装),对应版本传送,安装完成后,搭建JAVA 环境(网上搜索),环境配置完成后(需要添加几个系统配置的环境变量),下载 对应 php版本 的elastic 对应版本传送, elastic 下载传送,安装说明在下载的页面有相关文档,推荐下载对应的分词【ik版本传送】,安装比较简单,解压缩,复制/剪切到 elastic 的 plugins 文件夹下(没有就新建),重启elastic,
2、logstash 安装
这个需要的是ruby 环境,所以搭建ruby环境,然后下载安装
推荐参考范例,按照这个操作很快就可以完成,是我找到最好的教程,
3、php 实现搜索
由于数据还没有处理完成,就以上述案例的数据为准,进行操作
1)全部搜索没有 等价于 MySQL【select * 】
public function search($key){
$hosts = [
// '192.168.1.1:9200', // IP + Port
// '192.168.1.2', // Just IP
// 'mydomain.server.com:9201', // Domain + Port
// 'mydomain2.server.com', // Just Domain
// 'https://localhost', // SSL to localhost
// 'https://192.168.1.3:9200', // SSL to IP + Port
'127.0.0.1:9200',
];
$client = ClientBuilder::create() // Instantiate a new ClientBuilder
->setHosts($hosts) // Set the hosts
// ->setRetries(2) //
->build(); // Build the client object
$params = [
'index' => 'xc_course',
'type' => 'doc',
// 'body' => ['name' => 'js']
];
$response = $client->search($params);
print_r($response);
}
2)根据搜索词进行搜索
public function search($key){
// $key = $request->get('key');
$hosts = [
// '192.168.1.1:9200', // IP + Port
// '192.168.1.2', // Just IP
// 'mydomain.server.com:9201', // Domain + Port
// 'mydomain2.server.com', // Just Domain
// 'https://localhost', // SSL to localhost
// 'https://192.168.1.3:9200', // SSL to IP + Port
'127.0.0.1:9200',
];
$client = ClientBuilder::create() // Instantiate a new ClientBuilder
->setHosts($hosts) // Set the hosts
// ->setRetries(2) //
->build(); // Build the client object
$params = [
'index' => 'xc_course',
// 'type' => 'my_type',
'body' => [
'query' => [
'bool' => [
'should' => [
[ 'match' => [ 'name' => '实战' ] ],
// [ 'match' => [ 'description' => '课程' ] ],
[ 'match' => [ 'description' => '实战' ] ],
]
]
],
]
];
// $response = $client->index($params);
$response = $client->search($params);
print_r($response);
}
3) 搜索结果高亮显示
public function search($key){
// $key = $request->get('key');
$hosts = [
// '192.168.1.1:9200', // IP + Port
// '192.168.1.2', // Just IP
// 'mydomain.server.com:9201', // Domain + Port
// 'mydomain2.server.com', // Just Domain
// 'https://localhost', // SSL to localhost
// 'https://192.168.1.3:9200', // SSL to IP + Port
'127.0.0.1:9200',
];
$client = ClientBuilder::create() // Instantiate a new ClientBuilder
->setHosts($hosts) // Set the hosts
// ->setRetries(2) //
->build(); // Build the client object
$params = [
'index' => 'xc_course',
// 'type' => 'my_type',
'body' => [
'query' => [
'bool' => [
'should' => [
[ 'match' => [ 'name' => '实战' ] ],
// [ 'match' => [ 'description' => '课程' ] ],
[ 'match' => [ 'description' => '实战' ] ],
]
]
],
'highlight' => [
'pre_tags' => ["<em>"], // 高亮可以自己修改
'post_tags' => ["</em>"],
'fields' => [
"name" => new \stdClass(),
"description" => new \stdClass(),
]
]
]
];
$response = $client->search($params);
prt($response);
}
当然既然用了es不应该只是一个数据表,多表处理传送门
来源:oschina
链接:https://my.oschina.net/u/3268486/blog/4364740