Heim  >  Artikel  >  Backend-Entwicklung  >  用elasticsearch-php laravel为什么不能返回高亮数据?

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

WBOY
WBOYOriginal
2016-08-04 09:20:13977Durchsuche

<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数据,求朋友帮忙!!!

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

Stellungnahme:
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn