Skip to content

Commit

Permalink
fix:优化搜索
Browse files Browse the repository at this point in the history
  • Loading branch information
lifei6671 committed Dec 24, 2018
1 parent 329b87c commit d735ea2
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 2 deletions.
4 changes: 3 additions & 1 deletion controllers/BookController.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"encoding/json"
"errors"
"fmt"
"github.com/lifei6671/mindoc/utils/sqltil"
"html/template"
"os"
"path/filepath"
Expand Down Expand Up @@ -957,7 +958,7 @@ func (c *BookController) TeamSearch() {
if err != nil {
c.JsonResult(500, err.Error())
}

keyword = sqltil.EscapeLike(keyword)
searchResult, err := models.NewTeamRelationship().FindNotJoinBookByBookIdentify(book.BookId, keyword, 10)

if err != nil {
Expand All @@ -972,6 +973,7 @@ func (c *BookController) ItemsetsSearch() {
c.Prepare()

keyword := strings.TrimSpace(c.GetString("q"))
keyword = sqltil.EscapeLike(keyword)

searchResult, err := models.NewItemsets().FindItemsetsByName(keyword, 10)

Expand Down
4 changes: 3 additions & 1 deletion controllers/SearchController.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"github.com/lifei6671/mindoc/models"
"github.com/lifei6671/mindoc/utils"
"github.com/lifei6671/mindoc/utils/pagination"
"github.com/lifei6671/mindoc/utils/sqltil"
"strconv"
"strings"
)
Expand Down Expand Up @@ -36,7 +37,7 @@ func (c *SearchController) Index() {
if c.Member != nil {
memberId = c.Member.MemberId
}
searchResult, totalCount, err := models.NewDocumentSearchResult().FindToPager(keyword, pageIndex, conf.PageSize, memberId)
searchResult, totalCount, err := models.NewDocumentSearchResult().FindToPager(sqltil.EscapeLike(keyword), pageIndex, conf.PageSize, memberId)

if err != nil {
return
Expand Down Expand Up @@ -86,6 +87,7 @@ func (c *SearchController) User() {
if key == "" || keyword == "" {
c.JsonResult(404, "参数错误")
}
keyword = sqltil.EscapeLike(keyword)

book, err := models.NewBookResult().FindByIdentify(key, c.Member.MemberId)
if err != nil {
Expand Down
8 changes: 8 additions & 0 deletions utils/sqltil/sql.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package sqltil

import "strings"

//转义like语法的%_符号
func EscapeLike(keyword string) string {
return strings.Replace(strings.Replace(keyword,"_","\\_",-1),"%","\\%",-1)
}

0 comments on commit d735ea2

Please sign in to comment.