init: web_hgcg_server 初始化提交
Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
This commit is contained in:
@@ -0,0 +1,192 @@
|
||||
package esmanager
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
"io"
|
||||
"net/http"
|
||||
"strings"
|
||||
|
||||
"github.com/elastic/go-elasticsearch/v7/esapi"
|
||||
)
|
||||
|
||||
const (
|
||||
AnalyzerIkSmart = "ik_smart"
|
||||
AnalyzerIkMaxWord = "ik_max_word"
|
||||
NumberOfShards = 1
|
||||
NumberOfReplicas = 1
|
||||
)
|
||||
|
||||
func (m *Manager) InitIndices(indexName string, setting M) error {
|
||||
existsRes, err := m.client.Indices.Exists([]string{indexName})
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer existsRes.Body.Close()
|
||||
if existsRes.StatusCode == http.StatusOK {
|
||||
return nil
|
||||
}
|
||||
if existsRes.StatusCode != http.StatusNotFound {
|
||||
body, _ := io.ReadAll(existsRes.Body)
|
||||
return errors.New(fmt.Sprintf("%+v body:%s", existsRes, string(body)))
|
||||
}
|
||||
res, err := m.client.Indices.Create(indexName, func(request *esapi.IndicesCreateRequest) {
|
||||
request.Body = marshalSearchM(setting)
|
||||
})
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer res.Body.Close()
|
||||
if res.StatusCode == http.StatusOK || res.StatusCode == http.StatusCreated {
|
||||
return nil
|
||||
}
|
||||
body, _ := io.ReadAll(res.Body)
|
||||
if res.StatusCode == http.StatusBadRequest && strings.Contains(string(body), "resource_already_exists_exception") {
|
||||
return nil
|
||||
}
|
||||
return errors.New(fmt.Sprintf("%+v body:%s", res, string(body)))
|
||||
}
|
||||
|
||||
// GetTokens 获取分词
|
||||
func (m *Manager) Tokens(index string, bind interface{}, query M, o ...func(*esapi.IndicesAnalyzeRequest)) error {
|
||||
o = append(o,
|
||||
m.client.Indices.Analyze.WithContext(context.Background()),
|
||||
m.client.Indices.Analyze.WithIndex(index),
|
||||
m.client.Indices.Analyze.WithBody(marshalSearchM(query)),
|
||||
m.client.Indices.Analyze.WithPretty(),
|
||||
)
|
||||
|
||||
res, err := m.client.Indices.Analyze(o...)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer res.Body.Close()
|
||||
err = unmarshalAnalyzeBody(bind, res)
|
||||
return err
|
||||
}
|
||||
|
||||
// search 搜索
|
||||
func (m *Manager) Search(index string, bind interface{}, query M, o ...func(*esapi.SearchRequest)) error {
|
||||
marshalSearchM(query)
|
||||
o = append(o,
|
||||
m.client.Search.WithContext(context.Background()),
|
||||
m.client.Search.WithIndex(index),
|
||||
m.client.Search.WithBody(marshalSearchM(query)),
|
||||
m.client.Search.WithTrackTotalHits(true),
|
||||
m.client.Search.WithPretty(),
|
||||
)
|
||||
res, err := m.client.Search(o...)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer res.Body.Close()
|
||||
if res.StatusCode != http.StatusOK {
|
||||
return errors.New(fmt.Sprintf("%+v", res))
|
||||
}
|
||||
err = unmarshalBody(bind, res)
|
||||
return err
|
||||
}
|
||||
|
||||
// 批量插入
|
||||
func (m *Manager) Bulk(index string, source M, o ...func(*esapi.BulkRequest)) error {
|
||||
o = append(o,
|
||||
m.client.Bulk.WithIndex(index),
|
||||
m.client.Bulk.WithContext(context.Background()),
|
||||
m.client.Bulk.WithRefresh("true"),
|
||||
)
|
||||
res, err := m.client.Bulk(marshalBulkM(source), o...)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer res.Body.Close()
|
||||
if res.StatusCode != http.StatusOK {
|
||||
return errors.New(fmt.Sprintf("%+v", res))
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// Count 获取数量
|
||||
func (m *Manager) Count(index string, query M, o ...func(*esapi.CountRequest)) (int64, error) {
|
||||
o = append(o,
|
||||
m.client.Count.WithIndex(index),
|
||||
m.client.Count.WithBody(marshalSearchM(query)),
|
||||
m.client.Count.WithContext(context.Background()),
|
||||
)
|
||||
res, err := m.client.Count(o...)
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
defer res.Body.Close()
|
||||
if res.StatusCode != http.StatusOK {
|
||||
return 0, errors.New(fmt.Sprintf("%+v", res))
|
||||
}
|
||||
type Bind struct {
|
||||
Count int64 `json:"count"`
|
||||
}
|
||||
var bind = Bind{}
|
||||
err = unmarshalStruct(&bind, res)
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
return bind.Count, err
|
||||
}
|
||||
|
||||
func (m *Manager) Get(index string, id string, bind interface{}, o ...func(*esapi.GetRequest)) error {
|
||||
o = append(o,
|
||||
m.client.Get.WithContext(context.Background()),
|
||||
m.client.Get.WithPretty(),
|
||||
)
|
||||
res, err := m.client.Get(index, id, o...)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer res.Body.Close()
|
||||
if res.StatusCode != http.StatusOK {
|
||||
return errors.New(fmt.Sprintf("%+v", res))
|
||||
}
|
||||
return unmarshalBody(bind, res)
|
||||
}
|
||||
|
||||
func (m *Manager) Add(index string, id string, doc interface{}, o ...func(*esapi.IndexRequest)) error {
|
||||
o = append(o,
|
||||
m.client.Index.WithDocumentID(id),
|
||||
m.client.Index.WithContext(context.Background()),
|
||||
m.client.Index.WithPretty(),
|
||||
m.client.Index.WithRefresh("true"),
|
||||
)
|
||||
res, err := m.client.Index(index, marshalStruct(doc), o...)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer res.Body.Close()
|
||||
return nil
|
||||
}
|
||||
|
||||
func (m *Manager) Del(index string, id string, o ...func(*esapi.DeleteRequest)) error {
|
||||
o = append(o,
|
||||
m.client.Delete.WithContext(context.Background()),
|
||||
m.client.Delete.WithPretty(),
|
||||
m.client.Delete.WithRefresh("true"),
|
||||
)
|
||||
res, err := m.client.Delete(index, id, o...)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer res.Body.Close()
|
||||
return nil
|
||||
}
|
||||
|
||||
func (m *Manager) DelMany(indexs []string, query M, o ...func(*esapi.DeleteByQueryRequest)) error {
|
||||
o = append(o,
|
||||
m.client.DeleteByQuery.WithContext(context.Background()),
|
||||
m.client.DeleteByQuery.WithPretty(),
|
||||
m.client.DeleteByQuery.WithRefresh(true),
|
||||
)
|
||||
res, err := m.client.DeleteByQuery(indexs, marshalSearchM(query), o...)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer res.Body.Close()
|
||||
return nil
|
||||
}
|
||||
@@ -0,0 +1,143 @@
|
||||
package esmanager
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"io"
|
||||
"time"
|
||||
|
||||
"github.com/elastic/go-elasticsearch/v7/esapi"
|
||||
)
|
||||
|
||||
type M map[string]interface{}
|
||||
|
||||
type A []map[string]interface{}
|
||||
|
||||
type MGetBody struct {
|
||||
ID string `json:"_id"`
|
||||
}
|
||||
|
||||
// ElasticResp 响应
|
||||
type Response = esapi.Response
|
||||
|
||||
type Options struct {
|
||||
Address []string
|
||||
MaxIdleConnPerHost int
|
||||
IdleConnTimeout time.Duration
|
||||
}
|
||||
|
||||
func unmarshalAggregateBody(bind interface{}, resp *Response) error {
|
||||
var v map[string]interface{}
|
||||
defer func() { _ = resp.Body.Close() }()
|
||||
if bind == nil { //传入空表示只执行 defer
|
||||
return nil
|
||||
}
|
||||
err := json.NewDecoder(resp.Body).Decode(&v)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if v != nil && len(v) > 0 {
|
||||
if v, ok := v["aggregations"].(map[string]interface{}); ok {
|
||||
b, _ := json.Marshal(v)
|
||||
_ = json.Unmarshal(b, &bind)
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func unmarshalAnalyzeBody(bind interface{}, resp *Response) error {
|
||||
var v map[string]interface{}
|
||||
defer func() { _ = resp.Body.Close() }()
|
||||
if bind == nil { //传入空表示只执行 defer
|
||||
return nil
|
||||
}
|
||||
err := json.NewDecoder(resp.Body).Decode(&v)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if v != nil && len(v) > 0 {
|
||||
b, _ := json.Marshal(v)
|
||||
_ = json.Unmarshal(b, &bind)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func unmarshalBody(bind interface{}, resp *Response) error {
|
||||
var data map[string]interface{}
|
||||
defer func() { _ = resp.Body.Close() }()
|
||||
if bind == nil { //传入空表示只执行 defer
|
||||
return nil
|
||||
}
|
||||
err := json.NewDecoder(resp.Body).Decode(&data)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if data != nil && len(data) > 0 {
|
||||
if hits1, ok := data["hits"].(map[string]interface{}); ok {
|
||||
b, _ := json.Marshal(hits1)
|
||||
_ = json.Unmarshal(b, &bind)
|
||||
} else if _, ok := data["docs"].([]interface{}); ok {
|
||||
b, _ := json.Marshal(data["docs"])
|
||||
_ = json.Unmarshal(b, &bind)
|
||||
} else if _, ok := data["_source"].(map[string]interface{}); ok {
|
||||
b, _ := json.Marshal(data)
|
||||
_ = json.Unmarshal(b, &bind)
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func marshalSearchM(query M) io.Reader {
|
||||
var buf bytes.Buffer
|
||||
if query == nil || len(query) == 0 {
|
||||
return nil
|
||||
}
|
||||
err := json.NewEncoder(&buf).Encode(query)
|
||||
if err != nil {
|
||||
return nil
|
||||
}
|
||||
return bytes.NewReader(buf.Bytes())
|
||||
}
|
||||
|
||||
func marshalBulkM(source M) io.Reader {
|
||||
var buf bytes.Buffer
|
||||
for k, v := range source {
|
||||
var meta = []byte(fmt.Sprintf(`{ "index" : { "_id" : "%s" } }%s`, k, "\n"))
|
||||
var data, _ = json.Marshal(v)
|
||||
data = append(data, "\n"...)
|
||||
buf.Grow(len(meta) + len(data))
|
||||
buf.Write(meta)
|
||||
buf.Write(data)
|
||||
}
|
||||
return bytes.NewReader(buf.Bytes())
|
||||
}
|
||||
|
||||
func marshalStruct(v interface{}) io.Reader {
|
||||
var buf bytes.Buffer
|
||||
if v == nil {
|
||||
return nil
|
||||
}
|
||||
err := json.NewEncoder(&buf).Encode(v)
|
||||
if err != nil {
|
||||
return nil
|
||||
}
|
||||
return bytes.NewReader(buf.Bytes())
|
||||
}
|
||||
|
||||
func unmarshalStruct(bind interface{}, resp *Response) error {
|
||||
var v map[string]interface{}
|
||||
defer func() { _ = resp.Body.Close() }()
|
||||
if bind == nil { //传入空表示只执行 defer
|
||||
return nil
|
||||
}
|
||||
err := json.NewDecoder(resp.Body).Decode(&v)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if v != nil && len(v) > 0 {
|
||||
b, _ := json.Marshal(v)
|
||||
_ = json.Unmarshal(b, &bind)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
@@ -0,0 +1,53 @@
|
||||
package esmanager
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
"net/http"
|
||||
|
||||
"github.com/elastic/go-elasticsearch/v7"
|
||||
)
|
||||
|
||||
var Instance *Manager
|
||||
|
||||
type Manager struct {
|
||||
client *elasticsearch.Client
|
||||
conf *elasticsearch.Config
|
||||
}
|
||||
|
||||
func NewManager(url string, user string, pw string) (*Manager, error) {
|
||||
if url == "" {
|
||||
return nil, errors.New(fmt.Sprintf("Elastic NewManager url is empty!"))
|
||||
}
|
||||
cfg := elasticsearch.Config{
|
||||
Addresses: []string{url},
|
||||
Transport: &http.Transport{
|
||||
MaxIdleConnsPerHost: 10,
|
||||
IdleConnTimeout: 30,
|
||||
},
|
||||
Username: user,
|
||||
Password: pw,
|
||||
}
|
||||
return &Manager{conf: &cfg}, nil
|
||||
}
|
||||
|
||||
func (m *Manager) Client() *elasticsearch.Client {
|
||||
return m.client
|
||||
}
|
||||
|
||||
func (m *Manager) ClientConnect() error {
|
||||
c, err := elasticsearch.NewClient(*m.conf)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
resp, err := c.Ping(c.Ping.WithContext(context.Background()))
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if resp.StatusCode != http.StatusOK {
|
||||
return errors.New(fmt.Sprintf("%+v", resp))
|
||||
}
|
||||
m.client = c
|
||||
return nil
|
||||
}
|
||||
Reference in New Issue
Block a user