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資料,求朋友幫忙!!!
暫時沒做過搜尋服務,感覺高亮的資料應該在控制器裡面處理的吧。