From f3b03249b4cdbadf6d82701c1d449ff32841a5bc Mon Sep 17 00:00:00 2001 From: lifei6671 Date: Thu, 29 Nov 2018 15:11:34 +0800 Subject: [PATCH] =?UTF-8?q?feat:=E6=94=AF=E6=8C=81=E6=90=9C=E7=B4=A2?= =?UTF-8?q?=E4=B8=AD=E4=BD=BF=E7=94=A8=E7=A9=BA=E6=A0=BC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- controllers/SearchController.go | 24 +++++++++++++----------- models/DocumentSearchResult.go | 4 +++- 2 files changed, 16 insertions(+), 12 deletions(-) diff --git a/controllers/SearchController.go b/controllers/SearchController.go index eba89b42..5dfe1d48 100644 --- a/controllers/SearchController.go +++ b/controllers/SearchController.go @@ -48,22 +48,24 @@ func (c *SearchController) Index() { c.Data["PageHtml"] = "" } if len(searchResult) > 0 { + keywords := strings.Split(keyword," ") + for _, item := range searchResult { - item.DocumentName = strings.Replace(item.DocumentName, keyword, ""+keyword+"", -1) + for _,word := range keywords { + item.DocumentName = strings.Replace(item.DocumentName, word, ""+word+"", -1) + if item.Description != "" { + src := item.Description - if item.Description != "" { - src := item.Description + r := []rune(utils.StripTags(item.Description)) - r := []rune(utils.StripTags(item.Description)) - - if len(r) > 100 { - src = string(r[:100]) - } else { - src = string(r) + if len(r) > 100 { + src = string(r[:100]) + } else { + src = string(r) + } + item.Description = strings.Replace(src, word, ""+word+"", -1) } - item.Description = strings.Replace(src, keyword, ""+keyword+"", -1) } - if item.Identify == "" { item.Identify = strconv.Itoa(item.DocumentId) } diff --git a/models/DocumentSearchResult.go b/models/DocumentSearchResult.go index 471b37d2..4d6ecb65 100644 --- a/models/DocumentSearchResult.go +++ b/models/DocumentSearchResult.go @@ -5,6 +5,7 @@ import ( "github.com/astaxie/beego/orm" "github.com/astaxie/beego" + "strings" ) type DocumentSearchResult struct { @@ -31,7 +32,8 @@ func (m *DocumentSearchResult) FindToPager(keyword string, pageIndex, pageSize, o := orm.NewOrm() offset := (pageIndex - 1) * pageSize - keyword = "%" + keyword + "%" + + keyword = "%" + strings.Replace(keyword," ","%",-1) + "%" if memberId <= 0 { sql1 := `SELECT count(doc.document_id) as total_count FROM md_documents AS doc