@@ -226,6 +226,66 @@ func TestShardedSearcher_Ranking(t *testing.T) {
226226 }
227227}
228228
229+ func TestShardedSearcher_DocumentRanking (t * testing.T ) {
230+ ss := newShardedSearcher (1 )
231+
232+ var nextShardNum int
233+ addShard := func (repo string , priority float64 , docs ... zoekt.Document ) {
234+ r := & zoekt.Repository {ID : hash (repo ), Name : repo }
235+ r .RawConfig = map [string ]string {
236+ "public" : "1" ,
237+ "priority" : strconv .FormatFloat (priority , 'f' , 2 , 64 ),
238+ }
239+ b := testIndexBuilder (t , r , docs ... )
240+ shard := searcherForTest (t , b )
241+ ss .replace (map [string ]zoekt.Searcher {
242+ fmt .Sprintf ("key-%d" , nextShardNum ): shard ,
243+ })
244+ nextShardNum ++
245+ }
246+
247+ addShard ("weekend-project" , 0.25 , zoekt.Document {Name : "f1" , Content : []byte ("foobar" )})
248+ addShard ("moderately-popular" , 0.4 , zoekt.Document {Name : "f2" , Content : []byte ("foobaz" )})
249+ addShard ("weekend-project-2" , 0.25 , zoekt.Document {Name : "f3" , Content : []byte ("foo bar" )})
250+ addShard ("super-star" , 0.9 , zoekt.Document {Name : "f4" , Content : []byte ("foo baz" )},
251+ zoekt.Document {Name : "f5" , Content : []byte ("fooooo" )})
252+
253+ // Run a stream search and gather the results
254+ var results []* zoekt.SearchResult
255+ opts := & zoekt.SearchOptions {
256+ UseDocumentRanks : true ,
257+ FlushWallTime : 100 * time .Millisecond ,
258+ }
259+
260+ err := ss .StreamSearch (context .Background (), & query.Substring {Pattern : "foo" }, opts ,
261+ stream .SenderFunc (func (event * zoekt.SearchResult ) {
262+ results = append (results , event )
263+ }))
264+
265+ if err != nil {
266+ t .Fatal (err )
267+ }
268+
269+ // There should always be two stream results, first progress-only, then the file results
270+ if len (results ) != 2 {
271+ t .Fatalf ("expected 2 streamed results, but got %d" , len (results ))
272+ }
273+
274+ // The ranking should be determined by whether it's an exact word match,
275+ // followed by repository priority
276+ want := []string {"f4" , "f3" , "f5" , "f2" , "f1" }
277+
278+ files := results [1 ].Files
279+ got := make ([]string , len (files ))
280+ for i := 0 ; i < len (files ); i ++ {
281+ got [i ] = files [i ].FileName
282+ }
283+
284+ if ! reflect .DeepEqual (got , want ) {
285+ t .Errorf ("got %v, want %v" , got , want )
286+ }
287+ }
288+
229289func TestFilteringShardsByRepoSetOrBranchesReposOrRepoIDs (t * testing.T ) {
230290 ss := newShardedSearcher (1 )
231291
0 commit comments