使用ongr-io / ElasticsearchDSL软件包添加突出显示

时间:2019-06-23 03:04:36

标签: php elasticsearch ongr

找不到任何示例,如何使用higlighthttps://github.com/ongr-io/ElasticsearchDSL添加到查询结果中

 <?php
  require 'vendor/autoload.php'; //Composer autoload

  $client = ClientBuilder::create()->build(); //elasticsearch-php client

  $matchAll = new ONGR\ElasticsearchDSL\Query\MatchAllQuery();

  $search = new ONGR\ElasticsearchDSL\Search();
  $search->addQuery($matchAll);

  //How to highlight results in title field?

  $params = [
    'index' => 'your_index',
    'body' => $search->toArray(),
  ];

  $results = $client->search($params);

1 个答案:

答案 0 :(得分:1)

 <?php
  require 'vendor/autoload.php'; //Composer autoload

  $client = ClientBuilder::create()->build(); //elasticsearch-php client

  $matchAll = new ONGR\ElasticsearchDSL\Query\MatchAllQuery();

  $search = new ONGR\ElasticsearchDSL\Search();
  $search->addQuery($matchAll);

  $higlight = new Highlight();
  $higlight->addField('title');
  $search->addHighlight($higlight);

  $params = [
    'index' => 'your_index',
    'body' => $search->toArray(),
  ];

  $results = $client->search($params);