wangshan 3 жил өмнө
parent
commit
98a2ddc1dd

+ 54 - 0
jyBXCore/rpc/model/es/es.go

@@ -0,0 +1,54 @@
+package es
+
+import (
+	elastic "app.yhyue.com/moapp/jybase/esv1"
+	"fmt"
+	"strconv"
+	"strings"
+)
+
+const (
+	highlightStr = `%s: {"fragment_size": %d,"number_of_fragments": 1}`
+	HL           = `"highlight": {"pre_tags": [""],"post_tags": [""],"fields": {%s}}`
+)
+
+var SR = strings.Replace
+
+type EsSearch struct {
+	Index      string
+	Itype      string
+	Query      string
+	FindFields string
+	Order      string
+	Fields     string
+	Start      int
+	Limit      int
+	Count      int
+	HighLight  bool
+}
+
+//
+func (e *EsSearch) GetAllByNgramWithCount() (int64, *[]map[string]interface{}) {
+	qstr := e.Query
+	if e.Query != "" {
+		if e.HighLight {
+			ws := []string{}
+			for _, w := range strings.Split(e.Fields, ",") {
+				ws = append(ws, fmt.Sprintf(highlightStr, w, e.Count))
+			}
+			qstr = qstr[:len(qstr)-1] + `,` + fmt.Sprintf(HL, strings.Join(ws, ",")) + `}`
+		}
+		if len(e.Fields) > 0 {
+			qstr = qstr[:len(qstr)-1] + `,"_source":[` + e.Fields + "]}"
+		}
+		if len(e.Order) > 0 {
+			qstr = qstr[:len(qstr)-1] + `,"sort":[` + SR(SR(SR(SR(e.Order, ",", "},{", -1), " ", "", -1), ":-1", `:"desc"`, -1), ":1", `:"asc"`, -1) + `]}`
+		}
+		if e.Start > -1 {
+			qstr = qstr[:len(qstr)-1] + `,"from":` + strconv.Itoa(e.Start) + `,"size":` + strconv.Itoa(e.Limit) + "}"
+		}
+		return elastic.GetWithCount(e.Index, e.Itype, qstr)
+	} else {
+		return 0, nil
+	}
+}

+ 4 - 0
jyBXCore/rpc/util/search.go

@@ -358,6 +358,10 @@ func GetSearchQuery(in *bxcore.SearchReq, mustquery string) (qstr string) {
 		shoulds := []string{}
 		var switchBool = strings.Contains(findfields, "detail") && strings.Contains(findfields, "title") && IC.C.SearchTypeSwitch
 		for _, v := range strings.Split(in.KeyWords, "+") {
+			//多个关键词   单个字 搜索范围只有全文
+			if len(strings.Split(in.KeyWords, "+")) > 1 && len([]rune(elastic.ReplaceYH(v))) == 1 && findfields == `"detail"` {
+				continue
+			}
 			//标题 全文搜索 搜索类型开关打开 默认搜索全文;(全文包含标题)(单字排除)
 			if switchBool && len([]rune(elastic.ReplaceYH(v))) > 1 {
 				if strings.Contains(findfields, `"title",`) {