Skip to content

Commit 21a9692

Browse files
committed
话题和资源加入搜索
1 parent e72b939 commit 21a9692

File tree

5 files changed

+187
-6
lines changed

5 files changed

+187
-6
lines changed

websites/code/studygolang/src/model/document.go

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,32 @@ func NewDocument(object interface{}, objectExt interface{}) *Document {
3434
var document *Document
3535
switch objdoc := object.(type) {
3636
case *Topic:
37+
viewnum, cmtnum, likenum := 0, 0, 0
38+
if objectExt != nil {
39+
// 传递过来的是一个 *TopicEx 对象,类型是有的,即时值是 nil,这里也和 nil 是不等
40+
topicEx := objectExt.(*TopicEx)
41+
if topicEx != nil {
42+
viewnum = topicEx.View
43+
cmtnum = topicEx.Reply
44+
likenum = topicEx.Like
45+
}
46+
}
47+
48+
userLogin := NewUserLogin()
49+
userLogin.Where("uid=?", objdoc.Uid).Find("username")
50+
document = &Document{
51+
Id: fmt.Sprintf("%d%d", TYPE_TOPIC, objdoc.Tid),
52+
Objid: objdoc.Tid,
53+
Objtype: TYPE_TOPIC,
54+
Title: objdoc.Title,
55+
Author: userLogin.Username,
56+
PubTime: objdoc.Ctime,
57+
Content: objdoc.Content,
58+
Tags: "",
59+
Viewnum: viewnum,
60+
Cmtnum: cmtnum,
61+
Likenum: likenum,
62+
}
3763
case *Article:
3864
document = &Document{
3965
Id: fmt.Sprintf("%d%d", TYPE_ARTICLE, objdoc.Id),
@@ -49,6 +75,30 @@ func NewDocument(object interface{}, objectExt interface{}) *Document {
4975
Likenum: objdoc.Likenum,
5076
}
5177
case *Resource:
78+
viewnum, cmtnum, likenum := 0, 0, 0
79+
if objectExt != nil {
80+
resourceEx := objectExt.(*ResourceEx)
81+
if resourceEx != nil {
82+
viewnum = resourceEx.Viewnum
83+
cmtnum = resourceEx.Cmtnum
84+
}
85+
}
86+
87+
userLogin := NewUserLogin()
88+
userLogin.Where("uid=?", objdoc.Uid).Find("username")
89+
document = &Document{
90+
Id: fmt.Sprintf("%d%d", TYPE_RESOURCE, objdoc.Id),
91+
Objid: objdoc.Id,
92+
Objtype: TYPE_RESOURCE,
93+
Title: objdoc.Title,
94+
Author: userLogin.Username,
95+
PubTime: objdoc.Ctime,
96+
Content: objdoc.Content,
97+
Tags: "",
98+
Viewnum: viewnum,
99+
Cmtnum: cmtnum,
100+
Likenum: likenum,
101+
}
52102
case *Wiki:
53103
}
54104

websites/code/studygolang/src/server/indexer/main.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ func main() {
2929
c := cron.New()
3030
// 构建 solr 需要的索引数据
3131
// 一天一次全量
32-
c.AddFunc("*/20 * * * * *", func() {
32+
c.AddFunc("0 */1 * * * *", func() {
3333
logger.Infoln("indexing start...")
3434

3535
start := time.Now()

websites/code/studygolang/src/service/searcher.go

Lines changed: 122 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import (
1212
"net/http"
1313
"net/url"
1414
"strconv"
15+
"strings"
1516

1617
"config"
1718
"logger"
@@ -25,6 +26,8 @@ const MaxRows = 100
2526
// isAll: 是否全量
2627
func Indexing(isAll bool) {
2728
IndexingArticle(isAll)
29+
IndexingTopic(isAll)
30+
IndexingResource(isAll)
2831
}
2932

3033
// 索引博文
@@ -63,6 +66,124 @@ func IndexingArticle(isAll bool) {
6366
}
6467
}
6568

69+
// 索引帖子
70+
func IndexingTopic(isAll bool) {
71+
solrClient := NewSolrClient()
72+
73+
topicObj := model.NewTopic()
74+
topicExObj := model.NewTopicEx()
75+
76+
limit := strconv.Itoa(MaxRows)
77+
if isAll {
78+
id := 0
79+
for {
80+
topicList, err := topicObj.Where("tid>?", id).Limit(limit).FindAll()
81+
if err != nil {
82+
logger.Errorln("IndexingTopic error:", err)
83+
break
84+
}
85+
86+
if len(topicList) == 0 {
87+
break
88+
}
89+
90+
tids := util.Models2Intslice(topicList, "Tid")
91+
92+
tmpStr := strings.Repeat("?,", len(tids))
93+
query := "tid in(" + tmpStr[:len(tmpStr)-1] + ")"
94+
args := make([]interface{}, len(tids))
95+
for i, tid := range tids {
96+
args[i] = tid
97+
}
98+
99+
topicExList, err := topicExObj.Where(query, args...).FindAll()
100+
if err != nil {
101+
logger.Errorln("IndexingTopic error:", err)
102+
break
103+
}
104+
105+
topicExMap := make(map[int]*model.TopicEx, len(topicExList))
106+
for _, topicEx := range topicExList {
107+
topicExMap[topicEx.Tid] = topicEx
108+
}
109+
110+
for _, topic := range topicList {
111+
if id < topic.Tid {
112+
id = topic.Tid
113+
}
114+
115+
topicEx, _ := topicExMap[topic.Tid]
116+
117+
document := model.NewDocument(topic, topicEx)
118+
addCommand := model.NewDefaultArgsAddCommand(document)
119+
120+
solrClient.Push(addCommand)
121+
}
122+
123+
solrClient.Post()
124+
}
125+
}
126+
}
127+
128+
// 索引资源
129+
func IndexingResource(isAll bool) {
130+
solrClient := NewSolrClient()
131+
132+
resourceObj := model.NewResource()
133+
resourceExObj := model.NewResourceEx()
134+
135+
limit := strconv.Itoa(MaxRows)
136+
if isAll {
137+
id := 0
138+
for {
139+
resourceList, err := resourceObj.Where("id>?", id).Limit(limit).FindAll()
140+
if err != nil {
141+
logger.Errorln("IndexingResource error:", err)
142+
break
143+
}
144+
145+
if len(resourceList) == 0 {
146+
break
147+
}
148+
149+
ids := util.Models2Intslice(resourceList, "Id")
150+
151+
tmpStr := strings.Repeat("?,", len(ids))
152+
query := "id in(" + tmpStr[:len(tmpStr)-1] + ")"
153+
args := make([]interface{}, len(ids))
154+
for i, rid := range ids {
155+
args[i] = rid
156+
}
157+
158+
resourceExList, err := resourceExObj.Where(query, args...).FindAll()
159+
if err != nil {
160+
logger.Errorln("IndexingResource error:", err)
161+
break
162+
}
163+
164+
resourceExMap := make(map[int]*model.ResourceEx, len(resourceExList))
165+
for _, resourceEx := range resourceExList {
166+
resourceExMap[resourceEx.Id] = resourceEx
167+
}
168+
169+
for _, resource := range resourceList {
170+
if id < resource.Id {
171+
id = resource.Id
172+
}
173+
174+
resourceEx, _ := resourceExMap[resource.Id]
175+
176+
document := model.NewDocument(resource, resourceEx)
177+
addCommand := model.NewDefaultArgsAddCommand(document)
178+
179+
solrClient.Push(addCommand)
180+
}
181+
182+
solrClient.Post()
183+
}
184+
}
185+
}
186+
66187
type SolrClient struct {
67188
addCommands []*model.AddCommand
68189
}
@@ -206,7 +327,7 @@ func DoSearch(q, field string, start, rows int) (*model.ResponseBody, error) {
206327
doc.HlTitle = doc.Title
207328
}
208329

209-
if doc.HlContent == "" {
330+
if doc.HlContent == "" && doc.Content != "" {
210331
maxLen := len(doc.Content) - 1
211332
if maxLen > ContentLen {
212333
maxLen = ContentLen

websites/code/studygolang/template/search.html

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -47,23 +47,33 @@
4747
<div class="row">
4848
<div>
4949
<h2>
50-
<a href="/articles/{{.Objid}}" target="_blank" title="{{.Title}}">{{noescape .HlTitle}}</a></h2>
50+
<a href="/{{if eq .Objtype 0}}topics{{else if eq .Objtype 1}}articles{{else}}resources{{end}}/{{.Objid}}" target="_blank" title="{{.Title}}">{{noescape .HlTitle}}</a></h2>
51+
{{if .Content}}
5152
<p class="text">{{noescape .HlContent}}<a href="/articles/{{.Objid}}" target="_blank" title="阅读全文">阅读全文</a></p>
53+
{{end}}
5254
</div>
5355
</div>
5456
<div class="row">
5557
<div class="col-md-8 metatag">
58+
<i class="glyphicon glyphicon-tasks"></i>
59+
<span class="source" title="类别">{{if eq .Objtype 0}}话题{{else if eq .Objtype 1}}博文{{else}}帖子{{end}}</span>
5660
<i class="glyphicon glyphicon-calendar"></i>
5761
<span class="date" title="发布日期">{{.PubTime}}</span>
5862
<i class="glyphicon glyphicon-user"></i>
59-
<span class="author" title="作者">{{.Author}}</span>
63+
<span class="author" title="作者">
64+
{{if eq .Objtype 1}}
65+
{{.Author}}
66+
{{else}}
67+
<a href="/user/{{.Author}}" target="_blank">{{.Author}}</a>
68+
{{end}}
69+
</span>
6070
{{if .Tags}}
6171
{{$tags := explode .Tags ","}}
6272
<ul class="list-inline">
6373
<i class="glyphicon glyphicon-tags"></i>
6474
{{range $tag := $tags}}
6575
<li>
66-
<a href="/" title="{{$tag}}" target="_blank">
76+
<a href="/search?q={{$tag}}f=tag" title="{{$tag}}" target="_blank">
6777
{{$tag}}
6878
</a>
6979
</li>

websites/databases/studygolang_db.sql

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -335,7 +335,7 @@ CREATE TABLE `articles` (
335335
`author_txt` varchar(127) NOT NULL DEFAULT '' COMMENT '文章作者(纯文本)',
336336
`lang` tinyint unsigned NOT NULL DEFAULT 0 COMMENT '语言:0-中文;1-英文',
337337
`pub_date` varchar(20) NOT NULL DEFAULT '' COMMENT '发布时间',
338-
`url` varchar(127) NOT NULL DEFAULT '' COMMENT '文章原始链接',
338+
`url` varchar(255) NOT NULL DEFAULT '' COMMENT '文章原始链接',
339339
`content` longtext NOT NULL COMMENT '正文(带html)',
340340
`txt` text NOT NULL COMMENT '正文(纯文本)',
341341
`tags` varchar(50) NOT NULL DEFAULT '' COMMENT '文章tag,逗号分隔',

0 commit comments

Comments
 (0)