ES 多条件高亮搜索

编程入门 行业动态 更新时间:2024-10-03 23:31:30

ES <a href=https://www.elefans.com/category/jswz/34/1769707.html style=多条件高亮搜索"/>

ES 多条件高亮搜索

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;}

更多推荐

ES 多条件高亮搜索

本文发布于:2024-02-28 12:59:10,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1769946.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:多条   ES   高亮

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!