return fmt.Sprintf("http://www.gravatar.com/avatar/%s?s=%d", util.Md5(email), size) }, // 转为前端显示需要的时间格式 "formatTime": func(i interface{}) string { ctime, ok := i.(string) if !ok { return "" } t, _ := time.Parse("2006-01-02 15:04:05", ctime) return t.Format(time.RFC3339) + "+08:00" }, "substring": func(str string, length int, suffix string) string { if length >= len(str) { return str } utf8Str := util.NewString(str) if length > utf8Str.RuneCount() { return str } return utf8Str.Slice(0, length) + suffix }, // if 比较 "eq": func(a, b string) bool { if a == b { return true } return false }, "noteq": func(a, b string) bool { if a == b { return false
// DoSearch 搜索 func (self SearcherLogic) DoSearch(q, field string, start, rows int) (*model.ResponseBody, error) { selectUrl := self.engineUrl + "/select?" var values = url.Values{ "wt": []string{"json"}, "hl": []string{"true"}, "hl.fl": []string{"title,content"}, "hl.simple.pre": []string{"<em>"}, "hl.simple.post": []string{"</em>"}, "hl.fragsize": []string{strconv.Itoa(searchContentLen)}, "start": []string{strconv.Itoa(start)}, "rows": []string{strconv.Itoa(rows)}, } if q == "" { values.Add("q", "*:*") } else { searchStat := &model.SearchStat{} MasterDB.Where("keyword=?", q).Get(searchStat) if searchStat.Id > 0 { MasterDB.Where("keyword=?", q).Incr("times", 1).Update(new(model.SearchStat)) } else { searchStat.Keyword = q searchStat.Times = 1 _, err := MasterDB.Insert(searchStat) if err != nil { MasterDB.Where("keyword=?", q).Incr("times", 1).Update(new(model.SearchStat)) } } } isTag := false // TODO: 目前大部分都没有tag,因此,对tag特殊处理 if field == "text" || field == "tag" { if field == "tag" { isTag = true } field = "" } if field != "" { values.Add("df", field) if q != "" { values.Add("q", q) } } else { // 全文检索 if q != "" { if isTag { values.Add("q", "title:"+q+"^2"+" OR tags:"+q+"^4 OR content:"+q+"^0.2") } else { values.Add("q", "title:"+q+"^2"+" OR content:"+q+"^0.2") } } } resp, err := http.Get(selectUrl + values.Encode()) if err != nil { logger.Errorln("search error:", err) return &model.ResponseBody{}, err } defer resp.Body.Close() var searchResponse model.SearchResponse err = json.NewDecoder(resp.Body).Decode(&searchResponse) if err != nil { logger.Errorln("parse response error:", err) return &model.ResponseBody{}, err } if len(searchResponse.Highlight) > 0 { for _, doc := range searchResponse.RespBody.Docs { highlighting, ok := searchResponse.Highlight[doc.Id] if ok { if len(highlighting.Title) > 0 { doc.HlTitle = highlighting.Title[0] } if len(highlighting.Content) > 0 { doc.HlContent = highlighting.Content[0] } } if doc.HlTitle == "" { doc.HlTitle = doc.Title } if doc.HlContent == "" && doc.Content != "" { utf8string := util.NewString(doc.Content) maxLen := utf8string.RuneCount() - 1 if maxLen > searchContentLen { maxLen = searchContentLen } doc.HlContent = util.NewString(doc.Content).Slice(0, maxLen) } doc.HlContent += "..." } } if searchResponse.RespBody == nil { searchResponse.RespBody = &model.ResponseBody{} } return searchResponse.RespBody, nil }