栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 前沿技术 > 大数据 > 大数据系统

thinkphp5.0 ElasticSearch 高亮搜索

thinkphp5.0 ElasticSearch 高亮搜索

如何引入

引入步骤


public function getList($index, $keywords, $limit = 10, $page = 1, $sort_field='sort',$sort_value='desc')
{  
$offset = ((int)$page - 1) * (int)$limit;

    $params = [
        'index' => $this->_index_prefix . $index,
        'body' => [
            'query' => [
                'match' => [
                    'goods_name' => trim($keywords)
                ],
            ],
//highlight 声明高亮
//pre_tags 字符前
//post_tags 字符后
//fields  高亮字段  参数为数组 KEY 为字段名 VALUE  必须为obj 否则不生效

            'highlight' => [
                'pre_tags' => "

",
                'post_tags' => "

",
                'fields' => [
                    'goods_name' => (object)[]
                ]
            ],
            'sort' => [[$sort_field => ['order' => $sort_value]]],
            'from' => $offset,
            'size' => $limit
        ]
    ];

    if (empty($keywords)) {
        //如果没有搜索关键字 就删除query
        unset($params['body']['query']);
        unset($params['body']['highlight']);
    }
    $response = $this->_client->search($params);

    //$response['hits']['hits']是具体的数据列表
    return $response;
}


public function getNewGoodsEsList($param)
{
    Loader::import('libElasticSearch', EXTEND_PATH);
    $es = new ElasticSearch();
    $result = $es->getList('goods',$param['search'], $param['list_rows'], $param['page'],$param['sort_field'],$param['sort_value']);
    $resultData = [];
    foreach ($result['hits']['hits'] as $key=>$value){
        $goods_name = $value['_source']['goods_name'];
        if(isset($value['highlight']['goods_name'][0])){
            $goods_name = $value['highlight']['goods_name'][0];
        }
        $resultData[] = [
            'id'=>$value['_source']['id'],
            'goods_name'=>$goods_name,
            'cover_url'=>'http://' . $_SERVER['SERVER_NAME'] . get_thumb_picture_url($value['_source']['cover_id'], 'medium'),
            'goods_price'=>$value['_source']['goods_price'],
            'goods_original_price'=>$value['_source']['goods_original_price'],
            'sales'=>$value['_source']['sales'],
        ];
    }
    $data = [
        'total'=>$result['hits']['total']['value'],//总数
        'page'=>$param['page'],//当前页
        'last_page'=>ceil($result['hits']['total']['value']/$param['list_rows']),//总页数
        'data'=>$resultData
    ];
    return $data;
}

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/282821.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号