|
@@ -1,9 +1,11 @@
|
|
|
package core
|
|
package core
|
|
|
|
|
|
|
|
import (
|
|
import (
|
|
|
|
|
+ "testing"
|
|
|
|
|
+
|
|
|
|
|
+ "github.com/faberliu/wukong/engine"
|
|
|
"github.com/huichen/wukong/types"
|
|
"github.com/huichen/wukong/types"
|
|
|
"github.com/huichen/wukong/utils"
|
|
"github.com/huichen/wukong/utils"
|
|
|
- "testing"
|
|
|
|
|
)
|
|
)
|
|
|
|
|
|
|
|
func TestAddKeywords(t *testing.T) {
|
|
func TestAddKeywords(t *testing.T) {
|
|
@@ -370,3 +372,46 @@ func TestLookupWithLocations(t *testing.T) {
|
|
|
docs, _ := indexer.Lookup([]string{"token2", "token3"}, []string{}, nil, false)
|
|
docs, _ := indexer.Lookup([]string{"token2", "token3"}, []string{}, nil, false)
|
|
|
utils.Expect(t, "[[0 21] [28]]", docs[0].TokenLocations)
|
|
utils.Expect(t, "[[0 21] [28]]", docs[0].TokenLocations)
|
|
|
}
|
|
}
|
|
|
|
|
+
|
|
|
|
|
+func TestLookupWithLocations1(t *testing.T) {
|
|
|
|
|
+
|
|
|
|
|
+ type Data struct {
|
|
|
|
|
+ Id int
|
|
|
|
|
+ Content string
|
|
|
|
|
+ Labels []string
|
|
|
|
|
+ }
|
|
|
|
|
+ searcher := engine.Engine{}
|
|
|
|
|
+ datas := make([]Data, 0)
|
|
|
|
|
+
|
|
|
|
|
+ data0 := Data{Id: 0, Content: "此次百度收购将成中国互联网最大并购", Labels: []string{"百度", "中国"}}
|
|
|
|
|
+ datas = append(datas, data0)
|
|
|
|
|
+
|
|
|
|
|
+ data1 := Data{Id: 1, Content: "百度宣布拟全资收购91无线业务", Labels: []string{"百度"}}
|
|
|
|
|
+ datas = append(datas, data1)
|
|
|
|
|
+
|
|
|
|
|
+ data2 := Data{Id: 2, Content: "百度是中国最大的搜索引擎", Labels: []string{"百度"}}
|
|
|
|
|
+ datas = append(datas, data2)
|
|
|
|
|
+
|
|
|
|
|
+ data3 := Data{Id: 3, Content: "百度在研制无人汽车", Labels: []string{"百度"}}
|
|
|
|
|
+ datas = append(datas, data3)
|
|
|
|
|
+
|
|
|
|
|
+ data4 := Data{Id: 4, Content: "BAT是中国互联网三巨头", Labels: []string{"百度"}}
|
|
|
|
|
+ datas = append(datas, data4)
|
|
|
|
|
+
|
|
|
|
|
+ // 初始化
|
|
|
|
|
+ searcher.Init(types.EngineInitOptions{
|
|
|
|
|
+ SegmenterDictionaries: "../data/dictionary.txt",
|
|
|
|
|
+ IndexerInitOptions: &types.IndexerInitOptions{
|
|
|
|
|
+ IndexType: types.LocationsIndex,
|
|
|
|
|
+ },
|
|
|
|
|
+ })
|
|
|
|
|
+ defer searcher.Close()
|
|
|
|
|
+ for _, data := range datas {
|
|
|
|
|
+ searcher.IndexDocument(uint64(data.Id), types.DocumentIndexData{Content: data.Content, Labels: data.Labels})
|
|
|
|
|
+ }
|
|
|
|
|
+ searcher.FlushIndex()
|
|
|
|
|
+ res := searcher.Search(types.SearchRequest{Text: "百度"})
|
|
|
|
|
+ if res.NumDocs != 5 {
|
|
|
|
|
+ t.Errorf("期待的搜索结果个数=\"%d\", 实际=\"%d\"", 5, res.NumDocs)
|
|
|
|
|
+ }
|
|
|
|
|
+}
|