书籍内搜索.
()
| 1420 | |
| 1421 | // 书籍内搜索. |
| 1422 | func (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 | // 文档历史列表. |
| 1476 | func (this *DocumentController) History() { |
nothing calls this directly
no test coverage detected