0
0
bleve/search/collectors/collector_top_score_test.go

418 lines
8.0 KiB
Go
Raw Normal View History

2014-04-17 22:55:53 +02:00
// Copyright (c) 2014 Couchbase, Inc.
// Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file
// except in compliance with the License. You may obtain a copy of the License at
// http://www.apache.org/licenses/LICENSE-2.0
// Unless required by applicable law or agreed to in writing, software distributed under the
// License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
// either express or implied. See the License for the specific language governing permissions
// and limitations under the License.
package collectors
2014-04-17 22:55:53 +02:00
import (
"testing"
"golang.org/x/net/context"
"github.com/blevesearch/bleve/search"
2014-04-17 22:55:53 +02:00
)
func TestTop10Scores(t *testing.T) {
// a stub search with more than 10 matches
// the top-10 scores are > 10
// everything else is less than 10
searcher := &stubSearcher{
matches: search.DocumentMatchCollection{
&search.DocumentMatch{
2014-04-17 22:55:53 +02:00
ID: "a",
Score: 11,
},
&search.DocumentMatch{
2014-04-17 22:55:53 +02:00
ID: "b",
Score: 9,
},
&search.DocumentMatch{
2014-04-17 22:55:53 +02:00
ID: "c",
Score: 11,
},
&search.DocumentMatch{
2014-04-17 22:55:53 +02:00
ID: "d",
Score: 9,
},
&search.DocumentMatch{
2014-04-17 22:55:53 +02:00
ID: "e",
Score: 11,
},
&search.DocumentMatch{
2014-04-17 22:55:53 +02:00
ID: "f",
Score: 9,
},
&search.DocumentMatch{
2014-04-17 22:55:53 +02:00
ID: "g",
Score: 11,
},
&search.DocumentMatch{
2014-04-17 22:55:53 +02:00
ID: "h",
Score: 9,
},
&search.DocumentMatch{
2014-04-17 22:55:53 +02:00
ID: "i",
Score: 11,
},
&search.DocumentMatch{
2014-04-17 22:55:53 +02:00
ID: "j",
Score: 11,
},
&search.DocumentMatch{
2014-04-17 22:55:53 +02:00
ID: "k",
Score: 11,
},
&search.DocumentMatch{
2014-04-17 22:55:53 +02:00
ID: "l",
Score: 99,
},
&search.DocumentMatch{
2014-04-17 22:55:53 +02:00
ID: "m",
Score: 11,
},
&search.DocumentMatch{
2014-04-17 22:55:53 +02:00
ID: "n",
Score: 11,
},
},
}
collector := NewTopScorerCollector(10)
err := collector.Collect(context.Background(), searcher)
if err != nil {
t.Fatal(err)
}
maxScore := collector.MaxScore()
if maxScore != 99.0 {
t.Errorf("expected max score 99.0, got %f", maxScore)
}
total := collector.Total()
if total != 14 {
t.Errorf("expected 14 total results, got %d", total)
}
2014-04-17 22:55:53 +02:00
results := collector.Results()
if len(results) != 10 {
t.Fatalf("expected 10 results, got %d", len(results))
}
if results[0].ID != "l" {
t.Errorf("expected first result to have ID 'l', got %s", results[0].ID)
}
if results[0].Score != 99.0 {
t.Errorf("expected highest score to be 99.0, got %f", results[0].Score)
}
minScore := 1000.0
for _, result := range results {
if result.Score < minScore {
minScore = result.Score
}
}
if minScore < 10 {
t.Errorf("expected minimum score to be higher than 10, got %f", minScore)
}
}
func TestTop10ScoresSkip10(t *testing.T) {
// a stub search with more than 10 matches
// the top-10 scores are > 10
// everything else is less than 10
searcher := &stubSearcher{
matches: search.DocumentMatchCollection{
&search.DocumentMatch{
ID: "a",
Score: 11,
},
&search.DocumentMatch{
ID: "b",
Score: 9.5,
},
&search.DocumentMatch{
ID: "c",
Score: 11,
},
&search.DocumentMatch{
ID: "d",
Score: 9,
},
&search.DocumentMatch{
ID: "e",
Score: 11,
},
&search.DocumentMatch{
ID: "f",
Score: 9,
},
&search.DocumentMatch{
ID: "g",
Score: 11,
},
&search.DocumentMatch{
ID: "h",
Score: 9,
},
&search.DocumentMatch{
ID: "i",
Score: 11,
},
&search.DocumentMatch{
ID: "j",
Score: 11,
},
&search.DocumentMatch{
ID: "k",
Score: 11,
},
&search.DocumentMatch{
ID: "l",
Score: 99,
},
&search.DocumentMatch{
ID: "m",
Score: 11,
},
&search.DocumentMatch{
ID: "n",
Score: 11,
},
},
}
collector := NewTopScorerSkipCollector(10, 10)
err := collector.Collect(context.Background(), searcher)
if err != nil {
t.Fatal(err)
}
maxScore := collector.MaxScore()
if maxScore != 99.0 {
t.Errorf("expected max score 99.0, got %f", maxScore)
}
total := collector.Total()
if total != 14 {
t.Errorf("expected 14 total results, got %d", total)
}
results := collector.Results()
if len(results) != 4 {
t.Fatalf("expected 4 results, got %d", len(results))
}
if results[0].ID != "b" {
t.Errorf("expected first result to have ID 'b', got %s", results[0].ID)
}
if results[0].Score != 9.5 {
t.Errorf("expected highest score to be 9.5ß, got %f", results[0].Score)
}
}
2015-03-06 18:59:44 +01:00
fix pagination bug introduced by collector optimization fixes #378 this bug was introduced by: https://github.com/blevesearch/bleve/commit/f2aba116c49ea51b27bc9afd3bf15305ef04883c theory of operation for this collector (top N, skip K) - collect the highest scoring N+K results - if K > 0, skip K and return the next N internal details - the top N+K are kept in a list - the list is ordered from lowest scoring (first) to highest scoring (last) - as a hit comes in, we find where this new hit would fit into this list - if this caused the list to get too big, trim off the head (lowest scoring hit) theory of the optimization - we were not tracking the lowest score in the list - so if the score was lower than the lowest score, we would add/remove it - by keeping track of the lowest score in the list, we can avoid these ops problem with the optimization - the optimization worked by returning early - by returning early there was a subtle change to documents which had the same score - the reason is that which docs end up in the top N+K changed by returning early - why was that? docs are coming in, in order by key ascending - when finding the correct position to insert a hit into the list, we checked <, not <= the score - this has the subtle effect that docs with the same score end up in reverse order for example consider the following in progress list: doc ids [ c a b ] scores [ 1 5 9 ] if we now see doc d with score 5, we get: doc ids [ c a d b ] scores [ 1 5 5 9 ] While that appears in order (a, d) it is actually reverse order, because when we produce the top N we start at the end. theory of the fix - previous pagination depended on later hits with the same score "bumping" earlier hits with the same score off the bottom of the list - however, if we change the logic to <= instead of <, now the list in the previous example would look like: doc ids [ c d a b ] scores [ 1 5 5 9 ] - this small change means that now earlier (lower id) will score higher, and thus we no longer depend on later hits bumping things down, which means returning early is a valid thing to do NOTE: this does depend on the hits coming back in order by ID. this is not something strictly guaranteed, but it was the same assumption that allowed the original behavior This also has the side-effect that 2 hits with the same score come back in ascending ID order, which is somehow more pleasing to me than reverse order.
2016-06-01 16:43:14 +02:00
func TestPaginationSameScores(t *testing.T) {
// a stub search with more than 10 matches
// all documents have the same score
searcher := &stubSearcher{
matches: search.DocumentMatchCollection{
&search.DocumentMatch{
ID: "a",
Score: 5,
},
&search.DocumentMatch{
ID: "b",
Score: 5,
},
&search.DocumentMatch{
ID: "c",
Score: 5,
},
&search.DocumentMatch{
ID: "d",
Score: 5,
},
&search.DocumentMatch{
ID: "e",
Score: 5,
},
&search.DocumentMatch{
ID: "f",
Score: 5,
},
&search.DocumentMatch{
ID: "g",
Score: 5,
},
&search.DocumentMatch{
ID: "h",
Score: 5,
},
&search.DocumentMatch{
ID: "i",
Score: 5,
},
&search.DocumentMatch{
ID: "j",
Score: 5,
},
&search.DocumentMatch{
ID: "k",
Score: 5,
},
&search.DocumentMatch{
ID: "l",
Score: 5,
},
&search.DocumentMatch{
ID: "m",
Score: 5,
},
&search.DocumentMatch{
ID: "n",
Score: 5,
},
},
}
// first get first 5 hits
collector := NewTopScorerSkipCollector(5, 0)
err := collector.Collect(context.Background(), searcher)
if err != nil {
t.Fatal(err)
}
total := collector.Total()
if total != 14 {
t.Errorf("expected 14 total results, got %d", total)
}
results := collector.Results()
if len(results) != 5 {
t.Fatalf("expected 5 results, got %d", len(results))
}
firstResults := make(map[string]struct{})
for _, hit := range results {
firstResults[hit.ID] = struct{}{}
}
// a stub search with more than 10 matches
// all documents have the same score
searcher = &stubSearcher{
matches: search.DocumentMatchCollection{
&search.DocumentMatch{
ID: "a",
Score: 5,
},
&search.DocumentMatch{
ID: "b",
Score: 5,
},
&search.DocumentMatch{
ID: "c",
Score: 5,
},
&search.DocumentMatch{
ID: "d",
Score: 5,
},
&search.DocumentMatch{
ID: "e",
Score: 5,
},
&search.DocumentMatch{
ID: "f",
Score: 5,
},
&search.DocumentMatch{
ID: "g",
Score: 5,
},
&search.DocumentMatch{
ID: "h",
Score: 5,
},
&search.DocumentMatch{
ID: "i",
Score: 5,
},
&search.DocumentMatch{
ID: "j",
Score: 5,
},
&search.DocumentMatch{
ID: "k",
Score: 5,
},
&search.DocumentMatch{
ID: "l",
Score: 5,
},
&search.DocumentMatch{
ID: "m",
Score: 5,
},
&search.DocumentMatch{
ID: "n",
Score: 5,
},
},
}
// now get next 5 hits
collector = NewTopScorerSkipCollector(5, 5)
err = collector.Collect(context.Background(), searcher)
if err != nil {
t.Fatal(err)
}
total = collector.Total()
if total != 14 {
t.Errorf("expected 14 total results, got %d", total)
}
results = collector.Results()
if len(results) != 5 {
t.Fatalf("expected 5 results, got %d", len(results))
}
// make sure that none of these hits repeat ones we saw in the top 5
for _, hit := range results {
if _, ok := firstResults[hit.ID]; ok {
t.Errorf("doc ID %s is in top 5 and next 5 result sets", hit.ID)
}
}
}
2015-03-06 18:59:44 +01:00
func BenchmarkTop10of100000Scores(b *testing.B) {
benchHelper(10000, NewTopScorerCollector(10), b)
}
2015-03-06 18:59:44 +01:00
func BenchmarkTop100of100000Scores(b *testing.B) {
benchHelper(10000, NewTopScorerCollector(100), b)
}
2015-03-06 18:59:44 +01:00
func BenchmarkTop10of1000000Scores(b *testing.B) {
benchHelper(100000, NewTopScorerCollector(10), b)
}
2015-03-06 18:59:44 +01:00
func BenchmarkTop100of1000000Scores(b *testing.B) {
benchHelper(100000, NewTopScorerCollector(100), b)
2015-03-06 18:59:44 +01:00
}