feat:支持搜索中使用空格

pull/425/head v2.0-beta.2
lifei6671 2018-11-29 15:11:34 +08:00
parent 8681d8fa3e
commit f3b03249b4
2 changed files with 16 additions and 12 deletions

View File

@ -48,9 +48,11 @@ func (c *SearchController) Index() {
c.Data["PageHtml"] = "" c.Data["PageHtml"] = ""
} }
if len(searchResult) > 0 { if len(searchResult) > 0 {
for _, item := range searchResult { keywords := strings.Split(keyword," ")
item.DocumentName = strings.Replace(item.DocumentName, keyword, "<em>"+keyword+"</em>", -1)
for _, item := range searchResult {
for _,word := range keywords {
item.DocumentName = strings.Replace(item.DocumentName, word, "<em>"+word+"</em>", -1)
if item.Description != "" { if item.Description != "" {
src := item.Description src := item.Description
@ -61,9 +63,9 @@ func (c *SearchController) Index() {
} else { } else {
src = string(r) src = string(r)
} }
item.Description = strings.Replace(src, keyword, "<em>"+keyword+"</em>", -1) item.Description = strings.Replace(src, word, "<em>"+word+"</em>", -1)
}
} }
if item.Identify == "" { if item.Identify == "" {
item.Identify = strconv.Itoa(item.DocumentId) item.Identify = strconv.Itoa(item.DocumentId)
} }

View File

@ -5,6 +5,7 @@ import (
"github.com/astaxie/beego/orm" "github.com/astaxie/beego/orm"
"github.com/astaxie/beego" "github.com/astaxie/beego"
"strings"
) )
type DocumentSearchResult struct { type DocumentSearchResult struct {
@ -31,7 +32,8 @@ func (m *DocumentSearchResult) FindToPager(keyword string, pageIndex, pageSize,
o := orm.NewOrm() o := orm.NewOrm()
offset := (pageIndex - 1) * pageSize offset := (pageIndex - 1) * pageSize
keyword = "%" + keyword + "%"
keyword = "%" + strings.Replace(keyword," ","%",-1) + "%"
if memberId <= 0 { if memberId <= 0 {
sql1 := `SELECT count(doc.document_id) as total_count FROM md_documents AS doc sql1 := `SELECT count(doc.document_id) as total_count FROM md_documents AS doc