结构更新
This commit is contained in:
@@ -1,9 +1,8 @@
|
||||
package search
|
||||
|
||||
// 程序退出时关闭索引(可选)
|
||||
func CloseIndex() error {
|
||||
if globalIndex != nil {
|
||||
return globalIndex.Close()
|
||||
if GlobalIndex != nil {
|
||||
return GlobalIndex.Close()
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
@@ -10,7 +10,7 @@ import (
|
||||
// InsertDocument 插入单条文档到索引
|
||||
func InsertDocument(c *gin.Context) {
|
||||
// 检查索引是否初始化
|
||||
if globalIndex == nil {
|
||||
if GlobalIndex == nil {
|
||||
c.JSON(500, gin.H{"error": "索引未初始化"})
|
||||
return
|
||||
}
|
||||
@@ -23,15 +23,15 @@ func InsertDocument(c *gin.Context) {
|
||||
}
|
||||
|
||||
// 验证文档必填字段
|
||||
if doc.Title == "" && doc.Body == "" {
|
||||
c.JSON(400, gin.H{"error": "文档ID不能为空,且标题和内容不能同时为空"})
|
||||
if doc.Title == "" && doc.Content == "" && doc.AuthorId == "" {
|
||||
c.JSON(400, gin.H{"error": "用户ID不能为空,且标题和内容不能同时为空"})
|
||||
return
|
||||
}
|
||||
|
||||
docID := uuid.New().String()
|
||||
|
||||
// 将文档插入索引
|
||||
if err := globalIndex.Index(docID, doc); err != nil {
|
||||
if err := GlobalIndex.Index(docID, doc); err != nil {
|
||||
log.Printf("插入文档失败: %v", err)
|
||||
c.JSON(500, gin.H{"error": "插入文档到索引失败: " + err.Error()})
|
||||
return
|
||||
|
||||
@@ -8,10 +8,10 @@ import (
|
||||
)
|
||||
|
||||
// 全局索引变量,避免重复创建
|
||||
var globalIndex bleve.Index
|
||||
var GlobalIndex bleve.Index
|
||||
|
||||
func DataSearch(c *gin.Context) {
|
||||
if globalIndex == nil {
|
||||
if GlobalIndex == nil {
|
||||
c.JSON(500, gin.H{"error": "索引未初始化,请先调用InitIndex()"})
|
||||
return
|
||||
}
|
||||
@@ -22,13 +22,13 @@ func DataSearch(c *gin.Context) {
|
||||
return
|
||||
}
|
||||
|
||||
query := bleve.NewQueryStringQuery(`title:"` + keyword + `" OR body:"` + keyword + `"`)
|
||||
query := bleve.NewQueryStringQuery(`title:"` + keyword + `" OR content:"` + keyword + `"`)
|
||||
searchRequest := bleve.NewSearchRequest(query)
|
||||
|
||||
// 只返回必要字段减少资源消耗
|
||||
searchRequest.Fields = []string{"title", "body"}
|
||||
searchRequest.Fields = []string{"title", "content"}
|
||||
|
||||
searchResult, err := globalIndex.Search(searchRequest)
|
||||
searchResult, err := GlobalIndex.Search(searchRequest)
|
||||
if err != nil {
|
||||
c.JSON(500, gin.H{"error": "搜索失败: " + err.Error()})
|
||||
return
|
||||
@@ -53,11 +53,11 @@ func DataSearch(c *gin.Context) {
|
||||
log.Printf("文档 %s 缺少title字段", hit.ID)
|
||||
}
|
||||
|
||||
if body, ok := hit.Fields["body"].(string); ok {
|
||||
doc.Body = body
|
||||
if content, ok := hit.Fields["content"].(string); ok {
|
||||
doc.Content = content
|
||||
hasField = true
|
||||
} else {
|
||||
log.Printf("文档 %s 缺少body字段", hit.ID)
|
||||
log.Printf("文档 %s 缺少content字段", hit.ID)
|
||||
}
|
||||
|
||||
// 至少有一个字段存在才包含结果
|
||||
|
||||
@@ -1,10 +1,11 @@
|
||||
package search
|
||||
package bleveInit
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"github.com/blevesearch/bleve/v2"
|
||||
"log"
|
||||
"os"
|
||||
"toutoukan/controllers/search"
|
||||
)
|
||||
|
||||
// 初始化索引(程序启动时调用一次)
|
||||
@@ -19,7 +20,7 @@ func InitIndex() error {
|
||||
|
||||
if exists {
|
||||
// 打开已有索引
|
||||
globalIndex, err = bleve.Open("data")
|
||||
search.GlobalIndex, err = bleve.Open("data")
|
||||
if err != nil {
|
||||
return fmt.Errorf("打开索引失败: %v", err)
|
||||
}
|
||||
@@ -27,7 +28,7 @@ func InitIndex() error {
|
||||
} else {
|
||||
// 创建新索引
|
||||
mapping := bleve.NewIndexMapping()
|
||||
globalIndex, err = bleve.New("data", mapping)
|
||||
search.GlobalIndex, err = bleve.New("data", mapping)
|
||||
if err != nil {
|
||||
return fmt.Errorf("创建索引失败: %v", err)
|
||||
}
|
||||
3
main.go
3
main.go
@@ -4,13 +4,14 @@ import (
|
||||
"strconv"
|
||||
"toutoukan/config"
|
||||
"toutoukan/controllers/search"
|
||||
"toutoukan/init/bleveInit"
|
||||
"toutoukan/init/databaseInit"
|
||||
"toutoukan/init/redisInit"
|
||||
"toutoukan/router"
|
||||
)
|
||||
|
||||
func main() {
|
||||
search.InitIndex()
|
||||
bleveInit.InitIndex()
|
||||
databaseInit.DbInit()
|
||||
redisInit.RedisInit()
|
||||
if err := config.Goinit(); err != nil {
|
||||
|
||||
@@ -6,7 +6,7 @@ type Document struct {
|
||||
Title string `json:"title"`
|
||||
Content string `json:"content"`
|
||||
AuthorId string `json:"author_id"`
|
||||
Status string `json:"status"`
|
||||
Status int `json:"status"`
|
||||
TotalVotes int `json:"total_votes"`
|
||||
IsMultiple int `json:"is_multiple"`
|
||||
MaxChoices int `json:"max_choices"`
|
||||
|
||||
Reference in New Issue
Block a user