Files
servicebase/pkg/common/es/es_client_test.go
2025-11-18 17:48:20 +08:00

82 lines
1.9 KiB
Go

package ess
import (
"servicebase/pkg/common/messages"
"context"
"testing"
"time"
"github.com/anxpp/beego/logs"
"github.com/olivere/elastic/v7"
"github.com/spf13/viper"
)
func TestClient(t *testing.T) {
Init()
}
func TestClientCreateUser(t *testing.T) {
//for _, item := range models.LogUserRegisterAll() {
// cli := EsClient{}
// _, success, msg := cli.Create("test_user_register", item.Id, item)
// if !success {
// logs.Error(msg)
// }
//}
}
func TestClientSearch(t *testing.T) {
}
func _testConnect() (c *elastic.Client) {
c, e := elastic.NewSimpleClient(
elastic.SetHealthcheck(true),
elastic.SetHealthcheckInterval(10*time.Second),
// elastic.SetURL("http://47.97.157.234:9200"),
elastic.SetURL(viper.GetString("es.default.addr")),
// elastic.SetBasicAuth(beego.AppConfig.String("es_username"), beego.AppConfig.String("es_password")),
)
if e != nil {
logs.Error("NewClient_error: ", e.Error())
}
return c
}
func TestClientNew(t *testing.T) {
var client EsClient
client.Create("es_index_message", "003", map[string]interface{}{
"id": "003",
"curTime": 1440570500855,
"f1": 1234,
"f2": "22222",
"f3": "22222",
"f4": "22222",
"f5": "22222",
"f6": "2020-08-09 23:37:00",
})
}
func TestSearchMessage(t *testing.T) {
index := messages.TagIndex(string(messages.EventTagMessage))
client := _testConnect()
var list []elastic.Query
list = append(list, elastic.NewWildcardQuery("eventType", "1"))
list = append(list, elastic.NewMatchQuery("msgType", "TEXT"))
s := client.Search().Index(index)
for _, query := range list {
s = s.Query(query)
}
res, e := s.From(0).Size(20).Pretty(true).Do(context.Background())
if e != nil {
return
}
logs.Info(res.Status)
logs.Info(res.Hits.TotalHits)
logs.Info(len(res.Hits.Hits))
for i, item := range res.Hits.Hits {
b, _ := item.Source.MarshalJSON()
logs.Info(i, string(b))
}
time.Sleep(time.Second * 3)
}