jenkins-bot has submitted this change and it was merged.
Change subject: Add an auxiliary text field
......................................................................
Add an auxiliary text field
This field will hold text that isn't part of the article proper but still
should be searched. Stuff like caption titles and see more links. It'll
be searched with lower boost then page text and highlighted only if there
isn't any page text to highlight.
DEPLOYMENT: This requires rebuilding the index. If you don't rebuild the
index portions of the page text will be written to the auxiliary_text field
but won't be searchable or highlightable. This shouldn't be big problem
because we expect the text to be less commonly searched. We'll rebuild the
indexes quickly anyway.
NOTE: We're relying on field autocreation being off for the above statement
to be true. That is OK because it is off at WMF and the README tells folks
to turn it off. If it isn't off then the first update will autocreate the
field and it won't be highlightable. In fact, it'll cause queries to fail.
All of them. Don't do that.
REQUIRES: I97cf66014719244b8bb2b0509b419c82202bdb01 from core
Change-Id: I70be621d2702409d88befd8f7ba0936cbc4912ef
---
M CirrusSearch.php
M includes/BuildDocument/PageTextBuilder.php
M includes/MappingConfigBuilder.php
M includes/Result.php
M includes/ResultsType.php
M includes/Searcher.php
M tests/browser/features/full_text.feature
M tests/browser/features/highlighting.feature
M tests/browser/features/support/hooks.rb
9 files changed, 102 insertions(+), 29 deletions(-)
Approvals:
Chad: Looks good to me, approved
jenkins-bot: Verified
diff --git a/CirrusSearch.php b/CirrusSearch.php
index a0506a6..b277a95 100644
--- a/CirrusSearch.php
+++ b/CirrusSearch.php
@@ -192,7 +192,13 @@
$wgCirrusSearchUnlinkedArticlesToUpdate = 25;
// Weight of fields relative to article text
-$wgCirrusSearchWeights = array( 'title' => 20.0, 'redirect' => 15.0, 'heading'
=> 5.0, 'file_text' => 0.8 );
+$wgCirrusSearchWeights = array(
+ 'title' => 20.0,
+ 'redirect' => 15.0,
+ 'heading' => 5.0,
+ 'auxiliary_text' => 0.8,
+ 'file_text' => 0.5,
+);
// Weight of fields that match via "near_match" which is ordered.
$wgCirrusSearchNearMatchWeight = 2;
diff --git a/includes/BuildDocument/PageTextBuilder.php
b/includes/BuildDocument/PageTextBuilder.php
index 745c358..29b1032 100644
--- a/includes/BuildDocument/PageTextBuilder.php
+++ b/includes/BuildDocument/PageTextBuilder.php
@@ -29,8 +29,9 @@
}
public function build() {
- $text = $this->buildTextToIndex();
+ list( $text, $auxiliary ) = $this->buildTextToIndex();
$this->doc->add( 'text', $text );
+ $this->doc->add( 'auxiliary_text', $auxiliary );
$this->doc->add( 'text_bytes', strlen( $text ) );
return $this->doc;
}
@@ -42,13 +43,13 @@
private function buildTextToIndex() {
switch ( $this->content->getModel() ) {
case CONTENT_MODEL_WIKITEXT:
- $text = $this->formatWikitext(
$this->parserOutput );
- break;
+ return $this->formatWikitext(
$this->parserOutput );
default:
- $text = $this->content->getTextForSearchIndex();
+ $text = trim( Sanitizer::stripAllTags(
$this->content->getTextForSearchIndex() ) );
+ return array( $text, array() );
}
- return trim( Sanitizer::stripAllTags( $text ) );
+ return $text;
}
/**
@@ -60,12 +61,47 @@
private function formatWikitext( ParserOutput $parserOutput ) {
$parserOutput->setEditSectionTokens( false );
$formatter = new HtmlFormatter( $parserOutput->getText() );
- $formatter->remove( array( 'audio', 'video', '#toc',
'.thumbcaption',
+
+ // Strip elements from the page that we never want in the
search text.
+ $formatter->remove( array(
+ 'audio', 'video', // "it looks like you don't
have javascript enabled..." do not need to index
+ '#toc', // Already indexed as part of
the headings. No need.
'sup.reference', // The [1] for references
'.mw-cite-backlink', // The ↑ next to refenences in
the references section
+ 'h1', 'h2', 'h3', // Headings are already indexed
in their own field.
+ 'h5', 'h6', 'h4',
) );
$formatter->filterContent();
- return $formatter->getText();
+
+ // Strip elements from the page that are auxiliary text. These
will still be
+ // searched but matches will be ranked lower and non-auxiliary
matches will be
+ // prefered in highlighting.
+ $formatter->remove( array(
+ '.thumbcaption', // Thumbnail captions aren't
really part of the text proper
+ 'table', // Neither are tables
+ '.rellink', // Common style for "See also:".
+ '.dablink', // Common style for calling out
helpful links at the top of the article.
+ '.catlinks', // Categories aren't
article text.
+ '.searchaux', // New class users can
use to mark stuff as auxiliary to searches.
+ ) );
+ $auxiliaryElements = $formatter->filterContent();
+ $text = trim( Sanitizer::stripAllTags( $formatter->getText() )
);
+ $auxiliary = array();
+ foreach ( $auxiliaryElements as $auxiliaryElement ) {
+ $auxiliary[] = trim( Sanitizer::stripAllTags(
$formatter->getText( $auxiliaryElement ) ) );
+ }
+
+ return array( $text, $auxiliary );
}
+ /**
+ * Get the unicode paragraph separator character.
+ */
+ private function paragraphSeparator() {
+ static $paragraphSeparator;
+ if ( $paragraphSeparator === null ) {
+ $paragraphSeparator = json_decode('"\u2029"');
+ }
+ return $paragraphSeparator;
+ }
}
diff --git a/includes/MappingConfigBuilder.php
b/includes/MappingConfigBuilder.php
index 86e637a..98e2ec2 100644
--- a/includes/MappingConfigBuilder.php
+++ b/includes/MappingConfigBuilder.php
@@ -33,7 +33,7 @@
* and change the minor version when it changes but isn't
* incompatible
*/
- const VERSION = '1.0';
+ const VERSION = '1.1';
/**
* Whether to allow prefix searches to match on any word
@@ -114,6 +114,7 @@
'analyzer' => 'plain',
) ) )
),
+ 'auxiliary_text' => $this->buildStringField(
$textOptions ),
'file_text' => $this->buildStringField(
$textOptions ),
'category' =>
$this->buildLowercaseKeywordField(),
'template' =>
$this->buildLowercaseKeywordField(),
diff --git a/includes/Result.php b/includes/Result.php
index aabcf07..59a46b6 100644
--- a/includes/Result.php
+++ b/includes/Result.php
@@ -83,19 +83,7 @@
$this->redirectSnipppet = $this->escapeHighlightedText(
$highlights[ 'redirect.title' ][ 0 ] );
}
- if ( isset( $highlights[ 'text' ] ) ) {
- // This can get skipped if there the page was sent to
Elasticsearch without text.
- // This could be a bug or it could be that the page
simply doesn't have any text.
- $snippet = $highlights[ 'text' ][ 0 ];
- if ( isset( $highlights[ 'file_text' ] ) ) {
- $fileTextSnippet = $highlights[ 'file_text' ][
0 ];
- if ( !$this->containsMatches( $snippet ) &&
$this->containsMatches( $fileTextSnippet ) ) {
- $snippet = $fileTextSnippet;
- $this->isFileMatch = true;
- }
- }
- $this->textSnippet = $this->escapeHighlightedText(
$snippet );
- }
+ $this->textSnippet = $this->escapeHighlightedText(
$this->pickTextSnippet( $highlights ) );
if ( isset( $highlights[ 'heading' ] ) ) {
$this->sectionSnippet = $this->escapeHighlightedText(
$highlights[ 'heading' ][ 0 ] );
@@ -107,6 +95,33 @@
}
}
+ private function pickTextSnippet( $highlights ) {
+ if ( isset( $highlights[ 'text' ] ) ) {
+ $mainSnippet = $highlights[ 'text' ][ 0 ];
+ if ( $this->containsMatches( $mainSnippet ) ) {
+ return $mainSnippet;
+ }
+ } else {
+ // This can get skipped if there the page was sent to
Elasticsearch without text.
+ // This could be a bug or it could be that the page
simply doesn't have any text.
+ $mainSnipppet = '';
+ }
+ if ( isset( $highlights[ 'auxiliary_text' ] ) ) {
+ $auxSnippet = $highlights[ 'auxiliary_text' ][ 0 ];
+ if ( $this->containsMatches( $auxSnippet ) ) {
+ return $auxSnippet;
+ }
+ }
+ if ( isset( $highlights[ 'file_text' ] ) ) {
+ $fileSnippet = $highlights[ 'file_text' ][ 0 ];
+ if ( $this->containsMatches( $fileSnippet ) ) {
+ $this->isFileMatch = true;
+ return $fileSnippet;
+ }
+ }
+ return $mainSnippet;
+ }
+
/**
* Don't bother hitting the revision table and loading extra stuff like
* that into memory like the parent does, just return if we've got an
idea
diff --git a/includes/ResultsType.php b/includes/ResultsType.php
index d32b383..9cd8179 100644
--- a/includes/ResultsType.php
+++ b/includes/ResultsType.php
@@ -224,6 +224,7 @@
'fields' => $this->addMatchedFields( array(
'title' => $entireValue,
'text' => $text,
+ 'auxiliary_text' => $singleFragment,
'file_text' => $singleFragment,
'redirect.title' => $entireValueInListField,
'heading' => $entireValueInListField,
diff --git a/includes/Searcher.php b/includes/Searcher.php
index b32da14..9328791 100644
--- a/includes/Searcher.php
+++ b/includes/Searcher.php
@@ -894,6 +894,7 @@
global $wgCirrusSearchWeights;
$titleWeight = $weight * $wgCirrusSearchWeights[ 'title' ];
$headingWeight = $weight * $wgCirrusSearchWeights[ 'heading' ];
+ $auxiliaryTextWeight = $weight * $wgCirrusSearchWeights[
'auxiliary_text' ];
$fileTextWeight = $weight * $wgCirrusSearchWeights[ 'file_text'
];
$fields = array();
$fields[] = "title${fieldSuffix}^${titleWeight}";
@@ -901,6 +902,7 @@
if ( $fieldSuffix !== '.near_match' ) {
$fields[] = "heading${fieldSuffix}^${headingWeight}";
$fields[] = "text${fieldSuffix}^${weight}";
+ $fields[] =
"auxiliary_text${fieldSuffix}^${auxiliaryTextWeight}";
$fields[] = "file_text${fieldSuffix}^${fileTextWeight}";
}
diff --git a/tests/browser/features/full_text.feature
b/tests/browser/features/full_text.feature
index 0358b6e..8df2947 100644
--- a/tests/browser/features/full_text.feature
+++ b/tests/browser/features/full_text.feature
@@ -171,6 +171,7 @@
And Relevancytestviaredirect is the second search result
And Relevancytestviaheading is the third search result
And Relevancytestviatext is the fourth search result
+ And Relevancytestviaauxtext is the fifth search result
@relevancy
Scenario: Two word searches are sorted in the order we expect
diff --git a/tests/browser/features/highlighting.feature
b/tests/browser/features/highlighting.feature
index 28bf913..3c4674d 100644
--- a/tests/browser/features/highlighting.feature
+++ b/tests/browser/features/highlighting.feature
@@ -21,6 +21,8 @@
# Verify highlighting on large pages (Bug 52680). It is neat to see that
the stopwords aren't highlighted.
| "discuss problems of social and cultural importance" | Rashidun
Caliphate | the faithful gathered to *discuss problems of social and cultural
importance*. During the caliphate of |
| "discuss problems of social and cultural importance"~ | Rashidun
Caliphate | the faithful gathered to *discuss problems of social and cultural
importance*. During the caliphate of |
+ # Auxiliary text
+ | tallest alborz | Rashidun Caliphate | Mount Damavand,
Iran's *tallest* mountain is located in *Alborz* mountain range. |
@highlighting
Scenario: Even stopwords are highlighted
@@ -38,10 +40,14 @@
Then *I am a heading* is the highlighted alttitle of the first search
result
@highlighting
- Scenario: Found words are highlighted in headings and text even in large
documents
+ Scenario: Found words are highlighted in headings even in large documents
When I search for "Succession of Umar"
Then *Succession of Umar* is the highlighted alttitle of the first search
result
- And *Succession of Umar* is in the highlighted text of the first search
result
+
+ @highlighting
+ Scenario: Found words are highlighted in text even in large documents
+ When I search for "Allowance to non-Muslims"
+ Then *Allowance to non-Muslims* is in the highlighted text of the first
search result
@highlighting
Scenario: Words are not found in image captions
@@ -59,10 +65,14 @@
Then *I am a heading* is the highlighted alttitle of the first search
result
@headings @highlighting
- Scenario: Found words are highlighted in headings and text even in large
documents when searching in a non-strict phrase
+ Scenario: Found words are highlighted in headings even in large documents
when searching in a non-strict phrase
When I search for "Succession of Umar"~
Then *Succession of Umar* is the highlighted alttitle of the first search
result
- And *Succession of Umar* is in the highlighted text of the first search
result
+
+ @headings @highlighting
+ Scenario: Found words are highlighted in headings even in large documents
when searching in a non-strict phrase
+ When I search for "Allowance to non-Muslims"~
+ Then *Allowance to non-Muslims* is in the highlighted text of the first
search result
@headings @highlighting
Scenario: The highest scoring heading is highlighted AND it doesn't contain
html even if the heading on the page does
@@ -95,14 +105,14 @@
And *Reference* *Section* is the highlighted alttitle of the first search
result
@highlighting @references
- Scenario: References don't appear in highlighted text
+ Scenario: References ([1]) don't appear in highlighted text
When I search for Reference Text Highlight Test
- And *Reference* Section *Reference* *Text* *References* foo baz bar
is the highlighted text of the first search result
+ And *Reference* *Text* foo baz bar is the highlighted text of the
first search result
@highlighting @references
Scenario: References are highlighted if you search for them
When I search for Reference foo bar baz Highlight Test
- And *Reference* Section *Reference* Text *References* *foo* *baz*
*bar* is the highlighted text of the first search result
+ And *Reference* Text *foo* *baz* *bar* is the highlighted text of
the first search result
@programmer_friendly @highlighting
Scenario: camelCase is highlighted correctly
diff --git a/tests/browser/features/support/hooks.rb
b/tests/browser/features/support/hooks.rb
index f04e1fc..96e96de 100644
--- a/tests/browser/features/support/hooks.rb
+++ b/tests/browser/features/support/hooks.rb
@@ -373,6 +373,7 @@
And a page named Relevancytest Redirect exists with contents #REDIRECT
[[Relevancytestviaredirect]]
And a page named Relevancytestviaheading exists with contents
==Relevancytest==
And a page named Relevancytestviatext exists with contents Relevancytest
+ And a page named Relevancytestviaauxtext exists with contents
[[Dummy|thumb|right|200px|Relevancytest]]
And a page named Relevancytwo Wordtest exists
And a page named Wordtest Relevancytwo exists
And a page named Relevancynamespacetest exists
--
To view, visit https://gerrit.wikimedia.org/r/127140
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings
Gerrit-MessageType: merged
Gerrit-Change-Id: I70be621d2702409d88befd8f7ba0936cbc4912ef
Gerrit-PatchSet: 6
Gerrit-Project: mediawiki/extensions/CirrusSearch
Gerrit-Branch: master
Gerrit-Owner: Manybubbles <[email protected]>
Gerrit-Reviewer: Chad <[email protected]>
Gerrit-Reviewer: Manybubbles <[email protected]>
Gerrit-Reviewer: jenkins-bot <>
_______________________________________________
MediaWiki-commits mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits