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