From 5761bdcd5d364e534bbf1941d19d58a3308afcaa Mon Sep 17 00:00:00 2001 From: Roman Date: Thu, 22 Sep 2022 15:04:03 +0300 Subject: [PATCH 1/2] Support Elasticsearch v8 --- README.md | 6 +++--- count.go | 4 ++-- custom.go | 4 ++-- delete.go | 4 ++-- es.go | 4 ++-- go.mod | 4 ++-- go.sum | 6 ++++-- search.go | 8 ++------ 8 files changed, 19 insertions(+), 21 deletions(-) diff --git a/README.md b/README.md index 7e0eea7..c172dbd 100644 --- a/README.md +++ b/README.md @@ -35,7 +35,7 @@ This is an early release, API may still change. `esquery` is a Go module. To install, simply run this in your project's root directory: ```bash -go get github.com/aquasecurity/esquery +go get github.com/aquasecurity/esquery/v8 ``` ## Usage @@ -51,8 +51,8 @@ import ( "context" "log" - "github.com/aquasecurity/esquery" - "github.com/elastic/go-elasticsearch/v7" + "github.com/aquasecurity/esquery/v8" + "github.com/elastic/go-elasticsearch/v8" ) func main() { diff --git a/count.go b/count.go index 2e01b5c..43f70ab 100644 --- a/count.go +++ b/count.go @@ -4,8 +4,8 @@ import ( "bytes" "encoding/json" - "github.com/elastic/go-elasticsearch/v7" - "github.com/elastic/go-elasticsearch/v7/esapi" + "github.com/elastic/go-elasticsearch/v8" + "github.com/elastic/go-elasticsearch/v8/esapi" ) // CountRequest represents a request to get the number of matches for a search diff --git a/custom.go b/custom.go index 17186cf..35d0fad 100644 --- a/custom.go +++ b/custom.go @@ -1,8 +1,8 @@ package esquery import ( - "github.com/elastic/go-elasticsearch/v7" - "github.com/elastic/go-elasticsearch/v7/esapi" + "github.com/elastic/go-elasticsearch/v8" + "github.com/elastic/go-elasticsearch/v8/esapi" ) // CustomQueryMap represents an arbitrary query map for custom queries. diff --git a/delete.go b/delete.go index 4e5163d..dc50bf8 100644 --- a/delete.go +++ b/delete.go @@ -4,8 +4,8 @@ import ( "bytes" "encoding/json" - "github.com/elastic/go-elasticsearch/v7" - "github.com/elastic/go-elasticsearch/v7/esapi" + "github.com/elastic/go-elasticsearch/v8" + "github.com/elastic/go-elasticsearch/v8/esapi" ) // DeleteRequest represents a request to ElasticSearch's Delete By Query API, diff --git a/es.go b/es.go index 4149a01..e896e60 100644 --- a/es.go +++ b/es.go @@ -32,8 +32,8 @@ // "context" // "log" // -// "github.com/aquasecurity/esquery" -// "github.com/elastic/go-elasticsearch/v7" +// "github.com/aquasecurity/esquery/v8" +// "github.com/elastic/go-elasticsearch/v8" // ) // // func main() { diff --git a/go.mod b/go.mod index d17acbf..f066982 100644 --- a/go.mod +++ b/go.mod @@ -1,9 +1,9 @@ -module github.com/aquasecurity/esquery +module github.com/aquasecurity/esquery/v8 go 1.13 require ( - github.com/elastic/go-elasticsearch/v7 v7.6.0 + github.com/elastic/go-elasticsearch/v8 v8.4.0 github.com/fatih/structs v1.1.0 github.com/jgroeneveld/schema v1.0.0 // indirect github.com/jgroeneveld/trial v2.0.0+incompatible diff --git a/go.sum b/go.sum index fae67ff..cd26fbd 100644 --- a/go.sum +++ b/go.sum @@ -1,5 +1,7 @@ -github.com/elastic/go-elasticsearch/v7 v7.6.0 h1:sYpGLpEFHgLUKLsZUBfuaVI9QgHjS3JdH9fX4/z8QI8= -github.com/elastic/go-elasticsearch/v7 v7.6.0/go.mod h1:OJ4wdbtDNk5g503kvlHLyErCgQwwzmDtaFC4XyOxXA4= +github.com/elastic/elastic-transport-go/v8 v8.1.0 h1:NeqEz1ty4RQz+TVbUrpSU7pZ48XkzGWQj02k5koahIE= +github.com/elastic/elastic-transport-go/v8 v8.1.0/go.mod h1:87Tcz8IVNe6rVSLdBux1o/PEItLtyabHU3naC7IoqKI= +github.com/elastic/go-elasticsearch/v8 v8.4.0 h1:Rn1mcqaIMcNT43hnx2H62cIFZ+B6mjWtzj85BDKrvCE= +github.com/elastic/go-elasticsearch/v8 v8.4.0/go.mod h1:yY52i2Vj0unLz+N3Nwx1gM5LXwoj3h2dgptNGBYkMLA= github.com/fatih/structs v1.1.0 h1:Q7juDM0QtcnhCpeyLGQKyg4TOIghuNXrkL32pHAUMxo= github.com/fatih/structs v1.1.0/go.mod h1:9NiDSp5zOcgEDl+j00MP/WkGVPOlPRLejGD8Ga6PJ7M= github.com/jgroeneveld/schema v1.0.0 h1:J0E10CrOkiSEsw6dfb1IfrDJD14pf6QLVJ3tRPl/syI= diff --git a/search.go b/search.go index 5c661a2..adad2f9 100644 --- a/search.go +++ b/search.go @@ -6,8 +6,8 @@ import ( "fmt" "time" - "github.com/elastic/go-elasticsearch/v7" - "github.com/elastic/go-elasticsearch/v7/esapi" + "github.com/elastic/go-elasticsearch/v8" + "github.com/elastic/go-elasticsearch/v8/esapi" ) // SearchRequest represents a request to ElasticSearch's Search API, described @@ -26,7 +26,6 @@ type SearchRequest struct { sort Sort source Source timeout *time.Duration - } // Search creates a new SearchRequest object, to be filled via method chaining. @@ -113,8 +112,6 @@ func (req *SearchRequest) Highlight(highlight Mappable) *SearchRequest { return req } - - // Map implements the Mappable interface. It converts the request to into a // nested map[string]interface{}, as expected by the go-elasticsearch library. func (req *SearchRequest) Map() map[string]interface{} { @@ -155,7 +152,6 @@ func (req *SearchRequest) Map() map[string]interface{} { m["search_after"] = req.searchAfter } - source := req.source.Map() if len(source) > 0 { m["_source"] = source -- 2.40.1 From 90226c236a410b6bea3658205ed0228f0b173485 Mon Sep 17 00:00:00 2001 From: "maxim.yanchuk" Date: Thu, 2 Nov 2023 00:30:33 +0300 Subject: [PATCH 2/2] FIX import & go.sum & simple issues --- aggs_filter.go | 2 +- aggs_metric.go | 35 ++++++++++++----------------------- aggs_nested.go | 2 +- custom.go | 2 +- go.sum | 8 ++++---- highlight.go | 8 +++++--- query_boolean.go | 2 +- query_combined_fields.go | 17 ++++++++--------- query_match.go | 8 ++++---- query_multi_match.go | 18 +++++++++--------- query_term_level.go | 8 ++++---- search.go | 1 + 12 files changed, 51 insertions(+), 60 deletions(-) diff --git a/aggs_filter.go b/aggs_filter.go index 66770e2..1c513e2 100644 --- a/aggs_filter.go +++ b/aggs_filter.go @@ -6,7 +6,7 @@ type FilterAggregation struct { aggs []Aggregation } -// Filter creates a new aggregation of type "filter". The method name includes +// FilterAgg creates a new aggregation of type "filter". The method name includes // the "Agg" suffix to prevent conflict with the "filter" query. func FilterAgg(name string, filter Mappable) *FilterAggregation { return &FilterAggregation{ diff --git a/aggs_metric.go b/aggs_metric.go index dc4adb6..d375a3f 100644 --- a/aggs_metric.go +++ b/aggs_metric.go @@ -44,8 +44,7 @@ func (agg *BaseAgg) Map() map[string]interface{} { } // AvgAgg represents an aggregation of type "avg", as described in -// https://www.elastic.co/guide/en/elasticsearch/reference/ -// current/search-aggregations-metrics-avg-aggregation.html +// https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-metrics-avg-aggregation.html type AvgAgg struct { *BaseAgg `structs:",flatten"` } @@ -68,8 +67,7 @@ func (agg *AvgAgg) Missing(val interface{}) *AvgAgg { //----------------------------------------------------------------------------// // WeightedAvgAgg represents an aggregation of type "weighted_avg", as described -// in https://www.elastic.co/guide/en/elasticsearch/reference/ -// current/search-aggregations-metrics-weight-avg-aggregation.html +// in https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-metrics-weight-avg-aggregation.html type WeightedAvgAgg struct { name string apiName string @@ -106,7 +104,7 @@ func (agg *WeightedAvgAgg) Value(field string, missing ...interface{}) *Weighted return agg } -// Value sets the weight field and optionally a value to use when records are +// Weight sets the weight field and optionally a value to use when records are // missing a value for the field. func (agg *WeightedAvgAgg) Weight(field string, missing ...interface{}) *WeightedAvgAgg { agg.Weig = new(BaseAggParams) @@ -128,8 +126,7 @@ func (agg *WeightedAvgAgg) Map() map[string]interface{} { //----------------------------------------------------------------------------// // CardinalityAgg represents an aggregation of type "cardinality", as described -// in https://www.elastic.co/guide/en/elasticsearch/reference/ -// current/search-aggregations-metrics-cardinality-aggregation.html +// in https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-metrics-cardinality-aggregation.html type CardinalityAgg struct { *BaseAgg `structs:",flatten"` @@ -169,8 +166,7 @@ func (agg *CardinalityAgg) Map() map[string]interface{} { //----------------------------------------------------------------------------// // MaxAgg represents an aggregation of type "max", as described in: -// https://www.elastic.co/guide/en/elasticsearch/reference/ -// current/search-aggregations-metrics-max-aggregation.html +// https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-metrics-max-aggregation.html type MaxAgg struct { *BaseAgg `structs:",flatten"` } @@ -193,8 +189,7 @@ func (agg *MaxAgg) Missing(val interface{}) *MaxAgg { //----------------------------------------------------------------------------// // MinAgg represents an aggregation of type "min", as described in: -// https://www.elastic.co/guide/en/elasticsearch/reference/ -// current/search-aggregations-metrics-min-aggregation.html +// https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-metrics-min-aggregation.html type MinAgg struct { *BaseAgg `structs:",flatten"` } @@ -217,8 +212,7 @@ func (agg *MinAgg) Missing(val interface{}) *MinAgg { //----------------------------------------------------------------------------// // SumAgg represents an aggregation of type "sum", as described in: -// https://www.elastic.co/guide/en/elasticsearch/reference/ -// current/search-aggregations-metrics-sum-aggregation.html +// https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-metrics-sum-aggregation.html type SumAgg struct { *BaseAgg `structs:",flatten"` } @@ -241,8 +235,7 @@ func (agg *SumAgg) Missing(val interface{}) *SumAgg { //----------------------------------------------------------------------------// // ValueCountAgg represents an aggregation of type "value_count", as described -// in https://www.elastic.co/guide/en/elasticsearch/reference/ -// current/search-aggregations-metrics-valuecount-aggregation.html +// in https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-metrics-valuecount-aggregation.html type ValueCountAgg struct { *BaseAgg `structs:",flatten"` } @@ -258,8 +251,7 @@ func ValueCount(name, field string) *ValueCountAgg { //----------------------------------------------------------------------------// // PercentilesAgg represents an aggregation of type "percentiles", as described -// in https://www.elastic.co/guide/en/elasticsearch/reference/ -// current/search-aggregations-metrics-percentile-aggregation.html +// in https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-metrics-percentile-aggregation.html type PercentilesAgg struct { *BaseAgg `structs:",flatten"` @@ -334,8 +326,7 @@ func (agg *PercentilesAgg) Map() map[string]interface{} { //----------------------------------------------------------------------------// // StatsAgg represents an aggregation of type "stats", as described in: -// https://www.elastic.co/guide/en/elasticsearch/reference/ -// current/search-aggregations-metrics-stats-aggregation.html +// https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-metrics-stats-aggregation.html type StatsAgg struct { *BaseAgg `structs:",flatten"` } @@ -357,8 +348,7 @@ func (agg *StatsAgg) Missing(val interface{}) *StatsAgg { // ---------------------------------------------------------------------------// // StringStatsAgg represents an aggregation of type "string_stats", as described -// in https://www.elastic.co/guide/en/elasticsearch/reference/ -// current/search-aggregations-metrics-string-stats-aggregation.html +// in https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-metrics-string-stats-aggregation.html type StringStatsAgg struct { *BaseAgg `structs:",flatten"` @@ -399,8 +389,7 @@ func (agg *StringStatsAgg) Map() map[string]interface{} { // ---------------------------------------------------------------------------// // TopHitsAgg represents an aggregation of type "top_hits", as described -// in https://www.elastic.co/guide/en/elasticsearch/reference/ -// current/search-aggregations-metrics-top-hits-aggregation.html +// in https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-metrics-top-hits-aggregation.html type TopHitsAgg struct { name string from uint64 diff --git a/aggs_nested.go b/aggs_nested.go index 9b262a9..9cdb67a 100644 --- a/aggs_nested.go +++ b/aggs_nested.go @@ -20,7 +20,7 @@ func (agg *NestedAggregation) Name() string { return agg.name } -// NumberOfFragments sets the aggregations path +// Path sets the aggregations path func (agg *NestedAggregation) Path(p string) *NestedAggregation { agg.path = p return agg diff --git a/custom.go b/custom.go index 35d0fad..1bd96c3 100644 --- a/custom.go +++ b/custom.go @@ -21,7 +21,7 @@ func CustomQuery(m map[string]interface{}) *CustomQueryMap { // Map returns the custom query as a map[string]interface{}, thus implementing // the Mappable interface. func (m *CustomQueryMap) Map() map[string]interface{} { - return map[string]interface{}(*m) + return *m } // Run executes the custom query using the provided ElasticSearch client. Zero diff --git a/go.sum b/go.sum index cd26fbd..f34ef7c 100644 --- a/go.sum +++ b/go.sum @@ -1,7 +1,7 @@ -github.com/elastic/elastic-transport-go/v8 v8.1.0 h1:NeqEz1ty4RQz+TVbUrpSU7pZ48XkzGWQj02k5koahIE= -github.com/elastic/elastic-transport-go/v8 v8.1.0/go.mod h1:87Tcz8IVNe6rVSLdBux1o/PEItLtyabHU3naC7IoqKI= -github.com/elastic/go-elasticsearch/v8 v8.4.0 h1:Rn1mcqaIMcNT43hnx2H62cIFZ+B6mjWtzj85BDKrvCE= -github.com/elastic/go-elasticsearch/v8 v8.4.0/go.mod h1:yY52i2Vj0unLz+N3Nwx1gM5LXwoj3h2dgptNGBYkMLA= +github.com/elastic/elastic-transport-go/v8 v8.3.0 h1:DJGxovyQLXGr62e9nDMPSxRyWION0Bh6d9eCFBriiHo= +github.com/elastic/elastic-transport-go/v8 v8.3.0/go.mod h1:87Tcz8IVNe6rVSLdBux1o/PEItLtyabHU3naC7IoqKI= +github.com/elastic/go-elasticsearch/v8 v8.10.1 h1:JJ3i2DimYTsJcUoEGbg6tNB0eehTNdid9c5kTR1TGuI= +github.com/elastic/go-elasticsearch/v8 v8.10.1/go.mod h1:GU1BJHO7WeamP7UhuElYwzzHtvf9SDmeVpSSy9+o6Qg= github.com/fatih/structs v1.1.0 h1:Q7juDM0QtcnhCpeyLGQKyg4TOIghuNXrkL32pHAUMxo= github.com/fatih/structs v1.1.0/go.mod h1:9NiDSp5zOcgEDl+j00MP/WkGVPOlPRLejGD8Ga6PJ7M= github.com/jgroeneveld/schema v1.0.0 h1:J0E10CrOkiSEsw6dfb1IfrDJD14pf6QLVJ3tRPl/syI= diff --git a/highlight.go b/highlight.go index 855f114..70b1410 100644 --- a/highlight.go +++ b/highlight.go @@ -252,8 +252,9 @@ func (a HighlightBoundaryScanner) String() string { return "sentence" case BoundaryScannerWord: return "word" + default: + return "" } - return "" } type HighlightEncoder uint8 @@ -281,7 +282,7 @@ func (a HighlightEncoder) String() string { type HighlightFragmenter uint8 const ( - // FragmentSpan is the "span" value + // FragmenterSpan is the "span" value FragmenterSpan HighlightFragmenter = iota // FragmenterSimple is the "simple" value @@ -336,6 +337,7 @@ func (a HighlightTagsSchema) String() string { switch a { case TagsSchemaStyled: return "styled" + default: + return "" } - return "" } diff --git a/query_boolean.go b/query_boolean.go index 18acb92..977ad4b 100644 --- a/query_boolean.go +++ b/query_boolean.go @@ -32,7 +32,7 @@ func (q *BoolQuery) Filter(filter ...Mappable) *BoolQuery { return q } -// Must adds one or more queries of type "must_not" to the bool query. MustNot +// MustNot adds one or more queries of type "must_not" to the bool query. MustNot // can be called multiple times, queries will be appended to existing ones. func (q *BoolQuery) MustNot(mustnot ...Mappable) *BoolQuery { q.mustNot = append(q.mustNot, mustnot...) diff --git a/query_combined_fields.go b/query_combined_fields.go index 87b4e2d..0c43170 100644 --- a/query_combined_fields.go +++ b/query_combined_fields.go @@ -15,13 +15,13 @@ func (q *CombinedFieldsQuery) Map() map[string]interface{} { } type combinedFieldsParams struct { - Qry interface{} `structs:"query"` - Fields []string `structs:"fields"` - Boost float32 `structs:"boost,omitempty"` - AutoGenerate *bool `structs:"auto_generate_synonyms_phrase_query,omitempty"` - Op MatchOperator `structs:"operator,string,omitempty"` - MinMatch string `structs:"minimum_should_match,omitempty"` - ZeroTerms ZeroTerms `structs:"zero_terms_query,string,omitempty"` + Qry interface{} `structs:"query"` + Fields []string `structs:"fields"` + Boost float32 `structs:"boost,omitempty"` + AutoGenerate *bool `structs:"auto_generate_synonyms_phrase_query,omitempty"` + Op MatchOperator `structs:"operator,string,omitempty"` + MinMatch string `structs:"minimum_should_match,omitempty"` + ZeroTerms ZeroTerms `structs:"zero_terms_query,string,omitempty"` } // CombinedFields creates a new query of type "combined_fields" @@ -57,7 +57,7 @@ func (q *CombinedFieldsQuery) AutoGenerateSynonymsPhraseQuery(b bool) *CombinedF return q } -// Boost +// Boost sets the query boost func (q *CombinedFieldsQuery) Boost(l float32) *CombinedFieldsQuery { q.params.Boost = l return q @@ -83,4 +83,3 @@ func (q *CombinedFieldsQuery) ZeroTermsQuery(s ZeroTerms) *CombinedFieldsQuery { q.params.ZeroTerms = s return q } - diff --git a/query_match.go b/query_match.go index b0a9608..35846fd 100644 --- a/query_match.go +++ b/query_match.go @@ -10,7 +10,7 @@ const ( // TypeMatch denotes a query of type "match" TypeMatch matchType = iota - // TypeMatchBool denotes a query of type "match_bool_prefix" + // TypeMatchBoolPrefix denotes a query of type "match_bool_prefix" TypeMatchBoolPrefix // TypeMatchPhrase denotes a query of type "match_phrase" @@ -204,9 +204,9 @@ func (q *MatchQuery) ZeroTermsQuery(s ZeroTerms) *MatchQuery { } // Boost sets the boost value of the query. -func (a *MatchQuery) Boost(b float32) *MatchQuery { - a.params.Boost = b - return a +func (q *MatchQuery) Boost(b float32) *MatchQuery { + q.params.Boost = b + return q } // MatchOperator is an enumeration type representing supported values for a diff --git a/query_multi_match.go b/query_multi_match.go index 3dc6caf..c373efe 100644 --- a/query_multi_match.go +++ b/query_multi_match.go @@ -100,13 +100,13 @@ func (q *MultiMatchQuery) PrefixLength(l uint16) *MultiMatchQuery { return q } -// TieBreaker +// TieBreaker sets the query tiebreaker func (q *MultiMatchQuery) TieBreaker(l float32) *MultiMatchQuery { q.params.TieBrk = l return q } -// Boost +// Boost sets the query boost func (q *MultiMatchQuery) Boost(l float32) *MultiMatchQuery { q.params.Boost = l return q @@ -164,27 +164,27 @@ func (q *MultiMatchQuery) ZeroTermsQuery(s ZeroTerms) *MultiMatchQuery { return q } -// MatchType is an enumeration type representing supported values for a +// MultiMatchType is an enumeration type representing supported values for a // multi match query's "type" parameter. type MultiMatchType uint8 const ( - // TypeBestFields is the "best_fields" type + // MatchTypeBestFields is the "best_fields" type MatchTypeBestFields MultiMatchType = iota - // TypeMostFields is the "most_fields" type + // MatchTypeMostFields is the "most_fields" type MatchTypeMostFields - // TypeMostFields is the "cross_fields" type + // MatchTypeCrossFields is the "cross_fields" type MatchTypeCrossFields - // TypeMostFields is the "phrase" type + // MatchTypePhrase is the "phrase" type MatchTypePhrase - // TypeMostFields is the "phrase_prefix" type + // MatchTypePhrasePrefix is the "phrase_prefix" type MatchTypePhrasePrefix - // TypeMostFields is the "bool_prefix" type + // MatchTypeBoolPrefix is the "bool_prefix" type MatchTypeBoolPrefix ) diff --git a/query_term_level.go b/query_term_level.go index d2fb7cc..d7e7e14 100644 --- a/query_term_level.go +++ b/query_term_level.go @@ -122,7 +122,7 @@ func (a *RangeQuery) Gt(val interface{}) *RangeQuery { return a } -// Gt sets that the value of field must be greater than or equal to the provided +// Gte sets that the value of field must be greater than or equal to the provided // value func (a *RangeQuery) Gte(val interface{}) *RangeQuery { a.params.Gte = val @@ -454,7 +454,7 @@ func (q *TermsQuery) Boost(b float32) *TermsQuery { // Map returns a map representation of the query, thus implementing the // Mappable interface. -func (q TermsQuery) Map() map[string]interface{} { +func (q *TermsQuery) Map() map[string]interface{} { innerMap := map[string]interface{}{q.field: q.values} if q.boost > 0 { innerMap["boost"] = q.boost @@ -511,7 +511,7 @@ func (q *TermsSetQuery) MinimumShouldMatchScript(script string) *TermsSetQuery { // Map returns a map representation of the query, thus implementing the // Mappable interface. -func (q TermsSetQuery) Map() map[string]interface{} { +func (q *TermsSetQuery) Map() map[string]interface{} { return map[string]interface{}{ "terms_set": map[string]interface{}{ q.field: structs.Map(q.params), @@ -519,7 +519,7 @@ func (q TermsSetQuery) Map() map[string]interface{} { } } -// geoFilterParams represents a query of type "geo_distance", as described in: +// GeoFilter geoFilterParams represents a query of type "geo_distance", as described in: // https://www.elastic.co/guide/en/elasticsearch/reference/7.17/query-dsl-geo-distance-query.html type GeoFilter struct { params geoFilterParams diff --git a/search.go b/search.go index ee37943..f0f51c9 100644 --- a/search.go +++ b/search.go @@ -4,6 +4,7 @@ import ( "bytes" "encoding/json" "fmt" + "github.com/elastic/go-elasticsearch/v8" "time" "github.com/elastic/go-elasticsearch/v8/esapi" -- 2.40.1