2014-04-20 15:10:59 +02:00
|
|
|
// Copyright (c) 2013 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-01 17:15:38 +02:00
|
|
|
package searchers
|
2014-04-20 15:10:59 +02:00
|
|
|
|
|
|
|
import (
|
2014-04-20 15:33:15 +02:00
|
|
|
"math"
|
2014-04-20 15:10:59 +02:00
|
|
|
"testing"
|
|
|
|
|
2014-08-28 21:38:57 +02:00
|
|
|
"github.com/blevesearch/bleve/document"
|
|
|
|
"github.com/blevesearch/bleve/index/store/inmem"
|
|
|
|
"github.com/blevesearch/bleve/index/upside_down"
|
2014-04-20 15:10:59 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestTermSearcher(t *testing.T) {
|
|
|
|
|
2014-07-30 18:30:38 +02:00
|
|
|
var queryTerm = "beer"
|
|
|
|
var queryField = "desc"
|
|
|
|
var queryBoost = 3.0
|
|
|
|
var queryExplain = true
|
2014-04-20 15:10:59 +02:00
|
|
|
|
2014-06-26 17:43:13 +02:00
|
|
|
inMemStore, _ := inmem.Open()
|
|
|
|
i := upside_down.NewUpsideDownCouch(inMemStore)
|
2014-04-20 15:10:59 +02:00
|
|
|
i.Update(&document.Document{
|
|
|
|
ID: "a",
|
2014-07-14 20:47:05 +02:00
|
|
|
Fields: []document.Field{
|
2014-08-19 14:58:26 +02:00
|
|
|
document.NewTextField("desc", []uint64{}, []byte("beer")),
|
2014-04-20 15:10:59 +02:00
|
|
|
},
|
|
|
|
})
|
|
|
|
i.Update(&document.Document{
|
|
|
|
ID: "b",
|
2014-07-14 20:47:05 +02:00
|
|
|
Fields: []document.Field{
|
2014-08-19 14:58:26 +02:00
|
|
|
document.NewTextField("desc", []uint64{}, []byte("beer")),
|
2014-04-20 15:10:59 +02:00
|
|
|
},
|
|
|
|
})
|
|
|
|
i.Update(&document.Document{
|
|
|
|
ID: "c",
|
2014-07-14 20:47:05 +02:00
|
|
|
Fields: []document.Field{
|
2014-08-19 14:58:26 +02:00
|
|
|
document.NewTextField("desc", []uint64{}, []byte("beer")),
|
2014-04-20 15:10:59 +02:00
|
|
|
},
|
|
|
|
})
|
2014-04-20 15:33:15 +02:00
|
|
|
i.Update(&document.Document{
|
|
|
|
ID: "d",
|
2014-07-14 20:47:05 +02:00
|
|
|
Fields: []document.Field{
|
2014-08-19 14:58:26 +02:00
|
|
|
document.NewTextField("desc", []uint64{}, []byte("beer")),
|
2014-04-20 15:33:15 +02:00
|
|
|
},
|
|
|
|
})
|
|
|
|
i.Update(&document.Document{
|
|
|
|
ID: "e",
|
2014-07-14 20:47:05 +02:00
|
|
|
Fields: []document.Field{
|
2014-08-19 14:58:26 +02:00
|
|
|
document.NewTextField("desc", []uint64{}, []byte("beer")),
|
2014-04-20 15:33:15 +02:00
|
|
|
},
|
|
|
|
})
|
|
|
|
i.Update(&document.Document{
|
|
|
|
ID: "f",
|
2014-07-14 20:47:05 +02:00
|
|
|
Fields: []document.Field{
|
2014-08-19 14:58:26 +02:00
|
|
|
document.NewTextField("desc", []uint64{}, []byte("beer")),
|
2014-04-20 15:33:15 +02:00
|
|
|
},
|
|
|
|
})
|
|
|
|
i.Update(&document.Document{
|
|
|
|
ID: "g",
|
2014-07-14 20:47:05 +02:00
|
|
|
Fields: []document.Field{
|
2014-08-19 14:58:26 +02:00
|
|
|
document.NewTextField("desc", []uint64{}, []byte("beer")),
|
2014-04-20 15:33:15 +02:00
|
|
|
},
|
|
|
|
})
|
|
|
|
i.Update(&document.Document{
|
|
|
|
ID: "h",
|
2014-07-14 20:47:05 +02:00
|
|
|
Fields: []document.Field{
|
2014-08-19 14:58:26 +02:00
|
|
|
document.NewTextField("desc", []uint64{}, []byte("beer")),
|
2014-04-20 15:33:15 +02:00
|
|
|
},
|
|
|
|
})
|
|
|
|
i.Update(&document.Document{
|
|
|
|
ID: "i",
|
2014-07-14 20:47:05 +02:00
|
|
|
Fields: []document.Field{
|
2014-08-19 14:58:26 +02:00
|
|
|
document.NewTextField("desc", []uint64{}, []byte("beer")),
|
2014-04-20 15:33:15 +02:00
|
|
|
},
|
|
|
|
})
|
|
|
|
i.Update(&document.Document{
|
|
|
|
ID: "j",
|
2014-07-14 20:47:05 +02:00
|
|
|
Fields: []document.Field{
|
2014-08-19 14:58:26 +02:00
|
|
|
document.NewTextField("title", []uint64{}, []byte("cat")),
|
2014-04-20 15:33:15 +02:00
|
|
|
},
|
|
|
|
})
|
2014-04-20 15:10:59 +02:00
|
|
|
|
2014-07-30 18:30:38 +02:00
|
|
|
searcher, err := NewTermSearcher(i, queryTerm, queryField, queryBoost, queryExplain)
|
2014-04-20 15:10:59 +02:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2014-04-20 15:33:15 +02:00
|
|
|
defer searcher.Close()
|
|
|
|
|
|
|
|
searcher.SetQueryNorm(2.0)
|
|
|
|
idf := 1.0 + math.Log(float64(i.DocCount())/float64(searcher.Count()+1.0))
|
|
|
|
expectedQueryWeight := 3 * idf * 3 * idf
|
|
|
|
if expectedQueryWeight != searcher.Weight() {
|
|
|
|
t.Errorf("expected weight %v got %v", expectedQueryWeight, searcher.Weight())
|
|
|
|
}
|
2014-04-20 15:10:59 +02:00
|
|
|
|
2014-04-20 15:33:15 +02:00
|
|
|
if searcher.Count() != 9 {
|
|
|
|
t.Errorf("expected count of 9, got %d", searcher.Count())
|
2014-04-20 15:10:59 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
docMatch, err := searcher.Next()
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("expected result, got %v", err)
|
|
|
|
}
|
|
|
|
if docMatch.ID != "a" {
|
|
|
|
t.Errorf("expected result ID to be 'a', got '%s", docMatch.ID)
|
|
|
|
}
|
|
|
|
docMatch, err = searcher.Advance("c")
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("expected result, got %v", err)
|
|
|
|
}
|
|
|
|
if docMatch.ID != "c" {
|
|
|
|
t.Errorf("expected result ID to be 'c' got '%s'", docMatch.ID)
|
|
|
|
}
|
|
|
|
|
|
|
|
// try advancing past end
|
2014-04-20 15:33:15 +02:00
|
|
|
docMatch, err = searcher.Advance("z")
|
2014-04-20 15:10:59 +02:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if docMatch != nil {
|
|
|
|
t.Errorf("expected nil, got %v", docMatch)
|
|
|
|
}
|
|
|
|
|
|
|
|
// try pushing next past end
|
|
|
|
docMatch, err = searcher.Next()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if docMatch != nil {
|
|
|
|
t.Errorf("expected nil, got %v", docMatch)
|
|
|
|
}
|
|
|
|
}
|