소스 검색

feat:增加方法

wangchuanjin 2 년 전
부모
커밋
5bd61163c1
3개의 변경된 파일65개의 추가작업 그리고 13개의 파일을 삭제
  1. 1 1
      es/es.go
  2. 6 6
      es/esv1.go
  3. 58 6
      es/esv7.go

+ 1 - 1
es/es.go

@@ -66,7 +66,7 @@ type Es interface {
 }
 
 var (
-	filterQuery string
+	FilterQuery string
 	SR          = strings.Replace
 	VarEs       Es
 )

+ 6 - 6
es/esv1.go

@@ -16,7 +16,7 @@ import (
 )
 
 const (
-	esv1_filterQuery = `{"query": {"filtered": {"filter": {"bool": {"must": [%s]}},%s}}`
+	esv1_FilterQuery = `{"query": {"filtered": {"filter": {"bool": {"must": [%s]}},%s}}`
 )
 
 type EsV1 struct {
@@ -29,7 +29,7 @@ type EsV1 struct {
 
 //n倍的池
 func (e *EsV1) Init() {
-	filterQuery = esv1_filterQuery
+	FilterQuery = esv1_FilterQuery
 	if e.Size <= 0 {
 		e.Size = SIZE
 	}
@@ -878,7 +878,7 @@ func (e *EsV1) 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)
 	}
@@ -913,7 +913,7 @@ func (e *EsV1) 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)
 	}
@@ -1008,7 +1008,7 @@ func (e *EsV1) 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))
@@ -1169,7 +1169,7 @@ func (e *EsV1) 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))

+ 58 - 6
es/esv7.go

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