Ver código fonte

feat:工具类升级

wangchuanjin 2 anos atrás
pai
commit
cf35f19034
3 arquivos alterados com 9 adições e 35 exclusões
  1. 5 5
      es/es.go
  2. 0 5
      es/esv1.go
  3. 4 25
      es/esv7.go

+ 5 - 5
es/es.go

@@ -9,8 +9,9 @@ import (
 )
 
 const (
-	SIZE = 30
-	QStr = `{"query":{"bool":{"must":[$and],"must_not":[],
+	SIZE        = 30
+	FilterQuery = `{"query": {"filtered": {"filter": {"bool": {"must": [%s]}},%s}}`
+	QStr        = `{"query":{"bool":{"must":[$and],"must_not":[],
 	"should":[$or],"minimum_should_match" : 1}}}`
 	NgramStr        = `{"query":{"bool":{"must":[%s],"should":[%s],"minimum_should_match": 1}}}`
 	NgramMust       = `{"bool":{"must":[%s]}}`
@@ -88,9 +89,8 @@ type Es interface {
 }
 
 var (
-	FilterQuery string
-	SR          = strings.Replace
-	VarEs       Es
+	SR    = strings.Replace
+	VarEs Es
 )
 
 type KeyConfig struct {

+ 0 - 5
es/esv1.go

@@ -15,10 +15,6 @@ import (
 	es "app.yhyue.com/moapp/esv1/gopkg.in/olivere/elastic.v1"
 )
 
-const (
-	esv1_FilterQuery = `{"query": {"filtered": {"filter": {"bool": {"must": [%s]}},%s}}`
-)
-
 type EsV1 struct {
 	Address      string
 	Size         int
@@ -29,7 +25,6 @@ type EsV1 struct {
 
 //n倍的池
 func (e *EsV1) Init() {
-	FilterQuery = esv1_FilterQuery
 	if e.Size <= 0 {
 		e.Size = SIZE
 	}

+ 4 - 25
es/esv7.go

@@ -15,10 +15,6 @@ import (
 	es "github.com/olivere/elastic/v7"
 )
 
-const (
-	esv7_FilterQuery = `{"query": {"filtered": {"filter": {"bool": {"must": [%s]}},%s}}`
-)
-
 type EsV7 struct {
 	Address      string
 	UserName     string
@@ -31,7 +27,6 @@ type EsV7 struct {
 
 //初始化es,带有用户名密码认证
 func (e *EsV7) Init() {
-	FilterQuery = esv7_FilterQuery
 	if e.Size <= 0 {
 		e.Size = SIZE
 	}
@@ -733,13 +728,7 @@ func (e *EsV7) GetByNgram(index, itype string, query interface{}, mustquery, fin
 //增加高亮、过滤查询、高亮截取字数
 func (e *EsV7) GetByNgramOther(index, itype string, query interface{}, mustquery, findfields, order, fields string, start, limit int, highlight bool, filtermode bool, count int) *[]map[string]interface{} {
 	defer catch()
-	qstr := ""
-	if mustquery != "" && filtermode {
-		qstr = e.GetNgramQuery(query, "", findfields)
-		qstr = fmt.Sprintf(FilterQuery, mustquery, qstr[1:])
-	} else {
-		qstr = e.GetNgramQuery(query, mustquery, findfields)
-	}
+	qstr := e.GetNgramQuery(query, mustquery, findfields)
 	if qstr != "" {
 		if highlight {
 			ws := []string{}
@@ -768,13 +757,7 @@ func (e *EsV7) GetByNgramOther(index, itype string, query interface{}, mustquery
 //替换了"号
 func (e *EsV7) GetByNgramAll(index, itype string, query interface{}, mustquery, findfields, order, fields string, start, limit int, highlight bool, filtermode bool) *[]map[string]interface{} {
 	defer catch()
-	qstr := ""
-	if mustquery != "" && filtermode {
-		qstr = e.GetNgramQuery(query, "", findfields)
-		qstr = fmt.Sprintf(FilterQuery, mustquery, qstr[1:])
-	} else {
-		qstr = e.GetNgramQuery(query, mustquery, findfields)
-	}
+	qstr := e.GetNgramQuery(query, mustquery, findfields)
 	if qstr != "" {
 		if highlight {
 			ws := []string{}
@@ -864,9 +847,7 @@ func (e *EsV7) GetResForJY(index, itype string, keys []KeyConfig, allquery, find
 			}
 			musts = append(musts, fmt.Sprintf(NgramMustAndNot, strings.Join(mq, ","), strings.Join(notmq, ",")))
 		}
-		qstr = fmt.Sprintf(NgramStr, "", strings.Join(musts, ","))
-
-		qstr = fmt.Sprintf(FilterQuery, allquery, qstr[1:])
+		qstr = fmt.Sprintf(NgramStr, allquery, strings.Join(musts, ","))
 		ws := []string{}
 		for _, w := range strings.Split(findfields, ",") {
 			ws = append(ws, fmt.Sprintf(highlightStr, w, 1))
@@ -989,9 +970,7 @@ func (e *EsV7) GetResForJYView(index, itype string, keys []KeyConfig, allquery,
 			}
 			musts = append(musts, fmt.Sprintf(NgramMustAndNot, strings.Join(mq, ","), strings.Join(notmq, ",")))
 		}
-		qstr = fmt.Sprintf(NgramStr, "", strings.Join(musts, ","))
-
-		qstr = fmt.Sprintf(FilterQuery, allquery, qstr[1:])
+		qstr = fmt.Sprintf(NgramStr, allquery, strings.Join(musts, ","))
 		ws := []string{}
 		for _, w := range strings.Split(findfields, ",") {
 			ws = append(ws, fmt.Sprintf(highlightStr, w, 1))