-
Notifications
You must be signed in to change notification settings - Fork 808
/
Copy pathSearchController.go
118 lines (102 loc) · 2.71 KB
/
SearchController.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
package controllers
import (
"fmt"
"strings"
"time"
"github.com/TruthHun/BookStack/conf"
"github.com/TruthHun/BookStack/utils"
"github.com/TruthHun/BookStack/models"
"github.com/astaxie/beego"
)
type SearchController struct {
BaseController
}
//搜索首页
func (this *SearchController) Search() {
if wd := strings.TrimSpace(this.GetString("wd")); wd != "" {
this.Redirect(beego.URLFor("LabelController.Index", ":key", wd), 302)
return
}
this.Data["SeoTitle"] = "搜索 - " + this.Sitename
this.Data["IsSearch"] = true
this.TplName = "search/search.html"
}
// 搜索结果页
func (this *SearchController) Result() {
totalRows := 0
var ids []int
wd := this.GetString("wd")
if wd == "" {
this.Redirect(beego.URLFor("SearchController.Search"), 302)
return
}
now := time.Now()
tab := this.GetString("tab", models.GetOptionValue("DEFAULT_SEARCH", "book"))
isSearchDoc := false
if tab == "doc" {
isSearchDoc = true
}
page, _ := this.GetInt("page", 1)
size := 10
if page < 1 {
page = 1
}
client := models.NewElasticSearchClient()
if client.On { // elasticsearch 进行全文搜索
result, err := models.NewElasticSearchClient().Search(wd, page, size, isSearchDoc)
if err != nil {
beego.Error(err.Error())
} else { // 搜索结果处理
totalRows = result.Hits.Total
for _, item := range result.Hits.Hits {
ids = append(ids, item.Source.Id)
}
}
} else { //MySQL like 查询
if isSearchDoc { //搜索文档
docs, count, err := models.NewDocumentSearchResult().SearchDocument(wd, 0, page, size)
totalRows = count
if err != nil {
beego.Error(err.Error())
} else {
for _, doc := range docs {
ids = append(ids, doc.DocumentId)
}
}
} else { //搜索书籍
books, count, err := models.NewBook().SearchBook(wd, page, size)
totalRows = count
if err != nil {
beego.Error(err.Error())
} else {
for _, book := range books {
ids = append(ids, book.BookId)
}
}
}
}
if len(ids) > 0 {
if isSearchDoc {
this.Data["Docs"], _ = models.NewDocumentSearchResult().GetDocsById(ids)
} else {
this.Data["Books"], _ = models.NewBook().GetBooksById(ids)
}
this.Data["Words"] = client.SegWords(wd)
}
this.Data["TotalRows"] = totalRows
if totalRows > size {
if totalRows > 1000 {
totalRows = 1000
}
urlSuffix := fmt.Sprintf("&tab=%v&wd=%v", tab, wd)
html := utils.NewPaginations(conf.RollPage, totalRows, size, page, beego.URLFor("SearchController.Result"), urlSuffix)
this.Data["PageHtml"] = html
} else {
this.Data["PageHtml"] = ""
}
this.Data["SpendTime"] = fmt.Sprintf("%.3f", time.Since(now).Seconds())
this.Data["Wd"] = wd
this.Data["Tab"] = tab
this.Data["IsSearch"] = true
this.TplName = "search/result.html"
}