引言
分析
随便打开一篇公众号文章然后查看他的源代码
我们可以通过正则表达式获取这里的数据用于构建唯一的 URL 地址。
这段内容这是标题,摘要和缩略图
构建文档数据结构
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
为标题 -
Document.Summary
为摘要 -
Document.Content
为内容 -
Document.Meta
为额外信息,如封面图 -
Document.CreatedAt
为创建时间
创建 Mapping 并使用 sego 分词
_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
创建索引并索引文章
index, err := bleve.New("weixin-search.bleve", _mapping)
doc := Document{....}
index.Index(doc.ID, doc)
搜索查询
index, _ := bleve.Open("weixin-search.bleve")
query := bleve.NewQueryStringQuery("剽悍晨读")
searchRequest := bleve.NewSearchRequest(query)
searchResult, _ := index.Search(searchRequest)
结语
需要帮助的话扫微信
如果觉得文章好,对你有帮助就来关注我吧。