结构更新
This commit is contained in:
@@ -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)
|
||||
}
|
||||
|
||||
// 至少有一个字段存在才包含结果
|
||||
|
||||
Reference in New Issue
Block a user