標籤:持久化 添加 自訂 搜尋 ice 排列 資料結構 fatal top
轉自:http://blog.codeg.cn/2016/02/02/wukong-source-code-reading/索引過程分析
下面我們來分析索引過程。
// 將文檔加入索引//// 輸入參數:// docId標識文檔編號,必須唯一//data見DocumentIndexData注釋//// 注意:// 1. 這個函數是安全執行緒的,請儘可能並發調用以提高索引速度// 2. 這個函數調用是非同步的,也就是說在函數返回時有可能文檔還沒有加入索引中,因此// 如果立刻調用Search可能無法查詢到這個文檔。強制重新整理索引請調用FlushIndex函數。func (engine *Engine) IndexDocument(docId uint64, data types.DocumentIndexData) {engine.internalIndexDocument(docId, data)hash := murmur.Murmur3([]byte(fmt.Sprint("%d", docId))) % uint32(engine.initOptions.PersistentStorageShards)if engine.initOptions.UsePersistentStorage {engine.persistentStorageIndexDocumentChannels[hash] <- persistentStorageIndexDocumentRequest{docId: docId, data: data}}}func (engine *Engine) internalIndexDocument(docId uint64, data types.DocumentIndexData) {if !engine.initialized {log.Fatal("必須先初始化引擎")}atomic.AddUint64(&engine.numIndexingRequests, 1)hash := murmur.Murmur3([]byte(fmt.Sprint("%d%s", docId, data.Content)))engine.segmenterChannel <- segmenterRequest{docId: docId, hash: hash, data: data}}
這裡需要注意的是,docId參數需要調用者從外部傳入,而不是在內部自己建立,這給搜尋引擎的實現者更大的自由。 將文檔交給分詞器處理,然後根據murmur3計算的hash值模PersistentStorageShards,選擇合適的shard寫入持久化儲存中。
索引過程分析:分詞協程處理過程
分詞器協程的邏輯代碼在這裡:segmenter_worker.go:func (engine *Engine) segmenterWorker()
分詞器協程的邏輯是一個死迴圈,不停的從channel engine.segmenterChannel中讀取資料,針對每一次讀取的資料:
- 計算
shard號
- 將文檔分詞
- 根據分詞結果,構造
indexerAddDocumentRequest 和 rankerAddDocRequest
- 將
indexerAddDocumentRequest投遞到channel engine.indexerAddDocumentChannels[shard]中
- 將
rankerAddDocRequest投遞到channel engine.rankerAddDocChannels[shard]中
補充一句:這裡shard號的計算過程如下:
// 從文本hash得到要分配到的shardfunc (engine *Engine) getShard(hash uint32) int {return int(hash - hash/uint32(engine.initOptions.NumShards)*uint32(engine.initOptions.NumShards))}
為什麼不是直接模數呢?
索引過程分析:索引器協程處理過程
首先介紹一下倒排索引表,這是搜尋引擎的核心資料結構。
// 索引器type Indexer struct {// 從搜尋鍵到文檔列表的反向索引// 加了讀寫鎖以保證讀寫安全tableLock struct {sync.RWMutextable map[string]*KeywordIndicesdocs map[uint64]bool}initOptions types.IndexerInitOptionsinitialized bool// 這實際上是總文檔數的一個近似numDocuments uint64// 所有被索引文本的總關鍵詞數totalTokenLength float32// 每個文檔的關鍵詞長度docTokenLengths map[uint64]float32}// 反向索引表的一行,收集了一個搜尋鍵出現的所有文檔,按照DocId從小到大排序。type KeywordIndices struct {// 下面的切片是否為空白,取決於初始化時IndexType的值docIds []uint64 // 全部類型都有frequencies []float32 // IndexType == FrequenciesIndexlocations [][]int // IndexType == LocationsIndex}
table map[string]*KeywordIndices這個是核心:一個關鍵詞,對應一個KeywordIndices結構。該結構的docIds欄位記錄了所有包含這個關鍵詞的文檔id。 如果 IndexType == FrequenciesIndex ,則同時記錄這個關鍵詞在該文檔中出現次數。 如果 IndexType == LocationsIndex ,則同時記錄這個關鍵詞在該文檔中出現的所有位置的起始位移。
下面是索引的主函數代碼:
func (engine *Engine) indexerAddDocumentWorker(shard int) {for {request := <-engine.indexerAddDocumentChannels[shard]engine.indexers[shard].AddDocument(request.document)atomic.AddUint64(&engine.numTokenIndexAdded,uint64(len(request.document.Keywords)))atomic.AddUint64(&engine.numDocumentsIndexed, 1)}}
其主要邏輯又封裝在func (indexer *Indexer) AddDocument(document *types.DocumentIndex)函數中實現。其邏輯如下:
- 將倒排索引表加鎖
- 更新文檔關鍵詞的長度加在一起的總和
- 尋找關鍵詞在倒排索引表中是否存在
- 如果不存在,則直接加入到
table map[string]*KeywordIndices中
- 如果存在
KeywordIndices,則使用二分尋找該關鍵詞對應的docId是否已經在KeywordIndices.docIds中存在。分兩種情況: 1) docId存在,則更新原有的資料結構。 2) docId不存在,則插入到KeywordIndices.docIds數組中,同時保持升序排列。
- 更新索引過的文章總數
索引過程分析:排序器協程處理過程
在新索引文檔的過程,排序器的主邏輯如下:
func (engine *Engine) rankerAddDocWorker(shard int) {for {request := <-engine.rankerAddDocChannels[shard]engine.rankers[shard].AddDoc(request.docId, request.fields)}}
進而調用下面的函數
// 給某個文檔添加評分欄位func (ranker *Ranker) AddDoc(docId uint64, fields interface{}) {if ranker.initialized == false {log.Fatal("排序器尚未初始化")}ranker.lock.Lock()ranker.lock.fields[docId] = fieldsranker.lock.docs[docId] = trueranker.lock.Unlock()}
上述函數非常簡單,只是將應用程式層自訂的資料加入到ranker中。
至此索引過程就完成了。簡單來講就是下面兩個過程:
- 將文檔分詞,得到一堆關鍵詞
- 將 關鍵詞->docId 的對應關係加入到全域的map中(實際上是分了多個shard)
悟空分詞的搜尋和排序源碼分析之——索引