wangchuanjin há 2 anos atrás
pai
commit
e0bdfc8ee0
3 ficheiros alterados com 116 adições e 222 exclusões
  1. 111 16
      es/es.go
  2. 2 104
      es/esv1.go
  3. 3 102
      es/esv7.go

+ 111 - 16
es/es.go

@@ -8,19 +8,19 @@ import (
 	"log"
 	"log"
 	"math/big"
 	"math/big"
 	"net/http"
 	"net/http"
+	"reflect"
 	"runtime"
 	"runtime"
+	"strconv"
 	"strings"
 	"strings"
 	"unicode"
 	"unicode"
 
 
-	util "app.yhyue.com/moapp/jybase/common"
 	esV7 "github.com/olivere/elastic/v7"
 	esV7 "github.com/olivere/elastic/v7"
 )
 )
 
 
 const (
 const (
-	SIZE        = 30
-	FilterQuery = `{"query": {"filtered": {"filter": {"bool": {"must": [%s]}},%s}}`
-	QStr        = `{"query":{"bool":{"must":[$and],"must_not":[],
-	"should":[$or],"minimum_should_match" : 1}}}`
+	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}}}`
 	NgramStr        = `{"query":{"bool":{"must":[%s],"should":[%s],"minimum_should_match": 1}}}`
 	NgramMust       = `{"bool":{"must":[%s]}}`
 	NgramMust       = `{"bool":{"must":[%s]}}`
 	NgramMustAndNot = `{"bool":{"must":[%s],"must_not":[%s]}}`
 	NgramMustAndNot = `{"bool":{"must":[%s],"must_not":[%s]}}`
@@ -50,8 +50,6 @@ type Es interface {
 	GetNoLimit(index, itype, query string) *[]map[string]interface{}
 	GetNoLimit(index, itype, query string) *[]map[string]interface{}
 	GetPage(index, itype, query, order, field string, start, limit int) *[]map[string]interface{}
 	GetPage(index, itype, query, order, field string, start, limit int) *[]map[string]interface{}
 	GetOAPage(index, itype, query, order, field string, start, limit int) (*[]map[string]interface{}, int)
 	GetOAPage(index, itype, query, order, field string, start, limit int) (*[]map[string]interface{}, int)
-	MakeQuery(query, order, fileds string, start, limit int) string
-	AnalyQuery(query interface{}, parent string, result string) string
 	GetByIdField(index, itype, id, fields string) *map[string]interface{}
 	GetByIdField(index, itype, id, fields string) *map[string]interface{}
 	GetById(index, itype string, ids ...string) *[]map[string]interface{}
 	GetById(index, itype string, ids ...string) *[]map[string]interface{}
 	Update(index, itype, id string, updateStr string) bool
 	Update(index, itype, id string, updateStr string) bool
@@ -90,6 +88,108 @@ type KeyConfig struct {
 	Areas     []string `json:"area"`
 	Areas     []string `json:"area"`
 }
 }
 
 
+func MakeQuery(query, order, fileds string, start, limit int) string {
+	res := AnalyQuery(query, "", QStr)
+	if len(res) > 10 {
+		if strings.Contains(res, "###剑鱼###") {
+			res = strings.ReplaceAll(res, "###剑鱼###", "\\\"")
+		}
+		res = SR(SR(SR(SR(res, ",$and", "", -1), "$and", "", -1), ",$or", "", -1), "$or", "", -1)
+		if len(fileds) > 0 {
+			//"_source":["account_number","balance"]
+			res = res[:len(res)-1] + `,"_source":[` + fileds + "]}"
+		}
+		//{"name":-1,"age":1}
+		if len(order) > 0 {
+			res = res[:len(res)-1] + `,"sort":[` + SR(SR(SR(SR(order, ",", "},{", -1), " ", "", -1), ":-1", `:"desc"`, -1), ":1", `:"asc"`, -1) + `]}`
+		}
+		if start > -1 {
+			res = res[:len(res)-1] + `,"from":` + strconv.Itoa(start) + `,"size":` + strconv.Itoa(limit) + "}"
+		}
+		return strings.ReplaceAll(res, `,"should":[],"minimum_should_match":1`, "")
+	}
+	return ""
+}
+
+//{"name":"aaa"}
+func AnalyQuery(query interface{}, parent string, result string) string {
+	m := make(map[string]interface{})
+	if q1, ok := query.(string); ok {
+		json.Unmarshal([]byte(q1), &m)
+	} else if q2, ok2 := query.(map[string]interface{}); ok2 {
+		m = q2
+	}
+	if len(parent) == 0 {
+		for k, v := range m {
+			if k == "$and" || k == "$or" {
+				temps := ""
+				if map1, ok := v.([]interface{}); ok {
+					for i := 0; i < len(map1); i++ {
+						temps += "," + AnalyQuery(map1[i], k, "")
+					}
+				}
+				if len(temps) > 0 {
+					temps = temps[1:]
+				}
+				result = SR(result, k, temps+","+k, 1)
+			} else {
+				switch reflect.TypeOf(v).String() {
+				case "string":
+					if strings.Index(k, "TERM_") == 0 {
+						result = SR(result, "$and", `{"term":{"`+SR(k, "TERM_", "", 1)+`":"`+fmt.Sprintf("%v", v)+`"}},$and`, 1)
+					} else {
+						result = SR(result, "$and", `{"query_string":{"default_field":"`+k+`","query":"`+fmt.Sprintf("%v", v)+`"}},$and`, 1)
+					}
+				case "int", "int8", "int32", "int64", "float32", "float64":
+					if strings.Index(k, "TERM_") == 0 {
+						result = SR(result, "$and", `{"term":{"`+SR(k, "TERM_", "", 1)+`":`+fmt.Sprintf("%v", v)+`}},$and`, 1)
+					} else {
+						result = SR(result, "$and", `{"query_string":{"default_field":"`+k+`","query":`+fmt.Sprintf("%v", v)+`}},$and`, 1)
+					}
+				default:
+					result = SR(result, "$and", AnalyQuery(v, k, "")+",$and", 1)
+				}
+			}
+		}
+		return result
+	} else {
+		for k, v := range m {
+			if k == "$in" {
+				s := ""
+				if map1, ok := v.([]interface{}); ok {
+					for i := 0; i < len(map1); i++ {
+						s += "," + `"` + fmt.Sprintf("%v", map1[i]) + `"`
+					}
+				}
+				if len(s) > 0 {
+					s = s[1:]
+				}
+				return `{"terms":{"` + parent + `":[` + s + `]}}`
+			} else if strings.Contains(k, "$lt") || strings.Contains(k, "$gt") {
+				return `{"range":{"` + parent + `":{"` + SR(k, "$", "", 1) + `":` + fmt.Sprintf("%v", v) + `}}}`
+			} else {
+				switch reflect.TypeOf(v).String() {
+				case "string":
+					if strings.Index(k, "TERM_") == 0 {
+						return `{"term":{"` + SR(k, "TERM_", "", 1) + `":"` + fmt.Sprintf("%v", v) + `"}}`
+					} else {
+						return `{"query_string":{"default_field":"` + k + `","query":"` + fmt.Sprintf("%v", v) + `"}}`
+					}
+				case "int", "int8", "int32", "int64", "float32", "float64":
+					if strings.Index(k, "TERM_") == 0 {
+						return `{"term":{"` + SR(k, "TERM_", "", 1) + `":` + fmt.Sprintf("%v", v) + `}}`
+					} else {
+						return `{"query_string":{"default_field":"` + k + `","query":` + fmt.Sprintf("%v", v) + `}}`
+					}
+				default:
+					return AnalyQuery(v, k, result)
+				}
+			}
+		}
+	}
+	return result
+}
+
 func ReplaceYH(src string) (rpl string) {
 func ReplaceYH(src string) (rpl string) {
 	return strings.Replace(src, `"`, `\"`, -1)
 	return strings.Replace(src, `"`, `\"`, -1)
 }
 }
@@ -185,9 +285,10 @@ func (a *analyze) post(text, analyzer, url string, e Es) (string, error) {
 	json.Unmarshal(result, &resmap)
 	json.Unmarshal(result, &resmap)
 	res := ""
 	res := ""
 	if resmap != nil && resmap["tokens"] != nil {
 	if resmap != nil && resmap["tokens"] != nil {
-		tokens := util.ObjArrToMapArr(resmap["tokens"].([]interface{}))
-		for _, v := range tokens {
-			token := util.ObjToString(v["token"])
+		tokens, _ := resmap["tokens"].([]interface{})
+		for _, ov := range tokens {
+			v, _ := ov.(map[string]interface{})
+			token, _ := v["token"].(string)
 			if len([]rune(token)) == 1 && !unicode.Is(unicode.Scripts["Han"], []rune(token)[0]) { //(P260保留单个汉字)
 			if len([]rune(token)) == 1 && !unicode.Is(unicode.Scripts["Han"], []rune(token)[0]) { //(P260保留单个汉字)
 				continue
 				continue
 			}
 			}
@@ -240,12 +341,6 @@ func GetPage(index, itype, query, order, field string, start, limit int) *[]map[
 func GetOAPage(index, itype, query, order, field string, start, limit int) (*[]map[string]interface{}, int) {
 func GetOAPage(index, itype, query, order, field string, start, limit int) (*[]map[string]interface{}, int) {
 	return VarEs.GetOAPage(index, itype, query, order, field, start, limit)
 	return VarEs.GetOAPage(index, itype, query, order, field, start, limit)
 }
 }
-func MakeQuery(query, order, fileds string, start, limit int) string {
-	return VarEs.MakeQuery(query, order, fileds, start, limit)
-}
-func AnalyQuery(query interface{}, parent string, result string) string {
-	return VarEs.AnalyQuery(query, parent, result)
-}
 func GetByIdField(index, itype, id, fields string) *map[string]interface{} {
 func GetByIdField(index, itype, id, fields string) *map[string]interface{} {
 	return VarEs.GetByIdField(index, itype, id, fields)
 	return VarEs.GetByIdField(index, itype, id, fields)
 }
 }

+ 2 - 104
es/esv1.go

@@ -6,7 +6,6 @@ import (
 	"log"
 	"log"
 	"net/http"
 	"net/http"
 	"net/url"
 	"net/url"
-	"reflect"
 	"runtime"
 	"runtime"
 	"strconv"
 	"strconv"
 	"strings"
 	"strings"
@@ -267,115 +266,14 @@ func (e *EsV1) GetNoLimit(index, itype, query string) *[]map[string]interface{}
 //{"name":"张三","$and":[{"age":{"$gt":10}},{"age":{"$lte":20}}]}
 //{"name":"张三","$and":[{"age":{"$gt":10}},{"age":{"$lte":20}}]}
 //fields直接是 `"_id","title"`
 //fields直接是 `"_id","title"`
 func (e *EsV1) GetPage(index, itype, query, order, field string, start, limit int) *[]map[string]interface{} {
 func (e *EsV1) GetPage(index, itype, query, order, field string, start, limit int) *[]map[string]interface{} {
-	return e.Get(index, itype, e.MakeQuery(query, order, field, start, limit))
+	return e.Get(index, itype, MakeQuery(query, order, field, start, limit))
 }
 }
 
 
 //openapi
 //openapi
 func (e *EsV1) GetOAPage(index, itype, query, order, field string, start, limit int) (*[]map[string]interface{}, int) {
 func (e *EsV1) GetOAPage(index, itype, query, order, field string, start, limit int) (*[]map[string]interface{}, int) {
-	return e.GetOA(index, itype, e.MakeQuery(query, order, field, start, limit))
+	return e.GetOA(index, itype, MakeQuery(query, order, field, start, limit))
 }
 }
 
 
-func (e *EsV1) MakeQuery(query, order, fileds string, start, limit int) string {
-	res := e.AnalyQuery(query, "", QStr)
-	if len(res) > 10 {
-		if strings.Contains(res, "###剑鱼###") {
-			res = strings.ReplaceAll(res, "###剑鱼###", "\\\"")
-		}
-		res = SR(SR(SR(SR(res, ",$and", "", -1), "$and", "", -1), ",$or", "", -1), "$or", "", -1)
-		if len(fileds) > 0 {
-			//"_source":["account_number","balance"]
-			res = res[:len(res)-1] + `,"_source":[` + fileds + "]}"
-		}
-		//{"name":-1,"age":1}
-		if len(order) > 0 {
-			res = res[:len(res)-1] + `,"sort":[` + SR(SR(SR(SR(order, ",", "},{", -1), " ", "", -1), ":-1", `:"desc"`, -1), ":1", `:"asc"`, -1) + `]}`
-		}
-		if start > -1 {
-			res = res[:len(res)-1] + `,"from":` + strconv.Itoa(start) + `,"size":` + strconv.Itoa(limit) + "}"
-		}
-		return res
-	}
-	return ""
-}
-
-//{"name":"aaa"}
-func (e *EsV1) AnalyQuery(query interface{}, parent string, result string) string {
-	m := make(map[string]interface{})
-	if q1, ok := query.(string); ok {
-		json.Unmarshal([]byte(q1), &m)
-	} else if q2, ok2 := query.(map[string]interface{}); ok2 {
-		m = q2
-	}
-	if len(parent) == 0 {
-		for k, v := range m {
-			if k == "$and" || k == "$or" {
-				temps := ""
-				if map1, ok := v.([]interface{}); ok {
-					for i := 0; i < len(map1); i++ {
-						temps += "," + e.AnalyQuery(map1[i], k, "")
-					}
-				}
-				if len(temps) > 0 {
-					temps = temps[1:]
-				}
-				result = SR(result, k, temps+","+k, 1)
-			} else {
-				switch reflect.TypeOf(v).String() {
-				case "string":
-					if strings.Index(k, "TERM_") == 0 {
-						result = SR(result, "$and", `{"term":{"`+SR(k, "TERM_", "", 1)+`":"`+fmt.Sprintf("%v", v)+`"}},$and`, 1)
-					} else {
-						result = SR(result, "$and", `{"query_string":{"default_field":"`+k+`","query":"`+fmt.Sprintf("%v", v)+`"}},$and`, 1)
-					}
-				case "int", "int8", "int32", "int64", "float32", "float64":
-					if strings.Index(k, "TERM_") == 0 {
-						result = SR(result, "$and", `{"term":{"`+SR(k, "TERM_", "", 1)+`":`+fmt.Sprintf("%v", v)+`}},$and`, 1)
-					} else {
-						result = SR(result, "$and", `{"query_string":{"default_field":"`+k+`","query":`+fmt.Sprintf("%v", v)+`}},$and`, 1)
-					}
-				default:
-					result = SR(result, "$and", e.AnalyQuery(v, k, "")+",$and", 1)
-				}
-			}
-		}
-		return result
-	} else {
-		for k, v := range m {
-			if k == "$in" {
-				s := ""
-				if map1, ok := v.([]interface{}); ok {
-					for i := 0; i < len(map1); i++ {
-						s += "," + `"` + fmt.Sprintf("%v", map1[i]) + `"`
-					}
-				}
-				if len(s) > 0 {
-					s = s[1:]
-				}
-				return `{"terms":{"` + parent + `":[` + s + `]}}`
-			} else if strings.Contains(k, "$lt") || strings.Contains(k, "$gt") {
-				return `{"range":{"` + parent + `":{"` + SR(k, "$", "", 1) + `":` + fmt.Sprintf("%v", v) + `}}}`
-			} else {
-				switch reflect.TypeOf(v).String() {
-				case "string":
-					if strings.Index(k, "TERM_") == 0 {
-						return `{"term":{"` + SR(k, "TERM_", "", 1) + `":"` + fmt.Sprintf("%v", v) + `"}}`
-					} else {
-						return `{"query_string":{"default_field":"` + k + `","query":"` + fmt.Sprintf("%v", v) + `"}}`
-					}
-				case "int", "int8", "int32", "int64", "float32", "float64":
-					if strings.Index(k, "TERM_") == 0 {
-						return `{"term":{"` + SR(k, "TERM_", "", 1) + `":` + fmt.Sprintf("%v", v) + `}}`
-					} else {
-						return `{"query_string":{"default_field":"` + k + `","query":` + fmt.Sprintf("%v", v) + `}}`
-					}
-				default:
-					return e.AnalyQuery(v, k, result)
-				}
-			}
-		}
-	}
-	return result
-}
 func (e *EsV1) GetByIdField(index, itype, id, fields string) *map[string]interface{} {
 func (e *EsV1) GetByIdField(index, itype, id, fields string) *map[string]interface{} {
 	client := e.GetEsConn()
 	client := e.GetEsConn()
 	defer e.DestoryEsConn(client)
 	defer e.DestoryEsConn(client)

+ 3 - 102
es/esv7.go

@@ -6,7 +6,6 @@ import (
 	"fmt"
 	"fmt"
 	"log"
 	"log"
 	"net/http"
 	"net/http"
-	"reflect"
 	"runtime"
 	"runtime"
 	"strconv"
 	"strconv"
 	"strings"
 	"strings"
@@ -146,7 +145,7 @@ func (e *EsV7) Get(index, itype, query string) *[]map[string]interface{} {
 	return r
 	return r
 }
 }
 func (e *EsV7) get(index, itype, searchType, query string, isLimit, isHighlight bool) (int64, int, *[]map[string]interface{}) {
 func (e *EsV7) get(index, itype, searchType, query string, isLimit, isHighlight bool) (int64, int, *[]map[string]interface{}) {
-	//log.Println("query  -- ", query)
+	log.Println("query  -- ", query)
 	client := e.GetEsConn()
 	client := e.GetEsConn()
 	defer func() {
 	defer func() {
 		go e.DestoryEsConn(client)
 		go e.DestoryEsConn(client)
@@ -215,112 +214,14 @@ func (e *EsV7) GetNoLimit(index, itype, query string) *[]map[string]interface{}
 //{"name":"张三","$and":[{"age":{"$gt":10}},{"age":{"$lte":20}}]}
 //{"name":"张三","$and":[{"age":{"$gt":10}},{"age":{"$lte":20}}]}
 //fields直接是 `"_id","title"`
 //fields直接是 `"_id","title"`
 func (e *EsV7) GetPage(index, itype, query, order, field string, start, limit int) *[]map[string]interface{} {
 func (e *EsV7) GetPage(index, itype, query, order, field string, start, limit int) *[]map[string]interface{} {
-	return e.Get(index, itype, e.MakeQuery(query, order, field, start, limit))
+	return e.Get(index, itype, MakeQuery(query, order, field, start, limit))
 }
 }
 
 
 //openapi
 //openapi
 func (e *EsV7) GetOAPage(index, itype, query, order, field string, start, limit int) (*[]map[string]interface{}, int) {
 func (e *EsV7) GetOAPage(index, itype, query, order, field string, start, limit int) (*[]map[string]interface{}, int) {
-	return e.GetOA(index, itype, e.MakeQuery(query, order, field, start, limit))
+	return e.GetOA(index, itype, MakeQuery(query, order, field, start, limit))
 }
 }
 
 
-func (e *EsV7) MakeQuery(query, order, fileds string, start, limit int) string {
-	res := e.AnalyQuery(query, "", QStr)
-	if len(res) > 10 {
-		res = SR(SR(SR(SR(res, ",$and", "", -1), "$and", "", -1), ",$or", "", -1), "$or", "", -1)
-		if len(fileds) > 0 {
-			//"_source":["account_number","balance"]
-			res = res[:len(res)-1] + `,"_source":[` + fileds + "]}"
-		}
-		//{"name":-1,"age":1}
-		if len(order) > 0 {
-			res = res[:len(res)-1] + `,"sort":[` + SR(SR(SR(SR(order, ",", "},{", -1), " ", "", -1), ":-1", `:"desc"`, -1), ":1", `:"asc"`, -1) + `]}`
-		}
-		if start > -1 {
-			res = res[:len(res)-1] + `,"from":` + strconv.Itoa(start) + `,"size":` + strconv.Itoa(limit) + "}"
-		}
-		return res
-	}
-	return ""
-}
-
-//{"name":"aaa"}
-func (e *EsV7) AnalyQuery(query interface{}, parent string, result string) string {
-	m := make(map[string]interface{})
-	if q1, ok := query.(string); ok {
-		json.Unmarshal([]byte(q1), &m)
-	} else if q2, ok2 := query.(map[string]interface{}); ok2 {
-		m = q2
-	}
-	if len(parent) == 0 {
-		for k, v := range m {
-			if k == "$and" || k == "$or" {
-				temps := ""
-				if map1, ok := v.([]interface{}); ok {
-					for i := 0; i < len(map1); i++ {
-						temps += "," + e.AnalyQuery(map1[i], k, "")
-					}
-				}
-				if len(temps) > 0 {
-					temps = temps[1:]
-				}
-				result = SR(result, k, temps+","+k, 1)
-			} else {
-				switch reflect.TypeOf(v).String() {
-				case "string":
-					if strings.Index(k, "TERM_") == 0 {
-						result = SR(result, "$and", `{"term":{"`+SR(k, "TERM_", "", 1)+`":"`+fmt.Sprintf("%v", v)+`"}},$and`, 1)
-					} else {
-						result = SR(result, "$and", `{"query_string":{"default_field":"`+k+`","query":"`+fmt.Sprintf("%v", v)+`"}},$and`, 1)
-					}
-				case "int", "int8", "int32", "int64", "float32", "float64":
-					if strings.Index(k, "TERM_") == 0 {
-						result = SR(result, "$and", `{"term":{"`+SR(k, "TERM_", "", 1)+`":`+fmt.Sprintf("%v", v)+`}},$and`, 1)
-					} else {
-						result = SR(result, "$and", `{"query_string":{"default_field":"`+k+`","query":`+fmt.Sprintf("%v", v)+`}},$and`, 1)
-					}
-				default:
-					result = SR(result, "$and", e.AnalyQuery(v, k, "")+",$and", 1)
-				}
-			}
-		}
-		return result
-	} else {
-		for k, v := range m {
-			if k == "$in" {
-				s := ""
-				if map1, ok := v.([]interface{}); ok {
-					for i := 0; i < len(map1); i++ {
-						s += "," + `"` + fmt.Sprintf("%v", map1[i]) + `"`
-					}
-				}
-				if len(s) > 0 {
-					s = s[1:]
-				}
-				return `{"terms":{"` + parent + `":[` + s + `]}}`
-			} else if strings.Contains(k, "$lt") || strings.Contains(k, "$gt") {
-				return `{"range":{"` + parent + `":{"` + SR(k, "$", "", 1) + `":` + fmt.Sprintf("%v", v) + `}}}`
-			} else {
-				switch reflect.TypeOf(v).String() {
-				case "string":
-					if strings.Index(k, "TERM_") == 0 {
-						return `{"term":{"` + SR(k, "TERM_", "", 1) + `":"` + fmt.Sprintf("%v", v) + `"}}`
-					} else {
-						return `{"query_string":{"default_field":"` + k + `","query":"` + fmt.Sprintf("%v", v) + `"}}`
-					}
-				case "int", "int8", "int32", "int64", "float32", "float64":
-					if strings.Index(k, "TERM_") == 0 {
-						return `{"term":{"` + SR(k, "TERM_", "", 1) + `":` + fmt.Sprintf("%v", v) + `}}`
-					} else {
-						return `{"query_string":{"default_field":"` + k + `","query":` + fmt.Sprintf("%v", v) + `}}`
-					}
-				default:
-					return e.AnalyQuery(v, k, result)
-				}
-			}
-		}
-	}
-	return result
-}
 func (e *EsV7) GetByIdField(index, itype, id, fields string) *map[string]interface{} {
 func (e *EsV7) GetByIdField(index, itype, id, fields string) *map[string]interface{} {
 	client := e.GetEsConn()
 	client := e.GetEsConn()
 	defer e.DestoryEsConn(client)
 	defer e.DestoryEsConn(client)