mirror of
https://github.com/wgh136/nysoure.git
synced 2025-12-16 15:51:14 +00:00
API for rebuilding search index
This commit is contained in:
@@ -6,12 +6,19 @@ import (
|
||||
"nysoure/server/dao"
|
||||
"nysoure/server/model"
|
||||
"nysoure/server/utils"
|
||||
"os"
|
||||
"strconv"
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
"github.com/blevesearch/bleve"
|
||||
)
|
||||
|
||||
var (
|
||||
index bleve.Index
|
||||
mu = sync.RWMutex{}
|
||||
)
|
||||
|
||||
type ResourceParams struct {
|
||||
Id uint
|
||||
Title string
|
||||
@@ -26,9 +33,9 @@ type ResourceCharacter struct {
|
||||
CV string
|
||||
}
|
||||
|
||||
var index bleve.Index
|
||||
|
||||
func AddResourceToIndex(r model.Resource) error {
|
||||
mu.RLock()
|
||||
defer mu.RUnlock()
|
||||
cs := make([]ResourceCharacter, 0, len(r.Characters))
|
||||
for _, c := range r.Characters {
|
||||
cs = append(cs, ResourceCharacter{
|
||||
@@ -96,6 +103,8 @@ func init() {
|
||||
}
|
||||
|
||||
func SearchResource(keyword string) ([]uint, error) {
|
||||
mu.RLock()
|
||||
defer mu.RUnlock()
|
||||
query := bleve.NewMatchQuery(keyword)
|
||||
searchRequest := bleve.NewSearchRequest(query)
|
||||
searchResults, err := index.Search(searchRequest)
|
||||
@@ -128,3 +137,17 @@ func IsStopWord(word string) bool {
|
||||
tokens := analyzer.Analyze([]byte(word))
|
||||
return len(tokens) == 0
|
||||
}
|
||||
|
||||
func RebuildSearchIndex() error {
|
||||
mu.Lock()
|
||||
defer mu.Unlock()
|
||||
err := index.Close()
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to close search index: %w", err)
|
||||
}
|
||||
err = os.Remove(utils.GetStoragePath() + "/resource_index.bleve")
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to remove search index: %w", err)
|
||||
}
|
||||
return createIndex()
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user