Commit 9c560be0 by yemin

no message

parents
go get -u git.168cad.top/go/logger/log
go get -u github.com/olivere/elastic/v7
\ No newline at end of file
package esclient
import (
"context"
"fmt"
"git.168cad.top/go/logger/log"
"github.com/olivere/elastic/v7"
_log "log"
"os"
)
func TestLog() {
log.Info("Es 测试")
}
var client *elastic.Client
func Init() {
host := fmt.Sprintf("http://%s:%d", Conf.ServerHost, Conf.ServerPort)
var err error
errorLog := _log.New(os.Stdout, "APP", _log.LstdFlags)
client, err = elastic.NewClient(elastic.SetSniff(Conf.Sniff), elastic.SetErrorLog(errorLog), elastic.SetURL(host))
if err != nil {
log.Error(fmt.Sprintf("Elasticsearch connect error:%s", err.Error()))
} else {
info, code, err := client.Ping(host).Do(context.Background())
if err != nil {
panic(err)
}
log.Info(fmt.Sprintf("Elasticsearch connect %d", code))
log.Info(fmt.Sprintf("Elasticsearch version %s", info.Version.Number))
}
}
func getRedisClient() *elastic.Client {
return client
}
package esclient
//ES conf
type EsConf struct {
ServerHost string
ServerPort int
Sniff bool
LogIndexNamePrefix string
IndexNamePrefix string
}
var Conf = EsConf{}
\ No newline at end of file
package esclient
import (
"context"
"encoding/json"
"fmt"
"git.168cad.top/go/logger/log"
"github.com/olivere/elastic/v7"
"sort"
"time"
)
func getCurrentIndex() string {
if Conf.LogIndexNamePrefix == "" {
return Conf.IndexNamePrefix + time.Now().Format("20060102")
} else {
return Conf.LogIndexNamePrefix + time.Now().Format("20060102")
}
}
type ServerLogModel struct {
ServerName string `json:"serverName"`
Env string `json:"env"`
TimeStamp int64 `json:"timeStamp"`
ThreadNo string `json:"threadNo"`
ServerIp string `json:"serverIp"`
ClientIp string `json:"clientIp"`
Token string `json:"token"`
CustomerId uint32 `json:"customerId"`
ClientSource string `json:"clientSource"`
Url string `json:"url"`
Method string `json:"method"`
Request string `json:"request"`
Response string `json:"response"`
DealTimes int64 `json:"dealTimes"`
}
func InsertEsLog(logInfo interface{}) {
indexName := getCurrentIndex()
put, err := getRedisClient().Index().
Index(indexName).
BodyJson(logInfo).
Do(context.Background())
if err != nil {
log.Error(fmt.Sprintf("insert es log fail:%s", err.Error()))
} else {
log.Debug(fmt.Sprintf("insert es log success:%s", put.Id))
}
}
func GroupField(indexName, field string, size, minCount int, excludeValues []string) ([]byte,error) {
if size == 0 {
size = 30
}
aggregationQuery := elastic.NewTermsAggregation().Field(field).MinDocCount(minCount).Size(size)
for _, excludeValue := range excludeValues {
aggregationQuery.ExcludeValues(excludeValue)
}
if log.IsDebug() {
source, _ := aggregationQuery.Source()
queryByte, _ := json.Marshal(source)
log.Info(fmt.Sprintf("查询条件:%s", string(queryByte)))
}
res, err := getRedisClient().Search(indexName).
Aggregation("group_field", aggregationQuery).
From(0).
Size(0).
Do(context.Background())
if err != nil {
return nil,err
}
return json.Marshal(res.Aggregations)
}
func QueryLogs(indexName, filed, value string, from, size int) (interface{},error) {
if from <= 0 {
from = 0
}
if size <= 0 {
size = 10
}
query := elastic.NewBoolQuery().Must(elastic.NewTermQuery(filed, value))
return getRedisClient().Search(indexName).Query(query).
From(from).
Size(size).
Do(context.Background())
}
type IndexInfo struct {
Name string `json:"name"`
Count int `json:"count"`
Size string `json:"size"`
}
func AllIndex() (interface{},error) {
res, err := getRedisClient().CatIndices().Do(context.Background())
if err != nil {
return nil,err
}
var keys []string
var sizeMap = make(map[string]IndexInfo)
for _, row := range res {
keys = append(keys, row.Index)
sizeMap[row.Index] = IndexInfo{row.Index, row.DocsCount, row.StoreSize}
}
sort.Strings(keys)
data := make([]IndexInfo, len(keys))
for i, key := range keys {
data[i] = sizeMap[key]
}
return data, nil
}
func DeleteIndex(indexName string) (interface{},error) {
return getRedisClient().DeleteIndex(indexName).Do(context.Background())
}
package esclient
import (
"context"
"fmt"
"git.168cad.top/go/logger/log"
"github.com/olivere/elastic/v7"
_log "log"
"os"
"testing"
)
func init() {
Conf = EsConf{
//ServerHost: "to-chengdu-office.168cad.top",
//ServerPort: 50037,
ServerHost: "192.168.1.124",
ServerPort: 9200,
Sniff: true,
}
}
func TestName(t *testing.T) {
host := fmt.Sprintf("http://%s:%d", Conf.ServerHost, Conf.ServerPort)
var err error
errorLog := _log.New(os.Stdout, "APP", _log.LstdFlags)
client, err := elastic.NewClient(elastic.SetSniff(Conf.Sniff), elastic.SetErrorLog(errorLog), elastic.SetURL(host))
if err != nil {
log.Error(err.Error())
}
info, code, err := client.Ping(host).Do(context.Background())
if err != nil {
panic(err)
}
log.Info(fmt.Sprintf("Elasticsearch connect %d", code))
log.Info(fmt.Sprintf("Elasticsearch version %s", info.Version.Number))
}
module git.168cad.top/go/ElasticClientSDK
go 1.17
require (
git.168cad.top/go/logger v1.0.4 // indirect
github.com/josharian/intern v1.0.0 // indirect
github.com/mailru/easyjson v0.7.7 // indirect
github.com/olivere/elastic v6.2.37+incompatible // indirect
github.com/olivere/elastic/v7 v7.0.32 // indirect
github.com/pkg/errors v0.9.1 // indirect
)
git.168cad.top/go/logger v1.0.4 h1:N+Nej3W+MbSPQSkpqn+RrkoFWzrHDEjxrXFBM3lZBz4=
git.168cad.top/go/logger v1.0.4/go.mod h1:HlxzmTuqONGC5xGiuBVQ1qAEoby29AaWYMDhBBSi8c4=
github.com/josharian/intern v1.0.0 h1:vlS4z54oSdjm0bgjRigI+G1HpF+tI+9rE5LLzOg8HmY=
github.com/josharian/intern v1.0.0/go.mod h1:5DoeVV0s6jJacbCEi61lwdGj/aVlrQvzHFFd8Hwg//Y=
github.com/mailru/easyjson v0.7.7 h1:UGYAvKxe3sBsEDzO8ZeWOSlIQfWFlxbzLZe7hwFURr0=
github.com/mailru/easyjson v0.7.7/go.mod h1:xzfreul335JAWq5oZzymOObrkdz5UnU4kGfJJLY9Nlc=
github.com/olivere/elastic v6.2.37+incompatible h1:UfSGJem5czY+x/LqxgeCBgjDn6St+z8OnsCuxwD3L0U=
github.com/olivere/elastic v6.2.37+incompatible/go.mod h1:J+q1zQJTgAz9woqsbVRqGeB5G1iqDKVBWLNSYW8yfJ8=
github.com/olivere/elastic/v7 v7.0.32 h1:R7CXvbu8Eq+WlsLgxmKVKPox0oOwAE/2T9Si5BnvK6E=
github.com/olivere/elastic/v7 v7.0.32/go.mod h1:c7PVmLe3Fxq77PIfY/bZmxY/TAamBhCzZ8xDOE09a9k=
github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4=
github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
package main
func main() {
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment