Skip to content

Commit

Permalink
feat:支持搜索中使用空格
Browse files Browse the repository at this point in the history
  • Loading branch information
lifei6671 committed Nov 29, 2018
1 parent 8681d8f commit f3b0324
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 14 deletions.
28 changes: 15 additions & 13 deletions controllers/SearchController.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,22 +48,24 @@ func (c *SearchController) Index() {
c.Data["PageHtml"] = ""
}
if len(searchResult) > 0 {
for _, item := range searchResult {
item.DocumentName = strings.Replace(item.DocumentName, keyword, "<em>"+keyword+"</em>", -1)

if item.Description != "" {
src := item.Description

r := []rune(utils.StripTags(item.Description))
keywords := strings.Split(keyword," ")

if len(r) > 100 {
src = string(r[:100])
} else {
src = string(r)
for _, item := range searchResult {
for _,word := range keywords {
item.DocumentName = strings.Replace(item.DocumentName, word, "<em>"+word+"</em>", -1)
if item.Description != "" {
src := item.Description

r := []rune(utils.StripTags(item.Description))

if len(r) > 100 {
src = string(r[:100])
} else {
src = string(r)
}
item.Description = strings.Replace(src, word, "<em>"+word+"</em>", -1)
}
item.Description = strings.Replace(src, keyword, "<em>"+keyword+"</em>", -1)
}

if item.Identify == "" {
item.Identify = strconv.Itoa(item.DocumentId)
}
Expand Down
4 changes: 3 additions & 1 deletion models/DocumentSearchResult.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (

"github.com/astaxie/beego/orm"
"github.com/astaxie/beego"
"strings"
)

type DocumentSearchResult struct {
Expand All @@ -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
Expand Down

0 comments on commit f3b0324

Please sign in to comment.