@@ -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
1921func (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}
0 commit comments