|
@@ -0,0 +1,54 @@
|
|
|
+package es
|
|
|
+
|
|
|
+import (
|
|
|
+ elastic "app.yhyue.com/moapp/jybase/esv1"
|
|
|
+ "fmt"
|
|
|
+ "strconv"
|
|
|
+ "strings"
|
|
|
+)
|
|
|
+
|
|
|
+const (
|
|
|
+ highlightStr = `%s: {"fragment_size": %d,"number_of_fragments": 1}`
|
|
|
+ HL = `"highlight": {"pre_tags": [""],"post_tags": [""],"fields": {%s}}`
|
|
|
+)
|
|
|
+
|
|
|
+var SR = strings.Replace
|
|
|
+
|
|
|
+type EsSearch struct {
|
|
|
+ Index string
|
|
|
+ Itype string
|
|
|
+ Query string
|
|
|
+ FindFields string
|
|
|
+ Order string
|
|
|
+ Fields string
|
|
|
+ Start int
|
|
|
+ Limit int
|
|
|
+ Count int
|
|
|
+ HighLight bool
|
|
|
+}
|
|
|
+
|
|
|
+//
|
|
|
+func (e *EsSearch) GetAllByNgramWithCount() (int64, *[]map[string]interface{}) {
|
|
|
+ qstr := e.Query
|
|
|
+ if e.Query != "" {
|
|
|
+ if e.HighLight {
|
|
|
+ ws := []string{}
|
|
|
+ for _, w := range strings.Split(e.Fields, ",") {
|
|
|
+ ws = append(ws, fmt.Sprintf(highlightStr, w, e.Count))
|
|
|
+ }
|
|
|
+ qstr = qstr[:len(qstr)-1] + `,` + fmt.Sprintf(HL, strings.Join(ws, ",")) + `}`
|
|
|
+ }
|
|
|
+ if len(e.Fields) > 0 {
|
|
|
+ qstr = qstr[:len(qstr)-1] + `,"_source":[` + e.Fields + "]}"
|
|
|
+ }
|
|
|
+ if len(e.Order) > 0 {
|
|
|
+ qstr = qstr[:len(qstr)-1] + `,"sort":[` + SR(SR(SR(SR(e.Order, ",", "},{", -1), " ", "", -1), ":-1", `:"desc"`, -1), ":1", `:"asc"`, -1) + `]}`
|
|
|
+ }
|
|
|
+ if e.Start > -1 {
|
|
|
+ qstr = qstr[:len(qstr)-1] + `,"from":` + strconv.Itoa(e.Start) + `,"size":` + strconv.Itoa(e.Limit) + "}"
|
|
|
+ }
|
|
|
+ return elastic.GetWithCount(e.Index, e.Itype, qstr)
|
|
|
+ } else {
|
|
|
+ return 0, nil
|
|
|
+ }
|
|
|
+}
|