فهرست منبع

feat:支持搜索中使用空格

lifei6671 6 سال پیش
والد
کامیت
f3b03249b4
2فایلهای تغییر یافته به همراه18 افزوده شده و 14 حذف شده
  1. 15 13
      controllers/SearchController.go
  2. 3 1
      models/DocumentSearchResult.go

+ 15 - 13
controllers/SearchController.go

@@ -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)
 				}

+ 3 - 1
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