悟空(wukong)搜索引擎源代码阅读
最近为了了解搜索引擎的一些知识,阅读了wukong搜索的一些源码,在这里记录一下。项目地址在这里
整个的处理流程如下:
在项目中主要有一下几个目录:
目录名 | 作用 |
---|---|
core | |
data | |
docs | |
engines | |
examples | |
testdata | |
Types | |
utils |
示例代码如下:
package main
import (
"github.com/huichen/wukong/engine"
"github.com/huichen/wukong/types"
"log"
)
var (
// searcher是协程安全的
searcher = engine.Engine{}
)
func main() {
// 初始化
searcher.Init(types.EngineInitOptions{
SegmenterDictionaries: "./data/dictionary.txt"})
defer searcher.Close()
// 将文档加入索引
searcher.IndexDocument(0, types.DocumentIndexData{Content: "此次百度收购将成中国互联网最大并购"})
searcher.IndexDocument(1, types.DocumentIndexData{Content: "百度宣布拟全资收购91无线业务"})
searcher.IndexDocument(2, types.DocumentIndexData{Content: "百度是中国最大的搜索引擎"})
// 等待索引刷新完毕
searcher.FlushIndex()
// 搜索输出格式见types.SearchResponse结构体
res := searcher.Search(types.SearchRequest{Text:"百度中国"})
log.Printf("num=%d ", res.NumDocs)
for _, d := range res.Docs {
log.Printf("docId=%d", d.DocId)
log.Print("\tscore:", d.Scores)
log.Print("\tTokenLocations:", d.TokenLocations)
log.Print("\tTokenSnipp