Explorar el Código

feat:增加方法

wangchuanjin hace 2 años
padre
commit
a9c3f41874
Se han modificado 4 ficheros con 108 adiciones y 10 borrados
  1. 68 0
      es/es.go
  2. 9 10
      es/es_test.go
  3. 14 0
      es/esv1.go
  4. 17 0
      es/esv7.go

+ 68 - 0
es/es.go

@@ -1,12 +1,18 @@
 package es
 
 import (
+	"crypto/rand"
 	"encoding/json"
 	"fmt"
+	"io/ioutil"
 	"log"
+	"math/big"
+	"net/http"
 	"runtime"
 	"strings"
+	"unicode"
 
+	util "app.yhyue.com/moapp/jybase/common"
 	esV7 "github.com/olivere/elastic/v7"
 )
 
@@ -68,6 +74,8 @@ type Es interface {
 	GetResForJYView(index, itype string, keys []KeyConfig, allquery, findfields, SortQuery, fields string, start, limit int) *[]map[string]interface{}
 	GetWithCount(index, itype, query string) (int64, *[]map[string]interface{})
 	GetAggs(index, itype, query string) (aggs esV7.Aggregations, count int64, res []map[string]interface{})
+	Analyze(text, index, analyzer string) (res string)
+	analyzeResp(text, analyzer, u string) (*http.Request, error)
 }
 
 var (
@@ -135,6 +143,63 @@ func bsonIdToSId(_id interface{}) string {
 	return v
 }
 
+type analyze struct {
+}
+
+func (a *analyze) do(text, index, analyzer, address string, e Es) string {
+	var addrs []string
+	for _, s := range strings.Split(address, ",") {
+		addrs = append(addrs, s)
+	}
+	i, _ := rand.Int(rand.Reader, big.NewInt(int64(len(addrs)))) //随机
+	res, err := a.post(text, analyzer, addrs[int(i.Int64())]+"/"+index+"/_analyze", e)
+	if err != nil {
+		for _, v := range addrs {
+			res, err = a.post(text, analyzer, v+"/"+index+"/_analyze", e)
+			if err == nil {
+				return res
+			}
+		}
+	}
+	return res
+}
+
+func (a *analyze) post(text, analyzer, url string, e Es) (string, error) {
+	req, err := e.analyzeResp(text, analyzer, url)
+	if err != nil {
+		log.Println("es分词req出错:", err)
+		return "", err
+	}
+	resp, err := http.DefaultClient.Do(req)
+	if err != nil {
+		log.Println("es分词do出错:", err)
+		return "", err
+	}
+	result, err := ioutil.ReadAll(resp.Body)
+	if err != nil {
+		log.Println("es分词read出错:", err)
+		return "", err
+	}
+	defer resp.Body.Close()
+	var resmap map[string]interface{}
+	json.Unmarshal(result, &resmap)
+	res := ""
+	if resmap != nil && resmap["tokens"] != nil {
+		tokens := util.ObjArrToMapArr(resmap["tokens"].([]interface{}))
+		for _, v := range tokens {
+			token := util.ObjToString(v["token"])
+			if len([]rune(token)) == 1 && !unicode.Is(unicode.Scripts["Han"], []rune(token)[0]) { //(P260保留单个汉字)
+				continue
+			}
+			if res != "" {
+				res += "+"
+			}
+			res += token
+		}
+	}
+	return res, nil
+}
+
 func NewEs(version, address string, size int, userName, password string) Es {
 	var es Es
 	if version == "v1" {
@@ -247,3 +312,6 @@ func GetWithCount(index, itype, query string) (int64, *[]map[string]interface{})
 func GetAggs(index, itype, query string) (aggs esV7.Aggregations, count int64, res []map[string]interface{}) {
 	return VarEs.GetAggs(index, itype, query)
 }
+func Analyze(text, index, analyzer string) (res string) {
+	return VarEs.Analyze(text, index, analyzer)
+}

+ 9 - 10
es/es_test.go

@@ -1,7 +1,6 @@
 package es
 
 import (
-	"encoding/json"
 	"log"
 	"testing"
 )
@@ -17,13 +16,13 @@ type AggregationsBucket struct {
 func TestGet(t *testing.T) {
 	NewEs("v7", "http://192.168.3.241:9205,http://192.168.3.149:9200", 2, "", "")
 	//NewEs("v1", "http://192.168.3.206:9800", 2, "", "")
-	aggs, _, _ := GetAggs("projectset", "projectset", `{"query":{"bool":{"must":[{"range":{"firsttime":{"gte":1609430400,"lt":1681891594}}}],"must_not": [{"term": {"buyer": ""}}],"should":[{"bool": {"must": [{"multi_match": {"query": "实验室","type": "phrase", "fields": ["projectname.pname","purchasing"]}}]}}],"minimum_should_match": 1}},"size": 0,"aggs": {"group_field": {"terms": {"field": "entidlist","size": 1000},"aggs": {"count": {"sum": {"field": "bidamount"}}}}}}`)
-	var aggsMap []*AggregationsBucket
-	if aggsMap_g, ok := aggs.Children("group_field"); ok {
-		bs, _ := aggsMap_g.Aggregations["buckets"].MarshalJSON()
-		if len(bs) > 0 {
-			json.Unmarshal(bs, &aggsMap)
-			log.Println(aggsMap)
-		}
-	}
+	_, res := GetWithCount("bidding", "bidding", `{"query":{"bool":{"must":[{"range":{"publishtime":{"gte":1524141314,"lt":1681907714}}},{"bool":{"must":[{"multi_match": {"query": "软件","type": "phrase", "fields": ["title"]}}]}}],"must_not":[]}},"_source":["_id","title","publishtime","dataweight","toptype","subtype","type","area","city","s_subscopeclass","bidamount","budget","buyerclass","spidercode","site","projectname","projectcode","buyer","winner","bidopentime","buyertel","buyerperson","agency","agencytel","agencyperson","s_winner","winnertel","winnerperson","signendtime","bidendtime","projectinfo","entidlist","district","sinendtime","buyeraddr","isValidFile"],"sort":[{"dataweight":"desc"},{"publishtime":"desc"}],"from":0,"size":50}`)
+	log.Println(res)
+}
+
+func TestAnalyze(t *testing.T) {
+	NewEs("v7", "http://192.168.3.241:9205,http://192.168.3.149:9200", 2, "", "")
+	//NewEs("v1", "http://192.168.3.206:9800", 2, "", "")
+	res := Analyze("软件中国", "bidding", "ik_smart")
+	log.Println(res)
 }

+ 14 - 0
es/esv1.go

@@ -4,6 +4,7 @@ import (
 	"encoding/json"
 	"fmt"
 	"log"
+	"net/http"
 	"net/url"
 	"reflect"
 	"runtime"
@@ -1276,3 +1277,16 @@ func (e *EsV1) GetAggs(index, itype, query string) (aggs esV7.Aggregations, coun
 	}
 	return
 }
+
+func (e *EsV1) Analyze(text, index, analyzer string) (res string) {
+	return (&analyze{}).do(text, index, analyzer, e.Address, e)
+}
+
+func (e *EsV1) analyzeResp(text, analyzer, u string) (*http.Request, error) {
+	up, _ := url.Parse(u)
+	query := up.Query()
+	query.Add("text", text)
+	query.Add("analyzer", analyzer)
+	up.RawQuery = query.Encode()
+	return http.NewRequest("GET", up.String(), nil)
+}

+ 17 - 0
es/esv7.go

@@ -5,6 +5,7 @@ import (
 	"encoding/json"
 	"fmt"
 	"log"
+	"net/http"
 	"reflect"
 	"runtime"
 	"strconv"
@@ -1067,3 +1068,19 @@ func (e *EsV7) GetAggs(index, itype, query string) (aggs es.Aggregations, count
 	}
 	return
 }
+
+func (e *EsV7) Analyze(text, index, analyzer string) (res string) {
+	return (&analyze{}).do(text, index, analyzer, e.Address, e)
+}
+
+func (e *EsV7) analyzeResp(text, analyzer, u string) (*http.Request, error) {
+	req, err := http.NewRequest("POST", u, strings.NewReader(fmt.Sprintf(`{"analyzer":"%s","text":"%s"}`, analyzer, text)))
+	if err != nil {
+		return req, err
+	}
+	if e.UserName != "" && e.Password != "" {
+		req.SetBasicAuth(e.UserName, e.Password)
+	}
+	req.Header.Set("Content-Type", "application/json")
+	return req, err
+}