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