1
1
package models
2
2
3
3
import (
4
- "fmt "
4
+ "encoding/json "
5
5
"strconv"
6
6
"strings"
7
7
"time"
@@ -22,22 +22,18 @@ func NewRelateBook() *RelateBook {
22
22
return & RelateBook {}
23
23
}
24
24
25
+ // Get the related books for a given book
25
26
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
+ }
26
31
27
32
length := 6
28
33
if len (limit ) > 0 && limit [0 ] > 0 {
29
34
length = limit [0 ]
30
35
}
31
36
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
-
41
37
var rb RelateBook
42
38
var ids []int
43
39
@@ -50,13 +46,13 @@ func (r *RelateBook) Lists(bookId int, limit ...int) (books []Book) {
50
46
fields := []string {"book_id" , "book_name" , "cover" , "identify" }
51
47
52
48
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 {
60
56
books , _ = bookModel .GetBooksById (ids , fields ... )
61
57
return
62
58
}
@@ -67,36 +63,65 @@ func (r *RelateBook) Lists(bookId int, limit ...int) (books []Book) {
67
63
return
68
64
}
69
65
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 ) {
70
90
client := NewElasticSearchClient ()
71
91
client .IsRelateSearch = true
72
92
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
76
96
}
77
- res , err := client .Search (wd , 1 , 12 , false )
97
+ res , err := client .Search (keyWord , 1 , 12 , false )
78
98
if err != nil {
79
99
beego .Error (err .Error ())
80
100
return
81
101
}
82
102
83
- var bookIds [] string
103
+ bookId := book . BookId
84
104
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
90
110
}
111
+ ids = append (ids , item .Source .Id )
91
112
}
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
100
122
}
123
+
124
+ bookModel := NewBook ()
125
+ ids , _ = bookModel .SearchBookByLabel (strings .Split (rawKeyWords , "," ), length , []int {book .BookId })
101
126
return
102
127
}
0 commit comments