引言
云灘公眾號(hào)搜索引擎是利用 Go中文分詞 和 bleve 構(gòu)建的衣盾。
本文將展示整個(gè)搜索引擎的構(gòu)建過(guò)程狂芋。
分析
http://mp.weixin.qq.com/s/sgc8IzjkbSikS2YIJdO1QA
隨便打開(kāi)一篇公眾號(hào)文章然后查看他的源代碼
我們可以通過(guò)正則表達(dá)式獲取這里的數(shù)據(jù)用于構(gòu)建唯一的 URL 地址。
這段內(nèi)容這是標(biāo)題枝秤,摘要和縮略圖
文章則放在 div#js_content
里面组底,于是可以通過(guò) goquery 來(lái)獲取文章的內(nèi)容糜工。
構(gòu)建文檔數(shù)據(jù)結(jié)構(gòu)
type Document struct {
ID string `json:"uri"`
Title string `json:"title,omitempty"`
Summary string `json:"summary,omitempty"`
Content string `json:"content,omitempty"`
Meta string `json:"meta,omitempty"`
CreatedAt int64 `json:"created_at,omitempty"`
}
-
Document.ID
為拼裝的鏈接地址 -
Document.Title
為標(biāo)題 -
Document.Summary
為摘要 -
Document.Content
為內(nèi)容 -
Document.Meta
為額外信息卖哎,如封面圖 -
Document.CreatedAt
為創(chuàng)建時(shí)間
創(chuàng)建 Mapping 并使用 sego 分詞
tokenizer
我自己構(gòu)建出來(lái)的分詞微服務(wù)
_mapping := bleve.NewIndexMapping()
_mapping.AddCustomTokenizer("sego",
map[string]interface{}{
"host": *tokenizerHost,
"type": tokenizer.Name,
}
)
_mapping.AddCustomAnalyzer("sego",
map[string]interface{}{
"type": custom.Name,
"char_filters": []string{
html.Name,
},
"tokenizer": "sego",
"token_filters": []string{
"possessive_en",
"to_lower",
"stop_en",
},
},
)
_mapping.DefaultAnalyzer = tokenizer.Name
創(chuàng)建索引并索引文章
index, err := bleve.New("weixin-search.bleve", _mapping)
doc := Document{....}
index.Index(doc.ID, doc)
搜索查詢(xún)
index, _ := bleve.Open("weixin-search.bleve")
query := bleve.NewQueryStringQuery("剽悍晨讀")
searchRequest := bleve.NewSearchRequest(query)
searchResult, _ := index.Search(searchRequest)
結(jié)語(yǔ)
到此為止搜索引擎基本完成鬼悠。
Demo: http://t.cn/RaphK4a
完整的代碼我會(huì)在合適的時(shí)候發(fā)布到 Github 上面,敬請(qǐng)期待亏娜。
需要使用的話(huà)焕窝,詳見(jiàn): http://www.reibang.com/p/221e056c9251
需要幫助的話(huà)掃微信
如果覺(jué)得文章好,對(duì)你有幫助就來(lái)關(guān)注我吧维贺。