Skip to content

Commit 6be1875

Browse files
committed
Merge branch 'dev' of github.com:TruthHun/BookStack into dev
2 parents 023cb4c + 50aa3bd commit 6be1875

File tree

2 files changed

+86
-35
lines changed

2 files changed

+86
-35
lines changed

models/book.go

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -657,3 +657,29 @@ func (n *Book) SearchBook(wd string, page, size int) (books []Book, cnt int, err
657657

658658
return
659659
}
660+
661+
// search books with labels
662+
func (b *Book) SearchBookByLabel(labels []string, limit int, excludeIds []int) (bookIds []int, err error) {
663+
bookIds = []int{}
664+
if len(labels) == 0 {
665+
return
666+
}
667+
668+
rawRegex := strings.Join(labels, "|")
669+
670+
excludeClause := ""
671+
if len(excludeIds) == 1 {
672+
excludeClause = fmt.Sprintf("book_id != %d AND", excludeIds[0])
673+
} else if len(excludeIds) > 1 {
674+
excludeVal := strings.Replace(strings.Trim(fmt.Sprint(excludeIds), "[]"), " ", ",", -1)
675+
excludeClause = fmt.Sprintf("book_id NOT IN (%s) AND", excludeVal)
676+
}
677+
678+
sql := fmt.Sprintf("SELECT book_id FROM md_books WHERE %v label REGEXP ? ORDER BY star DESC LIMIT ?", excludeClause)
679+
o := orm.NewOrm()
680+
_, err = o.Raw(sql, rawRegex, limit).QueryRows(&bookIds)
681+
if err != nil {
682+
logs.Error("failed to execute sql: %s, err: %s", sql, err.Error())
683+
}
684+
return
685+
}

models/releate_book.go

Lines changed: 60 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
package models
22

33
import (
4-
"fmt"
4+
"encoding/json"
55
"strconv"
66
"strings"
77
"time"
@@ -22,22 +22,18 @@ func NewRelateBook() *RelateBook {
2222
return &RelateBook{}
2323
}
2424

25+
// Get the related books for a given book
2526
func (r *RelateBook) Lists(bookId int, limit ...int) (books []Book) {
27+
day, _ := strconv.Atoi(GetOptionValue("RELATE_BOOK", "0"))
28+
if day <= 0 {
29+
return
30+
}
2631

2732
length := 6
2833
if len(limit) > 0 && limit[0] > 0 {
2934
length = limit[0]
3035
}
3136

32-
if GetOptionValue("ELASTICSEARCH_ON", "false") != "true" {
33-
return
34-
}
35-
36-
day, _ := strconv.Atoi(GetOptionValue("RELATE_BOOK", "0"))
37-
if day <= 0 {
38-
return
39-
}
40-
4137
var rb RelateBook
4238
var ids []int
4339

@@ -50,13 +46,13 @@ func (r *RelateBook) Lists(bookId int, limit ...int) (books []Book) {
5046
fields := []string{"book_id", "book_name", "cover", "identify"}
5147

5248
if rb.BookId > 0 && rb.Expire > now {
53-
if slice := strings.Split(rb.BookIds, ","); len(slice) > 0 {
54-
for _, item := range slice {
55-
id, _ := strconv.Atoi(item)
56-
if id > 0 && len(ids) < length {
57-
ids = append(ids, id)
58-
}
59-
}
49+
bookIds := rb.BookIds
50+
if !strings.HasPrefix(bookIds, "[") {
51+
bookIds = "[" + bookIds + "]"
52+
}
53+
54+
err := json.Unmarshal([]byte(bookIds), &ids)
55+
if err == nil && len(ids) > 0 {
6056
books, _ = bookModel.GetBooksById(ids, fields...)
6157
return
6258
}
@@ -67,36 +63,65 @@ func (r *RelateBook) Lists(bookId int, limit ...int) (books []Book) {
6763
return
6864
}
6965

66+
if GetOptionValue("ELASTICSEARCH_ON", "false") == "true" {
67+
ids = listByES(book, length)
68+
} else {
69+
ids = listByDBWithLabel(book, length)
70+
}
71+
72+
books, _ = bookModel.GetBooksById(ids, fields...)
73+
rb.BookId = bookId
74+
if ids == nil {
75+
ids = []int{}
76+
}
77+
relatedIdBytes, _ := json.Marshal(ids)
78+
rb.BookIds = string(relatedIdBytes)
79+
rb.Expire = now + day*24*3600
80+
if rb.Id > 0 {
81+
o.Update(&rb)
82+
} else {
83+
o.Insert(&rb)
84+
}
85+
return
86+
}
87+
88+
// Use ES to get the related books
89+
func listByES(book *Book, length int) (ids []int) {
7090
client := NewElasticSearchClient()
7191
client.IsRelateSearch = true
7292
client.Timeout = 1 * time.Second
73-
wd := book.Label
74-
if len(wd) == 0 {
75-
wd = book.BookName
93+
keyWord := book.Label
94+
if len(keyWord) == 0 {
95+
keyWord = book.BookName
7696
}
77-
res, err := client.Search(wd, 1, 12, false)
97+
res, err := client.Search(keyWord, 1, 12, false)
7898
if err != nil {
7999
beego.Error(err.Error())
80100
return
81101
}
82102

83-
var bookIds []string
103+
bookId := book.BookId
84104
for _, item := range res.Hits.Hits {
85-
if item.Source.Id != bookId {
86-
if len(ids) < length {
87-
ids = append(ids, item.Source.Id)
88-
}
89-
bookIds = append(bookIds, fmt.Sprint(item.Source.Id))
105+
if len(ids) >= length {
106+
break
107+
}
108+
if item.Source.Id == bookId {
109+
continue
90110
}
111+
ids = append(ids, item.Source.Id)
91112
}
92-
books, _ = bookModel.GetBooksById(ids, fields...)
93-
rb.BookId = bookId
94-
rb.BookIds = strings.Join(bookIds, ",")
95-
rb.Expire = now + day*24*3600
96-
if rb.Id > 0 {
97-
o.Update(&rb)
98-
} else {
99-
o.Insert(&rb)
113+
114+
return ids
115+
}
116+
117+
// Get the related books directly from DB by SQL composed with Labels
118+
func listByDBWithLabel(book *Book, length int) (ids []int) {
119+
rawKeyWords := book.Label
120+
if rawKeyWords == "" {
121+
return
100122
}
123+
124+
bookModel := NewBook()
125+
ids, _ = bookModel.SearchBookByLabel(strings.Split(rawKeyWords, ","), length, []int{book.BookId})
101126
return
102127
}

0 commit comments

Comments
 (0)