搜索结果页
()
| 29 | |
| 30 | // 搜索结果页 |
| 31 | func (this *SearchController) Result() { |
| 32 | |
| 33 | totalRows := 0 |
| 34 | |
| 35 | var ids []int |
| 36 | |
| 37 | wd := this.GetString("wd") |
| 38 | if wd == "" { |
| 39 | this.Redirect(beego.URLFor("SearchController.Search"), 302) |
| 40 | return |
| 41 | } |
| 42 | |
| 43 | now := time.Now() |
| 44 | |
| 45 | tab := this.GetString("tab", models.GetOptionValue("DEFAULT_SEARCH", "book")) |
| 46 | isSearchDoc := false |
| 47 | if tab == "doc" { |
| 48 | isSearchDoc = true |
| 49 | } |
| 50 | |
| 51 | page, _ := this.GetInt("page", 1) |
| 52 | size := 10 |
| 53 | |
| 54 | if page < 1 { |
| 55 | page = 1 |
| 56 | } |
| 57 | |
| 58 | client := models.NewElasticSearchClient() |
| 59 | |
| 60 | if client.On { // elasticsearch 进行全文搜索 |
| 61 | result, err := models.NewElasticSearchClient().Search(wd, page, size, isSearchDoc) |
| 62 | if err != nil { |
| 63 | beego.Error(err.Error()) |
| 64 | } else { // 搜索结果处理 |
| 65 | totalRows = result.Hits.Total |
| 66 | for _, item := range result.Hits.Hits { |
| 67 | ids = append(ids, item.Source.Id) |
| 68 | } |
| 69 | } |
| 70 | } else { //MySQL like 查询 |
| 71 | if isSearchDoc { //搜索文档 |
| 72 | docs, count, err := models.NewDocumentSearchResult().SearchDocument(wd, 0, page, size) |
| 73 | totalRows = count |
| 74 | if err != nil { |
| 75 | beego.Error(err.Error()) |
| 76 | } else { |
| 77 | for _, doc := range docs { |
| 78 | ids = append(ids, doc.DocumentId) |
| 79 | } |
| 80 | } |
| 81 | } else { //搜索书籍 |
| 82 | books, count, err := models.NewBook().SearchBook(wd, page, size) |
| 83 | totalRows = count |
| 84 | if err != nil { |
| 85 | beego.Error(err.Error()) |
| 86 | } else { |
| 87 | for _, book := range books { |
| 88 | ids = append(ids, book.BookId) |
nothing calls this directly
no test coverage detected