|
@@ -210,6 +210,60 @@ func Get(index, itype, query string) *[]map[string]interface{} {
|
|
|
}
|
|
|
return &res
|
|
|
}
|
|
|
+
|
|
|
+// GetNew 返回查询结果数量总数
|
|
|
+func GetNew(index, itype, query string) (*[]map[string]interface{}, int64) {
|
|
|
+ //log.Println("query -- ", query)
|
|
|
+ client := GetEsConn()
|
|
|
+ defer func() {
|
|
|
+ go DestoryEsConn(client)
|
|
|
+ }()
|
|
|
+ var (
|
|
|
+ res []map[string]interface{}
|
|
|
+ count int64
|
|
|
+ )
|
|
|
+ if client != nil {
|
|
|
+ defer func() {
|
|
|
+ if r := recover(); r != nil {
|
|
|
+ log.Println("[E]", r)
|
|
|
+ for skip := 1; ; skip++ {
|
|
|
+ _, file, line, ok := runtime.Caller(skip)
|
|
|
+ if !ok {
|
|
|
+ break
|
|
|
+ }
|
|
|
+ go log.Printf("%v,%v\n", file, line)
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }()
|
|
|
+ searchResult, err := client.Search().Index(index).Type(itype).Source(query).Do()
|
|
|
+ if err != nil {
|
|
|
+ log.Println("从ES查询出错", err.Error())
|
|
|
+ return nil, count
|
|
|
+ }
|
|
|
+
|
|
|
+ if searchResult.Hits != nil {
|
|
|
+ count = searchResult.Hits.TotalHits
|
|
|
+ resNum := len(searchResult.Hits.Hits)
|
|
|
+ if resNum < 5000 {
|
|
|
+ res = make([]map[string]interface{}, resNum)
|
|
|
+ for i, hit := range searchResult.Hits.Hits {
|
|
|
+ //d := json.NewDecoder(bytes.NewBuffer(*hit.Source))
|
|
|
+ //d.UseNumber()
|
|
|
+ //d.Decode(&res[i])
|
|
|
+ parseErr := json.Unmarshal(*hit.Source, &res[i])
|
|
|
+ if parseErr == nil && hit.Highlight != nil && res[i] != nil {
|
|
|
+ res[i]["highlight"] = map[string][]string(hit.Highlight)
|
|
|
+ }
|
|
|
+ }
|
|
|
+ } else {
|
|
|
+ log.Println("查询结果太多,查询到:", resNum, "条")
|
|
|
+ }
|
|
|
+
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return &res, count
|
|
|
+}
|
|
|
+
|
|
|
func GetOA(index, itype, query string) (*[]map[string]interface{}, int) {
|
|
|
//log.Println("query -- ", query)
|
|
|
client := GetEsConn()
|
|
@@ -1213,6 +1267,32 @@ func ReplaceYH(src string) (rpl string) {
|
|
|
return strings.Replace(src, `"`, `\"`, -1)
|
|
|
}
|
|
|
|
|
|
+//
|
|
|
+func GetAllByNgramNew(index, itype, qstr, findfields, order, fields string, start, limit, count int, highlight bool) (*[]map[string]interface{}, int64) {
|
|
|
+ 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 GetNew(index, itype, qstr)
|
|
|
+ } else {
|
|
|
+ return nil, 0
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
//
|
|
|
func GetAllByNgram(index, itype, qstr, findfields, order, fields string, start, limit, count int, highlight bool) *[]map[string]interface{} {
|
|
|
if qstr != "" {
|