1、创建索引
/**
* 创建索引,简单点就是你要搜索的库
*/
public function ElasticsearchIndex()
{
//这里的实例模块是线上阿里云的ES 线下你自己需要改一下
$client = ClientBuilder::create()->setHosts([
[
'host' => '',
'port' => '9200',
'scheme' => 'http',
'user' => '',
'pass' => ''
]
])->setConnectionPool('\Elasticsearch\ConnectionPool\SimpleConnectionPool', [])
->setRetries(10)->build();
//本地调用实例化
// $client = ClientBuilder::create()->setHosts(['127.0.0.1:9200'])->build();
$params = [
'index' => 'user',//类似于库名
'body' => [
//这里是权重值 你自己根据需求加入
'settings' => [
'number_of_shards' => 3,
'number_of_replicas' => 2
],
'mappings' => [
'_source' => [
'enabled' => true
],
'properties' => [
//之后可以进行搜索的字段名
'name' => [
'type' => 'text', //这是一种搜索的格式 自己可以了解一下参数
"analyzer" => "ik_max_word",
"search_analyzer" => "ik_max_word"
],
//字段名
'id' => [
'type' => 'text',
"analyzer" => "ik_max_word",
"search_analyzer" => "ik_max_word"
]
]
]
]
];
//执行创建
$r = $client->indices()->create($params);
print_r($r);
}
2、添加数据
/**
* 添加数据
* @return string
*/
public function createData()
{
//线上阿里云ES
$client = ClientBuilder::create()->setHosts([
[
'host' => '',
'port' => '9200',
'scheme' => 'http',
'user' => '',
'pass' => ''
]
])->setConnectionPool('\Elasticsearch\ConnectionPool\SimpleConnectionPool', [])
->setRetries(10)->build();
//本地ES
//$client = ClientBuilder::create()->setHosts(['127.0.0.1:9200'])->build();
//实例化你的数据库,将数据库循环加入你创建的索引(也就是库)
$data = User::all()->toArray();
foreach ($data as $k => $v) {
$params = [
'index' => 'user',//你上面创建的库
'type' => '_doc',//表(额外需要注意的,这里是固定的写法)
'id' => $v['id'],//主键
'body' => $v//数据
];
$r = $client->index($params);
}
return 'success';
}
3、多条件搜索、高亮展示
/**
* 多条件搜索、高亮搜索
* @param Request $request
* @return array
*/
public function searchIndex(Request $request)
{
$word = $request->get('word');//接收关键字
$client = ClientBuilder::create()->setHosts([
[
'host' => '',
'port' => '9200',
'scheme' => 'http',
'user' => '',
'pass' => ''
]
])->setConnectionPool('\Elasticsearch\ConnectionPool\SimpleConnectionPool', [])
->setRetries(10)->build();
//本地ES
// $client = ClientBuilder::create()->setHosts(['127.0.0.1:9200'])->build();
$params = [
'index' => 'user',//索引(跟上面你填写的数据库一样)
//'type' => '_doc',
'body' => [
'query' => [
//bool 关键字: 用来组合多个条件实现复杂查询
'bool' => [
//bool 查询如果不加 should 就是多个字段必须全部填写 才能查询 should: 相当于|| 成立一个就行
"should" => [
//几个条件就写几个match
['match' => ['name' => $word]],
['match' => ['id' => $word]],
]
]
]
],
];
//进行判断 该字段是否为空 不是为空就去搜索该字段
if (!empty($word)) {
$params['body']['query']['bool']['should'][] = [
'match' => [
'name' => $word
]
];
}
//进行判断 该字段是否为空 不是为空就去搜索该字段
if (!empty($word)) {
$params['body']['query']['bool']['should'][] = [
'match' => [
'id' => $word
]
];
}
//这里是高亮搜索 可以安装一个高亮包 composer require nunomaduro/collision
$params['body']['highlight'] = [
"pre_tags" => ["<span style='color:red'>"],
"post_tags" => ["</span>"],
];
//多条件高亮字段 加上对应的数据字段
$params['body']['highlight']['fields'] = [
'name' => new \stdClass(),
'id' => new \stdClass()
];
//惊醒搜索
$res = $client->search($params);
//循环出来 自己打印一下 $res 在打印一下 $v 看一下收据结构
foreach ($res['hits']['hits'] as $k => $v) {
//判断 对应的字段是否为空 如果不为空 就进行一个赋值 然后进行搜索
if (!empty($v['highlight']['name'][0])) {
$res['hits']['hits'][$k]['_source']['name'] = $v['highlight']['name'][0];
} else if (!empty($v['highlight']['id'][0])) {
$res['hits']['hits'][$k]['_source']['id'] = $v['highlight']['id'][0];
}
}
// 这里 array_column 取出数组的某一列 数据都在 _source 里面
$data = array_column($res['hits']['hits'], '_source');
$arr['data'] = $data;//数据
return $arr;
}