|
@@ -16,7 +16,7 @@ import (
|
|
|
"time"
|
|
|
)
|
|
|
|
|
|
-//检索库服务地址
|
|
|
+// 检索库服务地址
|
|
|
var addrs []string
|
|
|
var LocCity = map[string]string{}
|
|
|
var SIZE = 30
|
|
@@ -51,7 +51,7 @@ var httpclient = &http.Client{Transport: &http.Transport{
|
|
|
//var op = es.SetHttpClient(httpclient)
|
|
|
var poolsize = int32(20)
|
|
|
|
|
|
-//n倍的池
|
|
|
+// n倍的池
|
|
|
func InitElasticSize(addr string, size int) {
|
|
|
poolsize = int32(3 * size)
|
|
|
pool = make(chan *es.Client, poolsize)
|
|
@@ -64,7 +64,7 @@ func InitElasticSize(addr string, size int) {
|
|
|
}
|
|
|
}
|
|
|
|
|
|
-//关闭连接
|
|
|
+// 关闭连接
|
|
|
func DestoryEsConn(client *es.Client) {
|
|
|
select {
|
|
|
case pool <- client:
|
|
@@ -119,21 +119,21 @@ func GetEsConn() *es.Client {
|
|
|
}
|
|
|
}
|
|
|
|
|
|
-//通用查询
|
|
|
-//{"query": {"bool":{"must":[{"query_string":{"default_field":"name","query":"*"}}]}}}
|
|
|
-//{"query":{"bool":{"must":{"match":{"content":{"query":"fulltextsearch","operator":"and"}}},"should":[{"match":{"content":{"query":"Elasticsearch","boost":3}}},{"match":{"content":{"query":"Lucene","boost":2}}}]}}}
|
|
|
-//prefix
|
|
|
-//{"query":{"match":{"title":{"query":"brownfox","operator":"and"}}}} //默认为or
|
|
|
-//{"query":{"multi_match":{"query":"PolandStreetW1V","type":"most_fields","fields":["*_street","city^2","country","postcode"]}}}
|
|
|
-//{"query":{"wildcard":{"postcode":"W?F*HW"}}}
|
|
|
-//{"query":{"regexp":{"postcode":"W[0-9].+"}}}
|
|
|
-//{"query":{"filtered":{"filter":{"range":{"price":{"gte":10000}}}}},"aggs":{"single_avg_price":{"avg":{"field":"price"}}}}
|
|
|
-//{"query":{"match":{"make":"ford"}},"aggs":{"colors":{"terms":{"field":"color"}}}}//查fork有几种颜色
|
|
|
-//过滤器不会计算相关度的得分,所以它们在计算上更快一些
|
|
|
-//{"query":{"filtered":{"query":{"match_all":{}},"filter":{"range":{"balance":{"gte":20000,"lte":30000}}}}}}
|
|
|
-//{"query":{"match_all":{}},"from":10,"size":10,"_source":["account_number","balance"],"sort":{"balance":{"order":"desc"}}}
|
|
|
-//{"query":{"match_phrase":{"address":"milllane"}}}和match不同会去匹配整个短语,相当于must[]
|
|
|
-func Get(index, itype, query string) *[]map[string]interface{} {
|
|
|
+// 通用查询
|
|
|
+// {"query": {"bool":{"must":[{"query_string":{"default_field":"name","query":"*"}}]}}}
|
|
|
+// {"query":{"bool":{"must":{"match":{"content":{"query":"fulltextsearch","operator":"and"}}},"should":[{"match":{"content":{"query":"Elasticsearch","boost":3}}},{"match":{"content":{"query":"Lucene","boost":2}}}]}}}
|
|
|
+// prefix
|
|
|
+// {"query":{"match":{"title":{"query":"brownfox","operator":"and"}}}} //默认为or
|
|
|
+// {"query":{"multi_match":{"query":"PolandStreetW1V","type":"most_fields","fields":["*_street","city^2","country","postcode"]}}}
|
|
|
+// {"query":{"wildcard":{"postcode":"W?F*HW"}}}
|
|
|
+// {"query":{"regexp":{"postcode":"W[0-9].+"}}}
|
|
|
+// {"query":{"filtered":{"filter":{"range":{"price":{"gte":10000}}}}},"aggs":{"single_avg_price":{"avg":{"field":"price"}}}}
|
|
|
+// {"query":{"match":{"make":"ford"}},"aggs":{"colors":{"terms":{"field":"color"}}}}//查fork有几种颜色
|
|
|
+// 过滤器不会计算相关度的得分,所以它们在计算上更快一些
|
|
|
+// {"query":{"filtered":{"query":{"match_all":{}},"filter":{"range":{"balance":{"gte":20000,"lte":30000}}}}}}
|
|
|
+// {"query":{"match_all":{}},"from":10,"size":10,"_source":["account_number","balance"],"sort":{"balance":{"order":"desc"}}}
|
|
|
+// {"query":{"match_phrase":{"address":"milllane"}}}和match不同会去匹配整个短语,相当于must[]
|
|
|
+func Get(index string, query es.Query) *[]map[string]interface{} {
|
|
|
log.Println("query -- ", query)
|
|
|
client := GetEsConn()
|
|
|
defer func() {
|
|
@@ -153,7 +153,7 @@ func Get(index, itype, query string) *[]map[string]interface{} {
|
|
|
}
|
|
|
}
|
|
|
}()
|
|
|
- searchResult, err := client.Search().Index(index).Source(query).Do(context.Background())
|
|
|
+ searchResult, err := client.Search().Index(index).Query(query).Do(context.Background())
|
|
|
if err != nil {
|
|
|
log.Println("从ES查询出错", err.Error())
|
|
|
return nil
|
|
@@ -269,14 +269,7 @@ func GetNoLimit(index, itype, query string) *[]map[string]interface{} {
|
|
|
return &res
|
|
|
}
|
|
|
|
|
|
-//分页查询
|
|
|
-//{"name":"张三","$and":[{"age":{"$gt":10}},{"age":{"$lte":20}}]}
|
|
|
-//fields直接是 `"_id","title"`
|
|
|
-func GetPage(index, itype, query, order, field string, start, limit int) *[]map[string]interface{} {
|
|
|
- return Get(index, itype, MakeQuery(query, order, field, start, limit))
|
|
|
-}
|
|
|
-
|
|
|
-//openapi
|
|
|
+// openapi
|
|
|
func GetOAPage(index, itype, query, order, field string, start, limit int) (*[]map[string]interface{}, int) {
|
|
|
return GetOA(index, itype, MakeQuery(query, order, field, start, limit))
|
|
|
}
|
|
@@ -309,7 +302,7 @@ func MakeQuery(query, order, fileds string, start, limit int) string {
|
|
|
return ""
|
|
|
}
|
|
|
|
|
|
-//{"name":"aaa"}
|
|
|
+// {"name":"aaa"}
|
|
|
func AnalyQuery(query interface{}, parent string, result string) string {
|
|
|
m := make(map[string]interface{})
|
|
|
if q1, ok := query.(string); ok {
|
|
@@ -476,7 +469,7 @@ func GetByIdField(index, itype, id, fields string) *map[string]interface{} {
|
|
|
// return &res
|
|
|
//}
|
|
|
|
|
|
-//删除某个索引,根据查询
|
|
|
+// 删除某个索引,根据查询
|
|
|
func Del(index, itype string, query interface{}) bool {
|
|
|
client := GetEsConn()
|
|
|
defer DestoryEsConn(client)
|
|
@@ -593,7 +586,7 @@ func Del(index, itype string, query interface{}) bool {
|
|
|
// }
|
|
|
//}
|
|
|
|
|
|
-//根据id删除索引对象
|
|
|
+// 根据id删除索引对象
|
|
|
func DelById(index, itype, id string) bool {
|
|
|
client := GetEsConn()
|
|
|
defer DestoryEsConn(client)
|
|
@@ -622,7 +615,7 @@ func DelById(index, itype, id string) bool {
|
|
|
return b
|
|
|
}
|
|
|
|
|
|
-//把地市代码转为地市
|
|
|
+// 把地市代码转为地市
|
|
|
func getLoc(code string, res *map[string]string) (loc string) {
|
|
|
switch len(code) {
|
|
|
case 6:
|
|
@@ -638,7 +631,7 @@ func getLoc(code string, res *map[string]string) (loc string) {
|
|
|
return
|
|
|
}
|
|
|
|
|
|
-//把地市代码转为地市
|
|
|
+// 把地市代码转为地市
|
|
|
func Loop(m interface{}, res *map[string]string) {
|
|
|
m1, ok := m.([]interface{})
|
|
|
if !ok {
|
|
@@ -964,143 +957,7 @@ type KeyConfig struct {
|
|
|
Areas []string `json:"area"`
|
|
|
}
|
|
|
|
|
|
-//替换了"号
|
|
|
-func GetResForJY(index, itype string, keys []KeyConfig, allquery, findfields, SortQuery, fields string, start, limit int) *[]map[string]interface{} {
|
|
|
- if len(keys) > 0 {
|
|
|
- qstr := ""
|
|
|
- new_minq := fmt.Sprintf(minq, "%s", findfields)
|
|
|
- not_new_minq := fmt.Sprintf(minq, "%s", findfields) //排除词只查询标题
|
|
|
- musts := []string{}
|
|
|
- for _, qs_words := range keys {
|
|
|
- mq := []string{}
|
|
|
- notmq := []string{}
|
|
|
- for _, qs_word := range qs_words.Keys {
|
|
|
- mq = append(mq, fmt.Sprintf(new_minq, ReplaceYH(qs_word)))
|
|
|
- /*
|
|
|
- qs := AnalyzerWord("bidding", qs_word)
|
|
|
- for _, qw := range qs {
|
|
|
- mq = append(mq, fmt.Sprintf(new_minq, ReplaceYH(qw)))
|
|
|
- }
|
|
|
- */
|
|
|
- }
|
|
|
- for _, qs_word := range qs_words.NotKeys {
|
|
|
- notmq = append(notmq, fmt.Sprintf(not_new_minq, ReplaceYH(qs_word)))
|
|
|
- }
|
|
|
- if len(qs_words.Areas) > 0 {
|
|
|
- mq = append(mq, fmt.Sprintf(`{"terms":{"area":["%s"]}}`, strings.Join(qs_words.Areas, `","`)))
|
|
|
- }
|
|
|
- if len(qs_words.InfoTypes) > 0 {
|
|
|
- mq = append(mq, fmt.Sprintf(`{"terms":{"toptype":["%s"]}}`, strings.Join(qs_words.InfoTypes, `","`)))
|
|
|
- }
|
|
|
- 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:])
|
|
|
- ws := []string{}
|
|
|
- for _, w := range strings.Split(findfields, ",") {
|
|
|
- ws = append(ws, fmt.Sprintf(highlightStr, w, 1))
|
|
|
- }
|
|
|
- qstr = qstr[:len(qstr)-1] + `,` + fmt.Sprintf(HL, strings.Join(ws, ",")) + `}`
|
|
|
- if len(fields) > 0 {
|
|
|
- qstr = qstr[:len(qstr)-1] + `,"_source":[` + fields + "]}"
|
|
|
- }
|
|
|
- if len(SortQuery) > 0 {
|
|
|
- qstr = qstr[:len(qstr)-1] + `,"sort":` + SortQuery + `}`
|
|
|
- }
|
|
|
- if start > -1 {
|
|
|
- qstr = qstr[:len(qstr)-1] + `,"from":` + strconv.Itoa(start) + `,"size":` + strconv.Itoa(limit) + "}"
|
|
|
- }
|
|
|
- //log.Println("jy-ngram-find", qstr)
|
|
|
- return Get(index, itype, qstr)
|
|
|
- } else {
|
|
|
- return nil
|
|
|
- }
|
|
|
-}
|
|
|
-
|
|
|
-func ReplaceYH(src string) (rpl string) {
|
|
|
- return strings.Replace(src, `"`, `\"`, -1)
|
|
|
-}
|
|
|
-
|
|
|
-//
|
|
|
-func GetAllByNgram(index, itype, qstr, findfields, order, fields string, start, limit, count int, highlight bool) *[]map[string]interface{} {
|
|
|
- if qstr != "" {
|
|
|
- if highlight {
|
|
|
- ws := []string{}
|
|
|
- for _, w := range strings.Split(findfields, ",") {
|
|
|
- ws = append(ws, fmt.Sprintf(highlightStr, w, count))
|
|
|
- }
|
|
|
- qstr = qstr[:len(qstr)-1] + `,` + fmt.Sprintf(HL, strings.Join(ws, ",")) + `}`
|
|
|
- }
|
|
|
- if len(fields) > 0 {
|
|
|
- qstr = qstr[:len(qstr)-1] + `,"_source":[` + fields + "]}"
|
|
|
- }
|
|
|
- if len(order) > 0 {
|
|
|
- qstr = qstr[:len(qstr)-1] + `,"sort":[` + SR(SR(SR(SR(order, ",", "},{", -1), " ", "", -1), ":-1", `:"desc"`, -1), ":1", `:"asc"`, -1) + `]}`
|
|
|
- }
|
|
|
- if start > -1 {
|
|
|
- qstr = qstr[:len(qstr)-1] + `,"from":` + strconv.Itoa(start) + `,"size":` + strconv.Itoa(limit) + "}"
|
|
|
- }
|
|
|
- log.Println("GetAllByNgram:", qstr)
|
|
|
- return Get(index, itype, qstr)
|
|
|
- } else {
|
|
|
- return nil
|
|
|
- }
|
|
|
-}
|
|
|
-
|
|
|
-//数据标记2019-07-10
|
|
|
-func GetAllByNgram_MP(index, itype, qstr, findfields, order, fields string, start, limit, count int, highlight bool) *[]map[string]interface{} {
|
|
|
- if qstr != "" {
|
|
|
- if highlight {
|
|
|
- ws := []string{}
|
|
|
- for _, w := range strings.Split(findfields, ",") {
|
|
|
- ws = append(ws, fmt.Sprintf(highlightStr, w, count))
|
|
|
- }
|
|
|
- qstr = qstr[:len(qstr)-1] + `,` + fmt.Sprintf(HL_MP, strings.Join(ws, ",")) + `}`
|
|
|
- }
|
|
|
- if len(fields) > 0 {
|
|
|
- qstr = qstr[:len(qstr)-1] + `,"_source":[` + fields + "]}"
|
|
|
- }
|
|
|
- if len(order) > 0 {
|
|
|
- qstr = qstr[:len(qstr)-1] + `,"sort":[` + SR(SR(SR(SR(order, ",", "},{", -1), " ", "", -1), ":-1", `:"desc"`, -1), ":1", `:"asc"`, -1) + `]}`
|
|
|
- }
|
|
|
- if start > -1 {
|
|
|
- qstr = qstr[:len(qstr)-1] + `,"from":` + strconv.Itoa(start) + `,"size":` + strconv.Itoa(limit) + "}"
|
|
|
- }
|
|
|
- // log.Println("GetAllByNgram:", qstr)
|
|
|
- return Get(index, itype, qstr)
|
|
|
- } else {
|
|
|
- return nil
|
|
|
- }
|
|
|
-}
|
|
|
-
|
|
|
-//ik 分词
|
|
|
-func GetAllByIk(index, itype, qstr, findfields, order, fields string, start, limit, count int, highlight bool) *[]map[string]interface{} {
|
|
|
- if qstr != "" {
|
|
|
- if highlight {
|
|
|
- ws := []string{}
|
|
|
- for _, w := range strings.Split(findfields, ",") {
|
|
|
- ws = append(ws, fmt.Sprintf(ik_highlightStr, w, count))
|
|
|
- }
|
|
|
- qstr = qstr[:len(qstr)-1] + `,` + fmt.Sprintf(HL_IK, strings.Join(ws, ",")) + `}`
|
|
|
- }
|
|
|
- if len(fields) > 0 {
|
|
|
- qstr = qstr[:len(qstr)-1] + `,"_source":[` + fields + "]}"
|
|
|
- }
|
|
|
- if len(order) > 0 {
|
|
|
- qstr = qstr[:len(qstr)-1] + `,"sort":[` + SR(SR(SR(SR(order, ",", "},{", -1), " ", "", -1), ":-1", `:"desc"`, -1), ":1", `:"asc"`, -1) + `]}`
|
|
|
- }
|
|
|
- if start > -1 {
|
|
|
- qstr = qstr[:len(qstr)-1] + `,"from":` + strconv.Itoa(start) + `,"size":` + strconv.Itoa(limit) + "}"
|
|
|
- }
|
|
|
- //log.Println("GetAllByNgram:", qstr)
|
|
|
- return Get(index, itype, qstr)
|
|
|
- } else {
|
|
|
- return nil
|
|
|
- }
|
|
|
-}
|
|
|
-
|
|
|
-//分词
|
|
|
+// 分词
|
|
|
func AnalyzerWord(index, word string) (result []string) {
|
|
|
client := GetEsConn()
|
|
|
defer DestoryEsConn(client)
|
|
@@ -1140,49 +997,3 @@ func AnalyzerWord(index, word string) (result []string) {
|
|
|
}
|
|
|
return
|
|
|
}
|
|
|
-
|
|
|
-func GetResForJYView(index, itype string, keys []KeyConfig, allquery, findfields, SortQuery, fields string, start, limit int) *[]map[string]interface{} {
|
|
|
- if len(keys) > 0 {
|
|
|
- qstr := ""
|
|
|
- new_minq := fmt.Sprintf(minq, "%s", findfields)
|
|
|
- not_new_minq := fmt.Sprintf(minq, "%s", findfields) //排除词只查询标题
|
|
|
- musts := []string{}
|
|
|
- for _, qs_words := range keys {
|
|
|
- mq := []string{}
|
|
|
- notmq := []string{}
|
|
|
- for _, qs_word := range qs_words.Keys {
|
|
|
- mq = append(mq, fmt.Sprintf(new_minq, ReplaceYH(qs_word)))
|
|
|
- }
|
|
|
- for _, qs_word := range qs_words.NotKeys {
|
|
|
- notmq = append(notmq, fmt.Sprintf(not_new_minq, ReplaceYH(qs_word)))
|
|
|
- }
|
|
|
- if len(qs_words.Areas) > 0 {
|
|
|
- mq = append(mq, fmt.Sprintf(`{"terms":{"area":["%s"]}}`, strings.Join(qs_words.Areas, `","`)))
|
|
|
- }
|
|
|
- if len(qs_words.InfoTypes) > 0 {
|
|
|
- mq = append(mq, fmt.Sprintf(`{"terms":{"toptype":["%s"]}}`, strings.Join(qs_words.InfoTypes, `","`)))
|
|
|
- }
|
|
|
- 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:])
|
|
|
- ws := []string{}
|
|
|
- for _, w := range strings.Split(findfields, ",") {
|
|
|
- ws = append(ws, fmt.Sprintf(highlightStr, w, 1))
|
|
|
- }
|
|
|
- qstr = qstr[:len(qstr)-1] + `,` + fmt.Sprintf(HL, strings.Join(ws, ",")) + `}`
|
|
|
- if len(fields) > 0 {
|
|
|
- qstr = qstr[:len(qstr)-1] + `,"_source":[` + fields + "]}"
|
|
|
- }
|
|
|
- if len(SortQuery) > 0 {
|
|
|
- qstr = qstr[:len(qstr)-1] + `,"sort":` + SortQuery + `}`
|
|
|
- }
|
|
|
- if start > -1 {
|
|
|
- qstr = qstr[:len(qstr)-1] + `,"from":` + strconv.Itoa(start) + `,"size":` + strconv.Itoa(limit) + "}"
|
|
|
- }
|
|
|
- return Get(index, itype, qstr)
|
|
|
- } else {
|
|
|
- return nil
|
|
|
- }
|
|
|
-}
|