Skip to content

Commit 1e62b19

Browse files
committed
optimize: optimize elasticsearch, use go-elasticsearch.
1 parent 2a76e08 commit 1e62b19

File tree

3 files changed

+22
-49
lines changed

3 files changed

+22
-49
lines changed

action/log/logadapter/elasticsearch.go

Lines changed: 22 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -9,15 +9,17 @@ import (
99
"net/http"
1010
"strings"
1111

12-
elasticsearch "github.com/elastic/go-elasticsearch/v8"
12+
"github.com/elastic/go-elasticsearch/v8"
1313
"github.com/elastic/go-elasticsearch/v8/esapi"
14-
elastic "github.com/olivere/elastic/v7"
14+
"github.com/elastic/go-elasticsearch/v8/typedapi/core/search"
15+
"github.com/elastic/go-elasticsearch/v8/typedapi/types"
16+
1517
"github.com/seata/seata-ctl/tool"
1618
)
1719

1820
// QueryLogs is a function that queries specific documents
1921
func (e *Elasticsearch) QueryLogs(filter map[string]interface{}, currency *Currency, number int) error {
20-
client, err := createElasticClient(currency)
22+
client, err := createEsDefaultClient(currency)
2123
if err != nil {
2224
return fmt.Errorf("failed to create elasticsearch client: %w", err)
2325
}
@@ -33,47 +35,21 @@ func (e *Elasticsearch) QueryLogs(filter map[string]interface{}, currency *Curre
3335
return err
3436
}
3537

36-
// Execute the search query
37-
searchResult, err := client.Search().
38-
Index(indexName).
39-
Size(number).
40-
Query(query).
41-
Do(context.Background())
38+
res, err := client.Search().Index(indexName).Size(number).Query(query).Do(context.Background())
39+
4240
if err != nil {
4341
return fmt.Errorf("error fetching documents: %w", err)
4442
}
4543

46-
err = processSearchHits(searchResult, currency)
44+
err = processSearchHits(res, currency)
4745
if err != nil {
4846
return err
4947
}
5048
return nil
5149
}
5250

53-
// createElasticClient configures and creates a new Elasticsearch client
54-
func createElasticClient(currency *Currency) (*elastic.Client, error) {
55-
httpClient := &http.Client{
56-
Transport: &http.Transport{
57-
TLSClientConfig: &tls.Config{
58-
InsecureSkipVerify: true,
59-
},
60-
},
61-
}
62-
63-
client, err := elastic.NewClient(
64-
elastic.SetURL(currency.Address),
65-
elastic.SetHttpClient(httpClient),
66-
elastic.SetSniff(false),
67-
elastic.SetBasicAuth(currency.Username, currency.Password),
68-
)
69-
if err != nil {
70-
return nil, err
71-
}
72-
return client, nil
73-
}
74-
7551
// createEsDefaultClient configures and creates a new Elasticsearch client
76-
func createEsDefaultClient(currency *Currency) (*elasticsearch.Client, error) {
52+
func createEsDefaultClient(currency *Currency) (*elasticsearch.TypedClient, error) {
7753
// Configure the Elasticsearch client
7854
cfg := elasticsearch.Config{
7955
Addresses: []string{
@@ -88,22 +64,22 @@ func createEsDefaultClient(currency *Currency) (*elasticsearch.Client, error) {
8864
}
8965

9066
// Create the client instance
91-
es, err := elasticsearch.NewClient(cfg)
67+
es, err := elasticsearch.NewTypedClient(cfg)
9268
if err != nil {
9369
return nil, fmt.Errorf("error creating the client: %s", err)
9470
}
9571
return es, nil
9672
}
9773

9874
// processSearchHits handles and formats the search results
99-
func processSearchHits(searchResult *elastic.SearchResult, currency *Currency) error {
100-
if len(searchResult.Hits.Hits) == 0 {
75+
func processSearchHits(res *search.Response, currency *Currency) error {
76+
if len(res.Hits.Hits) == 0 {
10177
return fmt.Errorf("no documents found")
10278
}
10379

104-
for _, hit := range searchResult.Hits.Hits {
80+
for _, hit := range res.Hits.Hits {
10581
var doc map[string]interface{}
106-
if err := json.Unmarshal(hit.Source, &doc); err != nil {
82+
if err := json.Unmarshal(hit.Source_, &doc); err != nil {
10783
return fmt.Errorf("failed to unmarshal document: %w", err)
10884
}
10985

@@ -257,21 +233,25 @@ func removeKeywordSuffix(input []string) []string {
257233
return result
258234
}
259235

260-
// buildQuery constructs a BoolQuery based on the provided filter and index fields
261-
func buildQuery(filter map[string]interface{}, indexFields []string) (*elastic.BoolQuery, error) {
262-
query := elastic.NewBoolQuery()
236+
// buildQuery constructs a types Query based on the provided filter and index fields
237+
func buildQuery(filter map[string]interface{}, indexFields []string) (*types.Query, error) {
238+
query := &types.Query{}
263239
if filter["query"].(string) != "{}" {
264240
indexMap, err := ParseJobString(filter["query"].(string))
265241
if err != nil {
266242
return query, err
267243
}
244+
var termQuery []types.Query
268245
for k, v := range indexMap {
269246
if Contains(indexFields, k) {
270-
query.Should(elastic.NewTermQuery(k, v))
247+
termQuery = append(termQuery, types.Query{Term: map[string]types.TermQuery{k: {Value: v}}})
271248
} else {
272249
return query, fmt.Errorf("invalid index key: %s", k)
273250
}
274251
}
252+
query.Bool = &types.BoolQuery{
253+
Should: termQuery,
254+
}
275255
}
276256
return query, nil
277257
}

go.mod

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ require (
66
github.com/elastic/go-elasticsearch/v8 v8.15.0
77
github.com/guptarohit/asciigraph v0.7.3
88
github.com/jedib0t/go-pretty/v6 v6.4.7
9-
github.com/olivere/elastic/v7 v7.0.32
109
github.com/sirupsen/logrus v1.9.3
1110
github.com/spf13/cobra v1.8.1
1211
github.com/spf13/viper v1.16.0
@@ -48,7 +47,6 @@ require (
4847
github.com/modern-go/reflect2 v1.0.2 // indirect
4948
github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822 // indirect
5049
github.com/pelletier/go-toml/v2 v2.0.8 // indirect
51-
github.com/pkg/errors v0.9.1 // indirect
5250
github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2 // indirect
5351
github.com/rivo/uniseg v0.2.0 // indirect
5452
github.com/spf13/afero v1.9.5 // indirect

go.sum

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -64,8 +64,6 @@ github.com/envoyproxy/go-control-plane v0.9.4/go.mod h1:6rpuAdCZL397s3pYoYcLgu1m
6464
github.com/envoyproxy/go-control-plane v0.9.7/go.mod h1:cwu0lG7PUMfa9snN8LXBig5ynNVH9qI8YYLbd1fK2po=
6565
github.com/envoyproxy/go-control-plane v0.9.9-0.20201210154907-fd9021fe5dad/go.mod h1:cXg6YxExXjJnVBQHBLXeUAgxn2UodCpnH306RInaBQk=
6666
github.com/envoyproxy/protoc-gen-validate v0.1.0/go.mod h1:iSmxcyjqTsJpI2R4NaDN7+kN2VEUnK/pcBlmesArF7c=
67-
github.com/fortytw2/leaktest v1.3.0 h1:u8491cBMTQ8ft8aeV+adlcytMZylmA5nnwwkRZjI8vw=
68-
github.com/fortytw2/leaktest v1.3.0/go.mod h1:jDsjWgpAGjm2CA7WthBh/CdZYEPF31XHquHwclZch5g=
6967
github.com/frankban/quicktest v1.14.4 h1:g2rn0vABPOOXmZUj+vbmUp0lPoXEMuhTpIluN0XL9UY=
7068
github.com/frankban/quicktest v1.14.4/go.mod h1:4ptaffx2x8+WTWXmUCuVU6aPUX1/Mz7zb5vbUoiM6w0=
7169
github.com/fsnotify/fsnotify v1.7.0 h1:8JEhPFa5W2WU7YfeZzPNqzMP6Lwt7L2715Ggo0nosvA=
@@ -205,15 +203,12 @@ github.com/modern-go/reflect2 v1.0.2 h1:xBagoLtFs94CBntxluKeaWgTMpvLxC4ur3nMaC9G
205203
github.com/modern-go/reflect2 v1.0.2/go.mod h1:yWuevngMOJpCy52FWWMvUC8ws7m/LJsjYzDa0/r8luk=
206204
github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822 h1:C3w9PqII01/Oq1c1nUAm88MOHcQC9l5mIlSMApZMrHA=
207205
github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822/go.mod h1:+n7T8mK8HuQTcFwEeznm/DIxMOiR9yIdICNftLE1DvQ=
208-
github.com/olivere/elastic/v7 v7.0.32 h1:R7CXvbu8Eq+WlsLgxmKVKPox0oOwAE/2T9Si5BnvK6E=
209-
github.com/olivere/elastic/v7 v7.0.32/go.mod h1:c7PVmLe3Fxq77PIfY/bZmxY/TAamBhCzZ8xDOE09a9k=
210206
github.com/onsi/ginkgo/v2 v2.19.0 h1:9Cnnf7UHo57Hy3k6/m5k3dRfGTMXGvxhHFvkDTCTpvA=
211207
github.com/onsi/ginkgo/v2 v2.19.0/go.mod h1:rlwLi9PilAFJ8jCg9UE1QP6VBpd6/xj3SRC0d6TU0To=
212208
github.com/onsi/gomega v1.19.0 h1:4ieX6qQjPP/BfC3mpsAtIGGlxTWPeA3Inl/7DtXw1tw=
213209
github.com/onsi/gomega v1.19.0/go.mod h1:LY+I3pBVzYsTBU1AnDwOSxaYi9WoWiqgwooUqq9yPro=
214210
github.com/pelletier/go-toml/v2 v2.0.8 h1:0ctb6s9mE31h0/lhu+J6OPmVeDxJn+kYnJc2jZR9tGQ=
215211
github.com/pelletier/go-toml/v2 v2.0.8/go.mod h1:vuYfssBdrU2XDZ9bYydBu6t+6a6PYNcZljzZR9VXg+4=
216-
github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4=
217212
github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
218213
github.com/pkg/profile v1.6.0/go.mod h1:qBsxPvzyUincmltOk6iyRVxHYg4adc0OFOv72ZdLa18=
219214
github.com/pkg/sftp v1.13.1/go.mod h1:3HaPG6Dq1ILlpPZRO0HVMrsydcdLt6HRDccSgb87qRg=

0 commit comments

Comments
 (0)