fix:优化搜索

pull/428/head
lifei6671 2018-12-24 15:23:17 +08:00
parent 329b87c685
commit d735ea27ca
3 changed files with 14 additions and 2 deletions

View File

@ -4,6 +4,7 @@ import (
"encoding/json"
"errors"
"fmt"
"github.com/lifei6671/mindoc/utils/sqltil"
"html/template"
"os"
"path/filepath"
@ -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 {
@ -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)

View File

@ -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"
)
@ -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
@ -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 {

View File

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