Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit c2175ea

Browse files
committed
搜索优化
1 parent 7ad5c3e commit c2175ea

File tree

5 files changed

+57
-22
lines changed

5 files changed

+57
-22
lines changed

config/solr_schema.xml

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -143,9 +143,10 @@
143143
<field name="likenum" type="int" indexed="true" stored="true" />
144144
<field name="nid" type="int" indexed="false" stored="true" />
145145
<field name="lastreplyuid" type="int" indexed="false" stored="true" />
146-
<field name="lastreplytime" type="string" indexed="false" stored="true" />
146+
<field name="lastreplytime" type="string" indexed="true" stored="true" />
147147
<field name="top" type="int" indexed="true" stored="true" />
148-
<field name="updated_at" type="string" indexed="false" stored="true" />
148+
<field name="updated_at" type="string" indexed="true" stored="true" />
149+
<field name="sort_time" type="string" indexed="true" stored="false" />
149150

150151
<!-- catchall field, containing all other searchable text fields (implemented
151152
via copyField further on in this schema -->

src/logic/searcher.go

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -36,10 +36,10 @@ var DefaultSearcher = SearcherLogic{maxRows: 100, engineUrl: config.ConfigFile.M
3636
// 准备索引数据,post 给 solr
3737
// isAll: 是否全量
3838
func (self SearcherLogic) Indexing(isAll bool) {
39+
go self.IndexingOpenProject(isAll)
40+
go self.IndexingTopic(isAll)
41+
go self.IndexingResource(isAll)
3942
self.IndexingArticle(isAll)
40-
self.IndexingTopic(isAll)
41-
self.IndexingResource(isAll)
42-
self.IndexingOpenProject(isAll)
4343
}
4444

4545
// IndexingArticle 索引博文
@@ -99,7 +99,7 @@ func (self SearcherLogic) IndexingTopic(isAll bool) {
9999

100100
var (
101101
topicList []*model.Topic
102-
topicExList map[int]*model.TopicEx
102+
topicExList map[int]*model.TopicUpEx
103103

104104
err error
105105
)
@@ -108,7 +108,7 @@ func (self SearcherLogic) IndexingTopic(isAll bool) {
108108
id := 0
109109
for {
110110
topicList = make([]*model.Topic, 0)
111-
topicExList = make(map[int]*model.TopicEx)
111+
topicExList = make(map[int]*model.TopicUpEx)
112112

113113
err = MasterDB.Where("tid>?", id).OrderBy("tid ASC").Limit(self.maxRows).Find(&topicList)
114114
if err != nil {
@@ -375,14 +375,18 @@ func (this *SearcherLogic) DoSearch(q, field string, start, rows int) (*model.Re
375375
}
376376

377377
// DoSearch 搜索
378-
func (this *SearcherLogic) SearchByField(field, value string, start, rows int) (*model.ResponseBody, error) {
378+
func (this *SearcherLogic) SearchByField(field, value string, start, rows int, sorts ...string) (*model.ResponseBody, error) {
379379
selectUrl := this.engineUrl + "/select?"
380380

381+
sort := "sort_time desc,cmtnum desc,viewnum desc"
382+
if len(sorts) > 0 {
383+
sort = sorts[0]
384+
}
381385
var values = url.Values{
382386
"wt": []string{"json"},
383387
"start": []string{strconv.Itoa(start)},
384388
"rows": []string{strconv.Itoa(rows)},
385-
"sort": []string{"viewnum desc"},
389+
"sort": []string{sort},
386390
"fl": []string{"objid,objtype,title,author,uid,pub_time,tags,viewnum,cmtnum,likenum,lastreplyuid,lastreplytime,updated_at,top,nid"},
387391
}
388392

src/model/document.go

Lines changed: 38 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,9 @@ type Document struct {
3434

3535
UpdatedAt OftenTime `json:"updated_at"`
3636

37+
// 排序用的时间
38+
SortTime OftenTime `json:"sort_time"`
39+
3740
Top uint8 `json:"top"`
3841

3942
Nid int `json:"nid"`
@@ -49,14 +52,21 @@ func NewDocument(object interface{}, objectExt interface{}) *Document {
4952
viewnum, cmtnum, likenum := 0, 0, 0
5053
if objectExt != nil {
5154
// 传递过来的是一个 *TopicEx 对象,类型是有的,即时值是 nil,这里也和 nil 是不等
52-
topicEx := objectExt.(*TopicEx)
55+
topicEx := objectExt.(*TopicUpEx)
5356
if topicEx != nil {
5457
viewnum = topicEx.View
5558
cmtnum = topicEx.Reply
5659
likenum = topicEx.Like
5760
}
5861
}
5962

63+
var sortTime = NewOftenTime()
64+
if objdoc.Lastreplyuid != 0 {
65+
sortTime = objdoc.Lastreplytime
66+
} else {
67+
sortTime = objdoc.Ctime
68+
}
69+
6070
userLogin := &UserLogin{}
6171
db.MasterDB.Id(objdoc.Uid).Get(userLogin)
6272
document = &Document{
@@ -79,6 +89,7 @@ func NewDocument(object interface{}, objectExt interface{}) *Document {
7989
Lastreplyuid: objdoc.Lastreplyuid,
8090
Lastreplytime: objdoc.Lastreplytime,
8191
UpdatedAt: objdoc.Mtime,
92+
SortTime: sortTime,
8293
}
8394
case *Article:
8495
var uid int
@@ -87,6 +98,14 @@ func NewDocument(object interface{}, objectExt interface{}) *Document {
8798
db.MasterDB.Where("username=?", objdoc.AuthorTxt).Get(userLogin)
8899
uid = userLogin.Uid
89100
}
101+
102+
var sortTime = NewOftenTime()
103+
if objdoc.Lastreplyuid != 0 {
104+
sortTime = objdoc.Lastreplytime
105+
} else {
106+
sortTime = objdoc.Ctime
107+
}
108+
90109
document = &Document{
91110
Id: fmt.Sprintf("%d%d", TypeArticle, objdoc.Id),
92111
Objid: objdoc.Id,
@@ -105,6 +124,7 @@ func NewDocument(object interface{}, objectExt interface{}) *Document {
105124
Lastreplyuid: objdoc.Lastreplyuid,
106125
Lastreplytime: objdoc.Lastreplytime,
107126
UpdatedAt: objdoc.Mtime,
127+
SortTime: sortTime,
108128
}
109129
case *Resource:
110130
viewnum, cmtnum, likenum := 0, 0, 0
@@ -116,6 +136,13 @@ func NewDocument(object interface{}, objectExt interface{}) *Document {
116136
}
117137
}
118138

139+
var sortTime = NewOftenTime()
140+
if objdoc.Lastreplyuid != 0 {
141+
sortTime = objdoc.Lastreplytime
142+
} else {
143+
sortTime = objdoc.Ctime
144+
}
145+
119146
userLogin := &UserLogin{}
120147
db.MasterDB.Id(objdoc.Uid).Get(userLogin)
121148
document = &Document{
@@ -136,10 +163,19 @@ func NewDocument(object interface{}, objectExt interface{}) *Document {
136163
Lastreplyuid: objdoc.Lastreplyuid,
137164
Lastreplytime: objdoc.Lastreplytime,
138165
UpdatedAt: objdoc.Mtime,
166+
SortTime: sortTime,
139167
}
140168
case *OpenProject:
141169
userLogin := &UserLogin{}
142170
db.MasterDB.Where("username=?", objdoc.Username).Get(userLogin)
171+
172+
var sortTime = NewOftenTime()
173+
if objdoc.Lastreplyuid != 0 {
174+
sortTime = objdoc.Lastreplytime
175+
} else {
176+
sortTime = objdoc.Ctime
177+
}
178+
143179
document = &Document{
144180
Id: fmt.Sprintf("%d%d", TypeProject, objdoc.Id),
145181
Objid: objdoc.Id,
@@ -158,6 +194,7 @@ func NewDocument(object interface{}, objectExt interface{}) *Document {
158194
Lastreplyuid: objdoc.Lastreplyuid,
159195
Lastreplytime: objdoc.Lastreplytime,
160196
UpdatedAt: objdoc.Mtime,
197+
SortTime: sortTime,
161198
}
162199
}
163200

src/model/topic.go

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -62,15 +62,8 @@ func (*TopicEx) TableName() string {
6262

6363
// 社区主题扩展(计数)信息,用于 incr 更新
6464
type TopicUpEx struct {
65-
Tid int `json:"-" xorm:"pk"`
66-
View int `json:"view"`
67-
Reply int `json:"reply"`
68-
Like int `json:"like"`
69-
Mtime time.Time `json:"mtime" xorm:"<-"`
70-
}
71-
72-
func (*TopicUpEx) TableName() string {
73-
return "topics_ex"
65+
Tid int `json:"-" xorm:"pk"`
66+
*TopicEx
7467
}
7568

7669
type TopicInfo struct {

template/new_index.html

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@
9898
{{if .lastreplyuid}}
9999
<span title="{{.lastreplytime}}" class="timeago"></span>&nbsp;•&nbsp;最后回复来自 <strong><a href="/user/{{.lastreplyusername}}" class="noul">{{.lastreplyusername}}</a></strong>
100100
{{else}}
101-
<span title="{{.ctime}}" class="timeago"></span>发布
101+
<span title="{{.ctime}}" class="timeago"></span> 发布
102102
{{end}}
103103
</td>
104104
{{if .reply}}
@@ -149,7 +149,7 @@
149149
{{if .Lastreplyuid}}
150150
<span title="{{.Lastreplytime}}" class="timeago"></span>&nbsp;•&nbsp;最后回复来自 <strong><a href="/user/{{.LastReplyUser.Username}}" class="noul">{{.LastReplyUser.Username}}</a></strong>
151151
{{else}}
152-
<span title="{{.Ctime}}" class="timeago"></span>发布
152+
<span title="{{.Ctime}}" class="timeago"></span> 发布
153153
{{end}}
154154
</td>
155155
{{if .Cmtnum}}
@@ -215,7 +215,7 @@
215215
{{$user := index $.users .Lastreplyuid}}
216216
<span title="{{.Lastreplytime}}" class="timeago"></span>&nbsp;•&nbsp; 最后回复来自 <strong><a href="/user/{{$user.Username}}" class="noul">{{$user.Username}}</a></strong>
217217
{{else}}
218-
<span title="{{.PubTime}}" class="timeago"></span>发布
218+
<span title="{{.PubTime}}" class="timeago"></span> 发布
219219
{{end}}
220220
</span>
221221
</td>

0 commit comments

Comments
 (0)