当前位置:Gxlcms > PHP教程 > 用elasticsearch-phplaravel为什么不能返回高亮数据?

用elasticsearch-phplaravel为什么不能返回高亮数据?

时间:2021-07-01 10:21:17 帮助过:47人阅读

    namespace App\Http\Controllers\Search;
    use Illuminate\Http\Request;
    use App\Http\Requests;
    use App\Http\Controllers\Controller;
    use Elasticsearch\Client;

class Index extends Controller
{
    protected $client;
    public function __construct(Client $client)
    {
        $this->client = $client;
    }
    public function search_test(Request $request,$filter='list'){
        $word = trim($request->input('word'));

        $s=is_null($request->input('s'))?10:trim($request->input('s'));//一页多少条
        $f=is_null($request->input('f'))?1:trim($request->input('f'));//当前页数
        $fr=($f-1)*$s;//当前页从第一条记录开始
        $params=[
            'index' => 's_index',
            'type' => 's_type',
            'body' => [
                'query' => [
                    'bool' => [
                        'should' => [
                            [ 'match' => [ 'title' => $word ] ],
                            [ 'match' => [ 'description' => $word ] ],
                        ]
                    ]
                ]
                ,'from'=>$fr, 'size'=>$s
                ,'highlight'=>[
                    'fields'=>[
                        'title'=>[]
                    ]
                ]
            ]
        ];

        $response = $this->client->search($params);

        echo "
";
        var_dump($response);
        echo "
"; } }

查询结果都能返回,就是无highlight数据,求朋友帮忙!!!

回复内容:

    namespace App\Http\Controllers\Search;
    use Illuminate\Http\Request;
    use App\Http\Requests;
    use App\Http\Controllers\Controller;
    use Elasticsearch\Client;

class Index extends Controller
{
    protected $client;
    public function __construct(Client $client)
    {
        $this->client = $client;
    }
    public function search_test(Request $request,$filter='list'){
        $word = trim($request->input('word'));

        $s=is_null($request->input('s'))?10:trim($request->input('s'));//一页多少条
        $f=is_null($request->input('f'))?1:trim($request->input('f'));//当前页数
        $fr=($f-1)*$s;//当前页从第一条记录开始
        $params=[
            'index' => 's_index',
            'type' => 's_type',
            'body' => [
                'query' => [
                    'bool' => [
                        'should' => [
                            [ 'match' => [ 'title' => $word ] ],
                            [ 'match' => [ 'description' => $word ] ],
                        ]
                    ]
                ]
                ,'from'=>$fr, 'size'=>$s
                ,'highlight'=>[
                    'fields'=>[
                        'title'=>[]
                    ]
                ]
            ]
        ];

        $response = $this->client->search($params);

        echo "
";
        var_dump($response);
        echo "
"; } }

查询结果都能返回,就是无highlight数据,求朋友帮忙!!!

暂时没做过搜索服务,感觉高亮的数据应该在控制器里面处理的吧。

人气教程排行