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

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

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

  1. <code> namespace App\Http\Controllers\Search;
  2. use Illuminate\Http\Request;
  3. use App\Http\Requests;
  4. use App\Http\Controllers\Controller;
  5. use Elasticsearch\Client;
  6. class Index extends Controller
  7. {
  8. protected $client;
  9. public function __construct(Client $client)
  10. {
  11. $this->client = $client;
  12. }
  13. public function search_test(Request $request,$filter='list'){
  14. $word = trim($request->input('word'));
  15. $s=is_null($request->input('s'))?10:trim($request->input('s'));//一页多少条
  16. $f=is_null($request->input('f'))?1:trim($request->input('f'));//当前页数
  17. $fr=($f-1)*$s;//当前页从第一条记录开始
  18. $params=[
  19. 'index' => 's_index',
  20. 'type' => 's_type',
  21. 'body' => [
  22. 'query' => [
  23. 'bool' => [
  24. 'should' => [
  25. [ 'match' => [ 'title' => $word ] ],
  26. [ 'match' => [ 'description' => $word ] ],
  27. ]
  28. ]
  29. ]
  30. ,'from'=>$fr, 'size'=>$s
  31. ,'highlight'=>[
  32. 'fields'=>[
  33. 'title'=>[]
  34. ]
  35. ]
  36. ]
  37. ];
  38. $response = $this->client->search($params);
  39. echo "<pre class="brush:php;toolbar:false layui-box layui-code-view layui-code-notepad"><ol class="layui-code-ol"><li>";</li><li> var_dump($response);</li><li> echo "</li></ol></pre>";
  40. }
  41. } </code>

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

回复内容:

  1. <code> namespace App\Http\Controllers\Search;
  2. use Illuminate\Http\Request;
  3. use App\Http\Requests;
  4. use App\Http\Controllers\Controller;
  5. use Elasticsearch\Client;
  6. class Index extends Controller
  7. {
  8. protected $client;
  9. public function __construct(Client $client)
  10. {
  11. $this->client = $client;
  12. }
  13. public function search_test(Request $request,$filter='list'){
  14. $word = trim($request->input('word'));
  15. $s=is_null($request->input('s'))?10:trim($request->input('s'));//一页多少条
  16. $f=is_null($request->input('f'))?1:trim($request->input('f'));//当前页数
  17. $fr=($f-1)*$s;//当前页从第一条记录开始
  18. $params=[
  19. 'index' => 's_index',
  20. 'type' => 's_type',
  21. 'body' => [
  22. 'query' => [
  23. 'bool' => [
  24. 'should' => [
  25. [ 'match' => [ 'title' => $word ] ],
  26. [ 'match' => [ 'description' => $word ] ],
  27. ]
  28. ]
  29. ]
  30. ,'from'=>$fr, 'size'=>$s
  31. ,'highlight'=>[
  32. 'fields'=>[
  33. 'title'=>[]
  34. ]
  35. ]
  36. ]
  37. ];
  38. $response = $this->client->search($params);
  39. echo "<pre class="brush:php;toolbar:false layui-box layui-code-view layui-code-notepad"><ol class="layui-code-ol"><li>";</li><li> var_dump($response);</li><li> echo "</li></ol></pre>";
  40. }
  41. } </code>

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

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

人气教程排行