Use ES to get the related books
(book *Book, length int)
| 87 | |
| 88 | // Use ES to get the related books |
| 89 | func listByES(book *Book, length int) (ids []int) { |
| 90 | client := NewElasticSearchClient() |
| 91 | client.IsRelateSearch = true |
| 92 | client.Timeout = 3 * time.Second |
| 93 | listRows := 13 // 这里要填 13,因为有可能返回的id恰好是本书的id |
| 94 | keyWord := book.Label |
| 95 | if len(keyWord) == 0 { |
| 96 | keyWord = book.BookName |
| 97 | } |
| 98 | |
| 99 | res, err := client.Search(keyWord, 1, listRows, false) |
| 100 | if err != nil { |
| 101 | beego.Error(err.Error()) |
| 102 | return |
| 103 | } |
| 104 | |
| 105 | bookId := book.BookId |
| 106 | for _, item := range res.Hits.Hits { |
| 107 | if len(ids) >= length { |
| 108 | break |
| 109 | } |
| 110 | if item.Source.Id == bookId || len(ids) == listRows-1 { |
| 111 | continue |
| 112 | } |
| 113 | ids = append(ids, item.Source.Id) |
| 114 | } |
| 115 | |
| 116 | return ids |
| 117 | } |
| 118 | |
| 119 | // Get the related books directly from DB by SQL composed with Labels |
| 120 | func listByDBWithLabel(book *Book, length int) (ids []int) { |
no test coverage detected