|
@@ -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)
|
|
}
|
|
}
|