|
@@ -16,7 +16,7 @@ import (
|
|
|
)
|
|
|
|
|
|
const (
|
|
|
- esv7_filterQuery = `{"query": {"filtered": {"filter": {"bool": {"must": [%s]}},%s}}`
|
|
|
+ esv7_FilterQuery = `{"query": {"filtered": {"filter": {"bool": {"must": [%s]}},%s}}`
|
|
|
)
|
|
|
|
|
|
type EsV7 struct {
|
|
@@ -31,7 +31,7 @@ type EsV7 struct {
|
|
|
|
|
|
//初始化es,带有用户名密码认证
|
|
|
func (e *EsV7) Init() {
|
|
|
- filterQuery = esv7_filterQuery
|
|
|
+ FilterQuery = esv7_FilterQuery
|
|
|
if e.Size <= 0 {
|
|
|
e.Size = SIZE
|
|
|
}
|
|
@@ -736,7 +736,7 @@ func (e *EsV7) GetByNgramOther(index, itype string, query interface{}, mustquery
|
|
|
qstr := ""
|
|
|
if mustquery != "" && filtermode {
|
|
|
qstr = e.GetNgramQuery(query, "", findfields)
|
|
|
- qstr = fmt.Sprintf(filterQuery, mustquery, qstr[1:])
|
|
|
+ qstr = fmt.Sprintf(FilterQuery, mustquery, qstr[1:])
|
|
|
} else {
|
|
|
qstr = e.GetNgramQuery(query, mustquery, findfields)
|
|
|
}
|
|
@@ -771,7 +771,7 @@ func (e *EsV7) GetByNgramAll(index, itype string, query interface{}, mustquery,
|
|
|
qstr := ""
|
|
|
if mustquery != "" && filtermode {
|
|
|
qstr = e.GetNgramQuery(query, "", findfields)
|
|
|
- qstr = fmt.Sprintf(filterQuery, mustquery, qstr[1:])
|
|
|
+ qstr = fmt.Sprintf(FilterQuery, mustquery, qstr[1:])
|
|
|
} else {
|
|
|
qstr = e.GetNgramQuery(query, mustquery, findfields)
|
|
|
}
|
|
@@ -866,7 +866,7 @@ func (e *EsV7) GetResForJY(index, itype string, keys []KeyConfig, allquery, find
|
|
|
}
|
|
|
qstr = fmt.Sprintf(NgramStr, "", strings.Join(musts, ","))
|
|
|
|
|
|
- qstr = fmt.Sprintf(filterQuery, allquery, qstr[1:])
|
|
|
+ qstr = fmt.Sprintf(FilterQuery, allquery, qstr[1:])
|
|
|
ws := []string{}
|
|
|
for _, w := range strings.Split(findfields, ",") {
|
|
|
ws = append(ws, fmt.Sprintf(highlightStr, w, 1))
|
|
@@ -991,7 +991,7 @@ func (e *EsV7) GetResForJYView(index, itype string, keys []KeyConfig, allquery,
|
|
|
}
|
|
|
qstr = fmt.Sprintf(NgramStr, "", strings.Join(musts, ","))
|
|
|
|
|
|
- qstr = fmt.Sprintf(filterQuery, allquery, qstr[1:])
|
|
|
+ qstr = fmt.Sprintf(FilterQuery, allquery, qstr[1:])
|
|
|
ws := []string{}
|
|
|
for _, w := range strings.Split(findfields, ",") {
|
|
|
ws = append(ws, fmt.Sprintf(highlightStr, w, 1))
|
|
@@ -1011,3 +1011,55 @@ func (e *EsV7) GetResForJYView(index, itype string, keys []KeyConfig, allquery,
|
|
|
return nil
|
|
|
}
|
|
|
}
|
|
|
+
|
|
|
+//返回count 和 res
|
|
|
+func (e *EsV7) GetWithCount(index, itype, query string) (int64, *[]map[string]interface{}) {
|
|
|
+ client := e.GetEsConn()
|
|
|
+ defer func() {
|
|
|
+ go e.DestoryEsConn(client)
|
|
|
+ }()
|
|
|
+ var res []map[string]interface{}
|
|
|
+ var count int64 = 0
|
|
|
+ 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).Source(query).Do(context.TODO())
|
|
|
+ if err != nil {
|
|
|
+ log.Println("从ES查询出错", err.Error())
|
|
|
+ return count, nil
|
|
|
+ }
|
|
|
+ count = searchResult.TotalHits()
|
|
|
+ if searchResult.Hits != nil {
|
|
|
+ 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 res[i] != nil {
|
|
|
+ res[i]["_id"] = hit.Id
|
|
|
+ }
|
|
|
+ if parseErr == nil && hit.Highlight != nil && res[i] != nil {
|
|
|
+ res[i]["highlight"] = map[string][]string(hit.Highlight)
|
|
|
+ }
|
|
|
+ }
|
|
|
+ } else {
|
|
|
+ log.Println("查询结果太多,查询到:", resNum, "条")
|
|
|
+ }
|
|
|
+
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return count, &res
|
|
|
+}
|