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.
|
2014-09-02 16:54:50 +02:00
|
|
|
|
2014-09-01 17:15:38 +02:00
|
|
|
package collectors
|
2014-04-17 22:55:53 +02:00
|
|
|
|
|
|
|
import (
|
2015-03-06 18:59:44 +01:00
|
|
|
"math/rand"
|
|
|
|
"strconv"
|
2014-04-17 22:55:53 +02:00
|
|
|
"testing"
|
2014-09-01 17:15:38 +02:00
|
|
|
|
2016-03-02 22:55:02 +01:00
|
|
|
"golang.org/x/net/context"
|
|
|
|
|
2014-09-01 17:15:38 +02:00
|
|
|
"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{
|
2014-09-01 17:15:38 +02:00
|
|
|
matches: search.DocumentMatchCollection{
|
|
|
|
&search.DocumentMatch{
|
2014-04-17 22:55:53 +02:00
|
|
|
ID: "a",
|
|
|
|
Score: 11,
|
|
|
|
},
|
2014-09-01 17:15:38 +02:00
|
|
|
&search.DocumentMatch{
|
2014-04-17 22:55:53 +02:00
|
|
|
ID: "b",
|
|
|
|
Score: 9,
|
|
|
|
},
|
2014-09-01 17:15:38 +02:00
|
|
|
&search.DocumentMatch{
|
2014-04-17 22:55:53 +02:00
|
|
|
ID: "c",
|
|
|
|
Score: 11,
|
|
|
|
},
|
2014-09-01 17:15:38 +02:00
|
|
|
&search.DocumentMatch{
|
2014-04-17 22:55:53 +02:00
|
|
|
ID: "d",
|
|
|
|
Score: 9,
|
|
|
|
},
|
2014-09-01 17:15:38 +02:00
|
|
|
&search.DocumentMatch{
|
2014-04-17 22:55:53 +02:00
|
|
|
ID: "e",
|
|
|
|
Score: 11,
|
|
|
|
},
|
2014-09-01 17:15:38 +02:00
|
|
|
&search.DocumentMatch{
|
2014-04-17 22:55:53 +02:00
|
|
|
ID: "f",
|
|
|
|
Score: 9,
|
|
|
|
},
|
2014-09-01 17:15:38 +02:00
|
|
|
&search.DocumentMatch{
|
2014-04-17 22:55:53 +02:00
|
|
|
ID: "g",
|
|
|
|
Score: 11,
|
|
|
|
},
|
2014-09-01 17:15:38 +02:00
|
|
|
&search.DocumentMatch{
|
2014-04-17 22:55:53 +02:00
|
|
|
ID: "h",
|
|
|
|
Score: 9,
|
|
|
|
},
|
2014-09-01 17:15:38 +02:00
|
|
|
&search.DocumentMatch{
|
2014-04-17 22:55:53 +02:00
|
|
|
ID: "i",
|
|
|
|
Score: 11,
|
|
|
|
},
|
2014-09-01 17:15:38 +02:00
|
|
|
&search.DocumentMatch{
|
2014-04-17 22:55:53 +02:00
|
|
|
ID: "j",
|
|
|
|
Score: 11,
|
|
|
|
},
|
2014-09-01 17:15:38 +02:00
|
|
|
&search.DocumentMatch{
|
2014-04-17 22:55:53 +02:00
|
|
|
ID: "k",
|
|
|
|
Score: 11,
|
|
|
|
},
|
2014-09-01 17:15:38 +02:00
|
|
|
&search.DocumentMatch{
|
2014-04-17 22:55:53 +02:00
|
|
|
ID: "l",
|
|
|
|
Score: 99,
|
|
|
|
},
|
2014-09-01 17:15:38 +02:00
|
|
|
&search.DocumentMatch{
|
2014-04-17 22:55:53 +02:00
|
|
|
ID: "m",
|
|
|
|
Score: 11,
|
|
|
|
},
|
2014-09-01 17:15:38 +02:00
|
|
|
&search.DocumentMatch{
|
2014-04-17 22:55:53 +02:00
|
|
|
ID: "n",
|
|
|
|
Score: 11,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
collector := NewTopScorerCollector(10)
|
2016-03-02 22:55:02 +01:00
|
|
|
err := collector.Collect(context.Background(), searcher)
|
2015-04-07 20:59:35 +02:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2014-04-25 18:28:02 +02:00
|
|
|
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
}
|
2014-07-30 18:30:38 +02:00
|
|
|
|
|
|
|
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{
|
2014-09-01 17:15:38 +02:00
|
|
|
matches: search.DocumentMatchCollection{
|
|
|
|
&search.DocumentMatch{
|
2014-07-30 18:30:38 +02:00
|
|
|
ID: "a",
|
|
|
|
Score: 11,
|
|
|
|
},
|
2014-09-01 17:15:38 +02:00
|
|
|
&search.DocumentMatch{
|
2014-07-30 18:30:38 +02:00
|
|
|
ID: "b",
|
|
|
|
Score: 9.5,
|
|
|
|
},
|
2014-09-01 17:15:38 +02:00
|
|
|
&search.DocumentMatch{
|
2014-07-30 18:30:38 +02:00
|
|
|
ID: "c",
|
|
|
|
Score: 11,
|
|
|
|
},
|
2014-09-01 17:15:38 +02:00
|
|
|
&search.DocumentMatch{
|
2014-07-30 18:30:38 +02:00
|
|
|
ID: "d",
|
|
|
|
Score: 9,
|
|
|
|
},
|
2014-09-01 17:15:38 +02:00
|
|
|
&search.DocumentMatch{
|
2014-07-30 18:30:38 +02:00
|
|
|
ID: "e",
|
|
|
|
Score: 11,
|
|
|
|
},
|
2014-09-01 17:15:38 +02:00
|
|
|
&search.DocumentMatch{
|
2014-07-30 18:30:38 +02:00
|
|
|
ID: "f",
|
|
|
|
Score: 9,
|
|
|
|
},
|
2014-09-01 17:15:38 +02:00
|
|
|
&search.DocumentMatch{
|
2014-07-30 18:30:38 +02:00
|
|
|
ID: "g",
|
|
|
|
Score: 11,
|
|
|
|
},
|
2014-09-01 17:15:38 +02:00
|
|
|
&search.DocumentMatch{
|
2014-07-30 18:30:38 +02:00
|
|
|
ID: "h",
|
|
|
|
Score: 9,
|
|
|
|
},
|
2014-09-01 17:15:38 +02:00
|
|
|
&search.DocumentMatch{
|
2014-07-30 18:30:38 +02:00
|
|
|
ID: "i",
|
|
|
|
Score: 11,
|
|
|
|
},
|
2014-09-01 17:15:38 +02:00
|
|
|
&search.DocumentMatch{
|
2014-07-30 18:30:38 +02:00
|
|
|
ID: "j",
|
|
|
|
Score: 11,
|
|
|
|
},
|
2014-09-01 17:15:38 +02:00
|
|
|
&search.DocumentMatch{
|
2014-07-30 18:30:38 +02:00
|
|
|
ID: "k",
|
|
|
|
Score: 11,
|
|
|
|
},
|
2014-09-01 17:15:38 +02:00
|
|
|
&search.DocumentMatch{
|
2014-07-30 18:30:38 +02:00
|
|
|
ID: "l",
|
|
|
|
Score: 99,
|
|
|
|
},
|
2014-09-01 17:15:38 +02:00
|
|
|
&search.DocumentMatch{
|
2014-07-30 18:30:38 +02:00
|
|
|
ID: "m",
|
|
|
|
Score: 11,
|
|
|
|
},
|
2014-09-01 17:15:38 +02:00
|
|
|
&search.DocumentMatch{
|
2014-07-30 18:30:38 +02:00
|
|
|
ID: "n",
|
|
|
|
Score: 11,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
collector := NewTopScorerSkipCollector(10, 10)
|
2016-03-02 22:55:02 +01:00
|
|
|
err := collector.Collect(context.Background(), searcher)
|
2015-04-07 20:59:35 +02:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2014-07-30 18:30:38 +02:00
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
func BenchmarkTop10of100000Scores(b *testing.B) {
|
|
|
|
|
|
|
|
matches := make(search.DocumentMatchCollection, 0, 100000)
|
|
|
|
for i := 0; i < 100000; i++ {
|
|
|
|
matches = append(matches, &search.DocumentMatch{
|
|
|
|
ID: strconv.Itoa(i),
|
|
|
|
Score: rand.Float64(),
|
|
|
|
})
|
|
|
|
}
|
|
|
|
searcher := &stubSearcher{
|
|
|
|
matches: matches,
|
|
|
|
}
|
|
|
|
|
|
|
|
collector := NewTopScorerCollector(10)
|
|
|
|
b.ResetTimer()
|
|
|
|
|
2016-03-02 22:55:02 +01:00
|
|
|
err := collector.Collect(context.Background(), searcher)
|
2015-04-07 20:59:35 +02:00
|
|
|
if err != nil {
|
|
|
|
b.Fatal(err)
|
|
|
|
}
|
2015-03-06 18:59:44 +01:00
|
|
|
res := collector.Results()
|
|
|
|
for _, dm := range res {
|
|
|
|
b.Logf("%s - %f\n", dm.ID, dm.Score)
|
|
|
|
}
|
|
|
|
}
|