MCPcopy Index your code
hub / github.com/TruthHun/BookStack / Search

Method Search

controllers/DocumentController.go:1422–1473  ·  view source on GitHub ↗

书籍内搜索.

()

Source from the content-addressed store, hash-verified

1420
1421// 书籍内搜索.
1422func (this *DocumentController) Search() {
1423 identify := this.Ctx.Input.Param(":key")
1424 token := this.GetString("token")
1425 keyword := strings.TrimSpace(this.GetString("keyword"))
1426
1427 if identify == "" {
1428 this.JsonResult(6001, "参数错误")
1429 }
1430 if !this.EnableAnonymous && this.Member == nil {
1431 this.Redirect(beego.URLFor("AccountController.Login"), 302)
1432 return
1433 }
1434 bookResult := isReadable(identify, token, this)
1435
1436 client := models.NewElasticSearchClient()
1437 if client.On { // 全文搜索
1438 result, err := client.Search(keyword, 1, 10000, true, bookResult.BookId)
1439 if err != nil {
1440 beego.Error(err)
1441 this.JsonResult(6002, "搜索结果错误")
1442 }
1443
1444 var ids []int
1445 for _, item := range result.Hits.Hits {
1446 ids = append(ids, item.Source.Id)
1447 }
1448 docs, err := models.NewDocumentSearchResult().GetDocsById(ids, true)
1449 if err != nil {
1450 beego.Error(err)
1451 }
1452
1453 // 如果全文搜索查询不到结果,用 MySQL like 再查询一次
1454 if len(docs) == 0 {
1455 if docsMySQL, _, err := models.NewDocumentSearchResult().SearchDocument(keyword, bookResult.BookId, 1, 10000); err != nil {
1456 beego.Error(err)
1457 this.JsonResult(6002, "搜索结果错误")
1458 } else {
1459 this.JsonResult(0, client.SegWords(keyword), docsMySQL)
1460 }
1461 } else {
1462 this.JsonResult(0, client.SegWords(keyword), docs)
1463 }
1464
1465 } else {
1466 docs, _, err := models.NewDocumentSearchResult().SearchDocument(keyword, bookResult.BookId, 1, 10000)
1467 if err != nil {
1468 beego.Error(err)
1469 this.JsonResult(6002, "搜索结果错误")
1470 }
1471 this.JsonResult(0, keyword, docs)
1472 }
1473}
1474
1475// 文档历史列表.
1476func (this *DocumentController) History() {

Callers

nothing calls this directly

Calls 9

SearchMethod · 0.95
SegWordsMethod · 0.95
NewElasticSearchClientFunction · 0.92
NewDocumentSearchResultFunction · 0.92
isReadableFunction · 0.85
JsonResultMethod · 0.80
GetDocsByIdMethod · 0.80
SearchDocumentMethod · 0.80
appendFunction · 0.50

Tested by

no test coverage detected