Skip to content

Commit 239d03d

Browse files
committed
[FEATURE] Highlighting support in fulltext search results
Automatically calls ``.highlight()`` to wrap search term matches found in fulltext search results. Matches are wrapped in ``<em>`` tags. This change re-implements the functionality originally submitted inside #63.
1 parent 3ec67c9 commit 239d03d

1 file changed

Lines changed: 31 additions & 2 deletions

File tree

Classes/Flowpack/ElasticSearch/ContentRepositoryAdaptor/Eel/ElasticSearchQueryBuilder.php

Lines changed: 31 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ class ElasticSearchQueryBuilder implements QueryBuilderInterface, ProtectedConte
6666
*
6767
* @var array
6868
*/
69-
protected $unsupportedFieldsInCountRequest = array('fields', 'sort', 'from', 'size');
69+
protected $unsupportedFieldsInCountRequest = array('fields', 'sort', 'from', 'size', 'highlight');
7070

7171
/**
7272
* Amount of total items in response without limit
@@ -570,12 +570,41 @@ public function count() {
570570
* @api
571571
*/
572572
public function fulltext($searchWord) {
573-
574573
$this->appendAtPath('query.filtered.query.bool.must', array(
575574
'query_string' => array(
576575
'query' => $searchWord
577576
)
578577
));
578+
579+
// We automatically enable result highlighting when doing fulltext searches. It is up to the user to use this information or not use it.
580+
return $this->highlight(150, 2);
581+
}
582+
583+
/**
584+
* Configure Result Highlighting. Only makes sense in combination with fulltext(). By default, highlighting is enabled.
585+
* It can be disabled by calling "highlight(FALSE)".
586+
*
587+
* @param integer|boolean $fragmentSize The result fragment size for highlight snippets. If this parameter is FALSE, highlighting will be disabled.
588+
* @param integer $fragmentCount The number of highlight fragments to show.
589+
* @return $this
590+
* @api
591+
*/
592+
public function highlight($fragmentSize, $fragmentCount = NULL) {
593+
if ($fragmentSize === FALSE) {
594+
// Highlighting is disabled.
595+
unset($this->request['highlight']);
596+
} else {
597+
$this->request['highlight'] = array(
598+
'fields' => array(
599+
'__fulltext*' => array(
600+
'fragment_size' => $fragmentSize,
601+
'no_match_size' => $fragmentSize,
602+
'number_of_fragments' => $fragmentCount
603+
)
604+
)
605+
);
606+
}
607+
579608
return $this;
580609
}
581610

0 commit comments

Comments
 (0)