Elastica\Query::setHighlight PHP Method

setHighlight() public method

Sets highlight arguments for the query.
public setHighlight ( array $highlightArgs )
$highlightArgs array Set all highlight arguments
    public function setHighlight(array $highlightArgs)
    {
        return $this->setParam('highlight', $highlightArgs);
    }

Usage Example

 /**
  * Search revisions with provided term.
  *
  * @param string $term Term to search
  * @return Status
  */
 public function searchText($term)
 {
     // full-text search
     $queryString = new QueryString($term);
     $queryString->setFields(array('revisions.text'));
     $this->query->setQuery($queryString);
     // add aggregation to determine exact amount of matching search terms
     $terms = $this->getTerms($term);
     $this->query->addAggregation($this->termsAggregation($terms));
     // @todo: abstract-away this config? (core/cirrus also has this - share it somehow?)
     $this->query->setHighlight(array('fields' => array(static::HIGHLIGHT_FIELD => array('type' => 'plain', 'order' => 'score', 'number_of_fragments' => 1, 'fragment_size' => 10000)), 'pre_tags' => array(static::HIGHLIGHT_PRE), 'post_tags' => array(static::HIGHLIGHT_POST)));
     // @todo: support insource: queries (and perhaps others)
     $searchable = Connection::getFlowIndex($this->indexBaseName);
     if ($this->type !== false) {
         $searchable = $searchable->getType($this->type);
     }
     $search = $searchable->createSearch($this->query);
     // @todo: PoolCounter config at PoolCounterSettings-eqiad.php
     // @todo: do we want this class to extend from ElasticsearchIntermediary and use its success & failure methods (like CirrusSearch/Searcher does)?
     // Perform the search
     $work = new PoolCounterWorkViaCallback('Flow-Search', "_elasticsearch", array('doWork' => function () use($search) {
         try {
             $result = $search->search();
             return Status::newGood($result);
         } catch (ExceptionInterface $e) {
             if (strpos($e->getMessage(), 'dynamic scripting for [groovy] disabled')) {
                 // known issue with default ES config, let's display a more helpful message
                 return Status::newFatal(new \RawMessage("Couldn't complete search: dynamic scripting needs to be enabled. " . "Please add 'script.disable_dynamic: false' to your elasticsearch.yml"));
             }
             return Status::newFatal('flow-error-search');
         }
     }, 'error' => function (Status $status) {
         $status = $status->getErrorsArray();
         wfLogWarning('Pool error searching Elasticsearch: ' . $status[0][0]);
         return Status::newFatal('flow-error-search');
     }));
     $result = $work->execute();
     return $result;
 }
All Usage Examples Of Elastica\Query::setHighlight