new: Added post search module

This commit is contained in:
2024-12-17 16:15:51 +08:00
parent 474bf3e6bb
commit 9386306e7e
26 changed files with 590608 additions and 11 deletions

View File

@@ -10,6 +10,7 @@ import (
"github.com/88250/lute"
"github.com/gin-gonic/gin"
"github.com/huichen/wukong/engine"
)
type MApp struct {
@@ -17,10 +18,12 @@ type MApp struct {
Port int
Config *config.MConfig
lute *lute.Lute
engine *gin.Engine
lute *lute.Lute
engine *gin.Engine
searcher *engine.Engine
Posts []*model.MPost
Posts []*model.MPost
IndexedPosts map[uint64]*model.MPost
Tags map[string]string
TagsCount map[string]int
@@ -61,6 +64,8 @@ func NewMApp(cfg *config.MConfig) *MApp {
Port: cfg.Port,
Config: cfg,
IndexedPosts: make(map[uint64]*model.MPost),
Tags: make(map[string]string),
TagsCount: make(map[string]int),

View File

@@ -2,6 +2,8 @@ package mApp
import (
"encoding/json"
"fmt"
"github.com/huichen/wukong/types"
"html/template"
"io"
"net/http"
@@ -174,8 +176,8 @@ func (ma *MApp) TagHandler(ctx *gin.Context) {
"all_page": allPage,
},
"tagged_post": gin.H{
"title": fmt.Sprintf("%s - %s", ma.Config.MSite.Post.Tag.Title, tagName),
"posts": taggedPosts,
"tag_name": tagName,
"tag_hash": tagHash,
"tag_list": string(tagListJson),
},
@@ -253,8 +255,8 @@ func (ma *MApp) CategoryHandler(ctx *gin.Context) {
"all_page": allPage,
},
"categorized_post": gin.H{
"title": fmt.Sprintf("%s - %s", ma.Config.MSite.Post.Category.Title, categoryName),
"posts": categorizedPosts,
"category_name": categoryName,
"category_hash": categoryHash,
"tag_list": string(tagListJson),
},
@@ -330,6 +332,75 @@ func (ma *MApp) ArchiveHandler(ctx *gin.Context) {
ctx.HTML(http.StatusOK, "archive.html", resData)
}
func (ma *MApp) SearchHandler(ctx *gin.Context) {
keyword := ctx.DefaultQuery("keyword", "")
if keyword == "" || len(ma.Posts) <= 1 {
ctx.Redirect(http.StatusFound, "/archive")
return
}
searchResult := ma.searcher.Search(types.SearchRequest{Text: keyword})
searchPosts := searchResult.Docs
page, _ := strconv.Atoi(ctx.DefaultQuery("page", "1"))
size := ma.Config.MSite.Post.Search.Number
var prePage, curPage, nxtPage, allPage int
allPage = (len(searchPosts) + size - 1) / size
if allPage > 0 {
if page <= 0 {
curPage = 1
} else if page > allPage {
curPage = allPage
} else {
curPage = page
}
} else {
curPage = 0
}
prePage = curPage - 1
nxtPage = curPage + 1
if prePage <= 0 {
prePage = curPage
}
if nxtPage > allPage {
nxtPage = allPage
}
var resultPosts []model.MPost
for _, searchPost := range searchPosts {
resultPosts = append(resultPosts, *ma.IndexedPosts[searchPost.DocId])
}
resData := gin.H{
"site_info": gin.H{
"logo": ma.Config.MSite.Info.Logo,
"title": ma.Config.MSite.Info.Title,
"author": ma.Config.MSite.Info.Author,
"language": ma.Config.MSite.Info.Language,
"copyright": template.HTML(ma.Config.MSite.Info.Copyright),
},
"menu": ma.Config.MSite.Menu,
"page_info": gin.H{
"pre_page": prePage,
"cur_page": curPage,
"nxt_page": nxtPage,
"all_page": allPage,
},
"search_post": gin.H{
"title": fmt.Sprintf("%s - %s", ma.Config.MSite.Post.Search.Title, keyword),
"posts": resultPosts,
"keyword": keyword,
},
}
ctx.HTML(http.StatusOK, "search.html", resData)
}
func (ma *MApp) UpdateBlogHandler(ctx *gin.Context) {
var err error
ma.resetStorage()
@@ -346,5 +417,8 @@ func (ma *MApp) UpdateBlogHandler(ctx *gin.Context) {
return
}
// parse post index
ma.loadPostIndex()
ctx.JSON(http.StatusOK, gin.H{"msg": "ok"})
}

View File

@@ -49,7 +49,7 @@ func (ma *MApp) loadMarkdownFiles() error {
func (ma *MApp) parseMarkdowns() error {
htmlPath := DST
for _, file := range ma.SrcFiles {
for index, file := range ma.SrcFiles {
// read markdown file
_mdFile, err := os.Open(file.Path)
if err != nil {
@@ -126,7 +126,11 @@ func (ma *MApp) parseMarkdowns() error {
post.Tags = nil
post.Categories = nil
// set post index
post.Index = uint64(index)
ma.Posts = append(ma.Posts, &post)
ma.IndexedPosts[post.Index] = &post
}
// sort Posts by date

View File

@@ -2,6 +2,7 @@ package mApp
func (ma *MApp) loadRoutes() {
ma.engine.GET("/", ma.IndexHandler)
ma.engine.GET("/search", ma.SearchHandler)
ma.engine.GET("/archive", ma.ArchiveHandler)
ma.engine.GET("/post/:hash", ma.PostHandler)
ma.engine.GET("/tag/:hash", ma.TagHandler)

32
internal/mApp/mSearch.go Normal file
View File

@@ -0,0 +1,32 @@
package mApp
import (
"io"
"os"
"github.com/huichen/wukong/engine"
"github.com/huichen/wukong/types"
)
func (ma *MApp) loadPostIndex() {
ma.searcher = &engine.Engine{}
ma.searcher.Init(types.EngineInitOptions{
UsePersistentStorage: true,
PersistentStorageFolder: "tmp",
StopTokenFile: "data/stop_tokens.txt",
SegmenterDictionaries: "data/dictionary.txt",
IndexerInitOptions: &types.IndexerInitOptions{
IndexType: types.LocationsIndex,
},
})
for _, post := range ma.Posts {
postFile, _ := os.OpenFile(post.HtmlPath, os.O_RDONLY, 0666)
postData, _ := io.ReadAll(postFile)
ma.searcher.IndexDocument(post.Index, types.DocumentIndexData{Content: string(postData)}, false)
}
ma.searcher.FlushIndex()
}

View File

@@ -7,6 +7,7 @@ import (
// MPost post metadata
type MPost struct {
Index uint64 `yaml:"index" json:"index"`
Title string `yaml:"title" json:"title"`
Cover string `yaml:"cover" json:"cover"`
Date string `yaml:"date" json:"date"`