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

Function listByES

models/releate_book.go:89–117  ·  view source on GitHub ↗

Use ES to get the related books

(book *Book, length int)

Source from the content-addressed store, hash-verified

87
88// Use ES to get the related books
89func 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
120func listByDBWithLabel(book *Book, length int) (ids []int) {

Callers 1

ListsMethod · 0.85

Calls 3

SearchMethod · 0.95
NewElasticSearchClientFunction · 0.85
appendFunction · 0.50

Tested by

no test coverage detected