[MediaWiki-commits] [Gerrit] Highlight changed words in suggestions. - change (mediawiki...CirrusSearch)
Manybubbles has uploaded a new change for review. https://gerrit.wikimedia.org/r/85206 Change subject: Highlight changed words in suggestions. .. Highlight changed words in suggestions. Bug: 52286 Change-Id: Ib0b6af9766896e826517cf5f9f9632a09e377cbe --- M CirrusSearchSearcher.php 1 file changed, 44 insertions(+), 10 deletions(-) git pull ssh://gerrit.wikimedia.org:29418/mediawiki/extensions/CirrusSearch refs/changes/06/85206/1 diff --git a/CirrusSearchSearcher.php b/CirrusSearchSearcher.php index 2afb534..f132635 100644 --- a/CirrusSearchSearcher.php +++ b/CirrusSearchSearcher.php @@ -21,6 +21,8 @@ class CirrusSearchSearcher { const PHRASE_TITLE = 'phrase_title'; const PHRASE_TEXT = 'phrase_text'; + const SUGGESTION_HIGHLIGHT_PRE = 'em'; + const SUGGESTION_HIGHLIGHT_POST = '/em'; const HIGHLIGHT_PRE = 'span class=searchmatch'; const HIGHLIGHT_POST = '/span'; @@ -186,6 +188,10 @@ 'suggest_mode' = 'always', // Forces us to generate lots of phrases to try. ), ), + 'highlight' = array( + 'pre_tag' = self::SUGGESTION_HIGHLIGHT_PRE, + 'post_tag' = self::SUGGESTION_HIGHLIGHT_POST, + ), ) ), self::PHRASE_TEXT = array( @@ -199,6 +205,10 @@ 'field' = 'text.suggest', 'suggest_mode' = 'always', // Forces us to generate lots of phrases to try. ), + ), + 'highlight' = array( + 'pre_tag' = self::SUGGESTION_HIGHLIGHT_PRE, + 'post_tag' = self::SUGGESTION_HIGHLIGHT_POST, ), ) ) @@ -487,17 +497,27 @@ * A set of results from Elasticsearch. */ class CirrusSearchResultSet extends SearchResultSet { + /** +* @var string|null lazy built escaped copy of CirrusSearchSearcher::SUGGESTION_HIGHLIGHT_PRE +*/ + private static $suggestionHighlightPreEscaped = null; + /** +* @var string|null lazy built escaped copy of CirrusSearchSearcher::SUGGESTION_HIGHLIGHT_POST +*/ + private static $suggestionHighlightPostEscaped = null; + private $result, $hits, $totalHits, $suggestionQuery, $suggestionSnippet; public function __construct( $res ) { $this-result = $res; $this-hits = $res-count(); $this-totalHits = $res-getTotalHits(); - $this-suggestionQuery = $this-findSuggestionQuery(); - $this-suggestionSnippet = $this-highlightingSuggestionSnippet(); + $suggestion = $this-findSuggestion(); + $this-suggestionQuery = $suggestion[ 'text' ]; + $this-suggestionSnippet = self::escapeHighlightedSuggestion( $suggestion[ 'highlighted' ] ); } - private function findSuggestionQuery() { + private function findSuggestion() { // TODO some kind of weighting? $suggest = $this-result-getResponse()-getData(); if ( !array_key_exists( 'suggest', $suggest ) ) { @@ -505,17 +525,29 @@ } $suggest = $suggest[ 'suggest' ]; foreach ( $suggest[ CirrusSearchSearcher::PHRASE_TITLE ][ 0 ][ 'options' ] as $option ) { - return $option[ 'text' ]; + return $option; } foreach ( $suggest[ CirrusSearchSearcher::PHRASE_TEXT ][ 0 ][ 'options' ] as $option ) { - return $option[ 'text' ]; + return $option; } return null; } - private function highlightingSuggestionSnippet() { - // TODO wrap the corrections in ems ES doesn't make this easy. - return $this-suggestionQuery; + /** +* Escape a highlighted suggestion coming back from Elasticsearch. +* @param $suggestion string suggestion from elasticsearch +* @return string $suggestion with html escaped _except_ highlighting pre and post tags +*/ + private static function
[MediaWiki-commits] [Gerrit] Highlight changed words in suggestions. - change (mediawiki...CirrusSearch)
jenkins-bot has submitted this change and it was merged. Change subject: Highlight changed words in suggestions. .. Highlight changed words in suggestions. Bug: 52286 Change-Id: Ib0b6af9766896e826517cf5f9f9632a09e377cbe --- M CirrusSearchSearcher.php 1 file changed, 44 insertions(+), 10 deletions(-) Approvals: Chad: Looks good to me, approved jenkins-bot: Verified diff --git a/CirrusSearchSearcher.php b/CirrusSearchSearcher.php index 2afb534..f132635 100644 --- a/CirrusSearchSearcher.php +++ b/CirrusSearchSearcher.php @@ -21,6 +21,8 @@ class CirrusSearchSearcher { const PHRASE_TITLE = 'phrase_title'; const PHRASE_TEXT = 'phrase_text'; + const SUGGESTION_HIGHLIGHT_PRE = 'em'; + const SUGGESTION_HIGHLIGHT_POST = '/em'; const HIGHLIGHT_PRE = 'span class=searchmatch'; const HIGHLIGHT_POST = '/span'; @@ -186,6 +188,10 @@ 'suggest_mode' = 'always', // Forces us to generate lots of phrases to try. ), ), + 'highlight' = array( + 'pre_tag' = self::SUGGESTION_HIGHLIGHT_PRE, + 'post_tag' = self::SUGGESTION_HIGHLIGHT_POST, + ), ) ), self::PHRASE_TEXT = array( @@ -199,6 +205,10 @@ 'field' = 'text.suggest', 'suggest_mode' = 'always', // Forces us to generate lots of phrases to try. ), + ), + 'highlight' = array( + 'pre_tag' = self::SUGGESTION_HIGHLIGHT_PRE, + 'post_tag' = self::SUGGESTION_HIGHLIGHT_POST, ), ) ) @@ -487,17 +497,27 @@ * A set of results from Elasticsearch. */ class CirrusSearchResultSet extends SearchResultSet { + /** +* @var string|null lazy built escaped copy of CirrusSearchSearcher::SUGGESTION_HIGHLIGHT_PRE +*/ + private static $suggestionHighlightPreEscaped = null; + /** +* @var string|null lazy built escaped copy of CirrusSearchSearcher::SUGGESTION_HIGHLIGHT_POST +*/ + private static $suggestionHighlightPostEscaped = null; + private $result, $hits, $totalHits, $suggestionQuery, $suggestionSnippet; public function __construct( $res ) { $this-result = $res; $this-hits = $res-count(); $this-totalHits = $res-getTotalHits(); - $this-suggestionQuery = $this-findSuggestionQuery(); - $this-suggestionSnippet = $this-highlightingSuggestionSnippet(); + $suggestion = $this-findSuggestion(); + $this-suggestionQuery = $suggestion[ 'text' ]; + $this-suggestionSnippet = self::escapeHighlightedSuggestion( $suggestion[ 'highlighted' ] ); } - private function findSuggestionQuery() { + private function findSuggestion() { // TODO some kind of weighting? $suggest = $this-result-getResponse()-getData(); if ( !array_key_exists( 'suggest', $suggest ) ) { @@ -505,17 +525,29 @@ } $suggest = $suggest[ 'suggest' ]; foreach ( $suggest[ CirrusSearchSearcher::PHRASE_TITLE ][ 0 ][ 'options' ] as $option ) { - return $option[ 'text' ]; + return $option; } foreach ( $suggest[ CirrusSearchSearcher::PHRASE_TEXT ][ 0 ][ 'options' ] as $option ) { - return $option[ 'text' ]; + return $option; } return null; } - private function highlightingSuggestionSnippet() { - // TODO wrap the corrections in ems ES doesn't make this easy. - return $this-suggestionQuery; + /** +* Escape a highlighted suggestion coming back from Elasticsearch. +* @param $suggestion string suggestion from elasticsearch +* @return string $suggestion with html escaped _except_ highlighting pre and post tags +*/ + private static function escapeHighlightedSuggestion( $suggestion ) { + if (