首頁  >  文章  >  後端開發  >  用elasticsearch-php laravel為什麼不能回傳高亮資料?

用elasticsearch-php laravel為什麼不能回傳高亮資料?

WBOY
WBOY原創
2016-08-04 09:20:13974瀏覽

<code>    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 "<pre class="brush:php;toolbar:false">";
        var_dump($response);
        echo "
"; } }

查詢結果都能返回,就是無highlight數據,求朋友幫忙! ! !

回覆內容:

<code>    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 "<pre class="brush:php;toolbar:false">";
        var_dump($response);
        echo "
"; } }

查詢結果都能返回,就是無highlight數據,求朋友幫忙! ! !

暫時沒做過搜尋服務,感覺高亮的資料應該在控制器裡面處理的吧。

陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn