@@ -7,6 +7,23 @@ describe('Basic html theme search', function() {
77 return req . responseText ;
88 }
99
10+ function checkRanking ( expectedRanking , results ) {
11+ let [ nextExpected , ...remainingItems ] = expectedRanking ;
12+
13+ for ( result of results . reverse ( ) ) {
14+ if ( ! nextExpected ) break ;
15+
16+ let [ expectedPage , expectedTitle , expectedTarget ] = nextExpected ;
17+ let [ page , title , target ] = result ;
18+
19+ if ( page == expectedPage && title == expectedTitle && target == expectedTarget ) {
20+ [ nextExpected , ...remainingItems ] = remainingItems ;
21+ }
22+ }
23+
24+ expect ( remainingItems . length ) . toEqual ( 0 ) ;
25+ }
26+
1027 describe ( 'terms search' , function ( ) {
1128
1229 it ( 'should find "C++" when in index' , function ( ) {
@@ -76,7 +93,7 @@ describe('Basic html theme search', function() {
7693 'Main Page' ,
7794 '' ,
7895 null ,
79- 100 ,
96+ 16 ,
8097 'index.rst'
8198 ]
8299 ] ;
@@ -85,6 +102,66 @@ describe('Basic html theme search', function() {
85102
86103 } ) ;
87104
105+ describe ( 'search result ranking' , function ( ) {
106+
107+ /*
108+ * These tests should not proscribe precise expected ordering of search
109+ * results; instead each test case should describe a single relevance rule
110+ * that helps users to locate relevant information efficiently.
111+ *
112+ * If you think that one of the rules seems to be poorly-defined or is
113+ * limiting the potential for search algorithm improvements, please check
114+ * for existing discussion/bugreports related to it on GitHub[1] before
115+ * creating one yourself. Suggestions for possible improvements are also
116+ * welcome.
117+ *
118+ * [1] - https://github.com/sphinx-doc/sphinx.git/
119+ */
120+
121+ it ( 'should score a code module match above a page-title match' , function ( ) {
122+ eval ( loadFixture ( "titles/searchindex.js" ) ) ;
123+
124+ expectedRanking = [
125+ [ 'index' , 'relevance' , '#module-relevance' ] , /* py:module documentation */
126+ [ 'relevance' , 'Relevance' , '' ] , /* main title */
127+ ] ;
128+
129+ searchParameters = Search . _parseQuery ( 'relevance' ) ;
130+ results = Search . _performSearch ( ...searchParameters ) ;
131+
132+ checkRanking ( expectedRanking , results ) ;
133+ } ) ;
134+
135+ it ( 'should score a main-title match above an object member match' , function ( ) {
136+ eval ( loadFixture ( "titles/searchindex.js" ) ) ;
137+
138+ expectedRanking = [
139+ [ 'relevance' , 'Relevance' , '' ] , /* main title */
140+ [ 'index' , 'relevance.Example.relevance' , '#module-relevance' ] , /* py:class attribute */
141+ ] ;
142+
143+ searchParameters = Search . _parseQuery ( 'relevance' ) ;
144+ results = Search . _performSearch ( ...searchParameters ) ;
145+
146+ checkRanking ( expectedRanking , results ) ;
147+ } ) ;
148+
149+ it ( 'should score a main-title match above a subheading-title match' , function ( ) {
150+ eval ( loadFixture ( "titles/searchindex.js" ) ) ;
151+
152+ expectedRanking = [
153+ [ 'relevance' , 'Relevance' , '' ] , /* main title */
154+ [ 'index' , 'Main Page > Relevance' , '#relevance' ] , /* subsection heading title */
155+ ] ;
156+
157+ searchParameters = Search . _parseQuery ( 'relevance' ) ;
158+ results = Search . _performSearch ( ...searchParameters ) ;
159+
160+ checkRanking ( expectedRanking , results ) ;
161+ } ) ;
162+
163+ } ) ;
164+
88165} ) ;
89166
90167describe ( "htmlToText" , function ( ) {
0 commit comments