Support Elasticsearch v8 #7

Merged
maxim.yanchuk merged 3 commits from es8 into master 2023-11-01 21:30:55 +00:00
17 changed files with 65 additions and 73 deletions

View File

@ -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: `esquery` is a Go module. To install, simply run this in your project's root directory:
```bash ```bash
go get github.com/aquasecurity/esquery go get github.com/aquasecurity/esquery/v8
``` ```
## Usage ## Usage
@ -51,8 +51,8 @@ import (
"context" "context"
"log" "log"
"github.com/aquasecurity/esquery" "github.com/aquasecurity/esquery/v8"
"github.com/elastic/go-elasticsearch/v7" "github.com/elastic/go-elasticsearch/v8"
) )
func main() { func main() {

View File

@ -6,7 +6,7 @@ type FilterAggregation struct {
aggs []Aggregation 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. // the "Agg" suffix to prevent conflict with the "filter" query.
func FilterAgg(name string, filter Mappable) *FilterAggregation { func FilterAgg(name string, filter Mappable) *FilterAggregation {
return &FilterAggregation{ return &FilterAggregation{

View File

@ -44,8 +44,7 @@ func (agg *BaseAgg) Map() map[string]interface{} {
} }
// AvgAgg represents an aggregation of type "avg", as described in // AvgAgg represents an aggregation of type "avg", as described in
// https://www.elastic.co/guide/en/elasticsearch/reference/ // https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-metrics-avg-aggregation.html
// current/search-aggregations-metrics-avg-aggregation.html
type AvgAgg struct { type AvgAgg struct {
*BaseAgg `structs:",flatten"` *BaseAgg `structs:",flatten"`
} }
@ -68,8 +67,7 @@ func (agg *AvgAgg) Missing(val interface{}) *AvgAgg {
//----------------------------------------------------------------------------// //----------------------------------------------------------------------------//
// WeightedAvgAgg represents an aggregation of type "weighted_avg", as described // WeightedAvgAgg represents an aggregation of type "weighted_avg", as described
// in https://www.elastic.co/guide/en/elasticsearch/reference/ // in https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-metrics-weight-avg-aggregation.html
// current/search-aggregations-metrics-weight-avg-aggregation.html
type WeightedAvgAgg struct { type WeightedAvgAgg struct {
name string name string
apiName string apiName string
@ -106,7 +104,7 @@ func (agg *WeightedAvgAgg) Value(field string, missing ...interface{}) *Weighted
return agg 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. // missing a value for the field.
func (agg *WeightedAvgAgg) Weight(field string, missing ...interface{}) *WeightedAvgAgg { func (agg *WeightedAvgAgg) Weight(field string, missing ...interface{}) *WeightedAvgAgg {
agg.Weig = new(BaseAggParams) agg.Weig = new(BaseAggParams)
@ -128,8 +126,7 @@ func (agg *WeightedAvgAgg) Map() map[string]interface{} {
//----------------------------------------------------------------------------// //----------------------------------------------------------------------------//
// CardinalityAgg represents an aggregation of type "cardinality", as described // CardinalityAgg represents an aggregation of type "cardinality", as described
// in https://www.elastic.co/guide/en/elasticsearch/reference/ // in https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-metrics-cardinality-aggregation.html
// current/search-aggregations-metrics-cardinality-aggregation.html
type CardinalityAgg struct { type CardinalityAgg struct {
*BaseAgg `structs:",flatten"` *BaseAgg `structs:",flatten"`
@ -169,8 +166,7 @@ func (agg *CardinalityAgg) Map() map[string]interface{} {
//----------------------------------------------------------------------------// //----------------------------------------------------------------------------//
// MaxAgg represents an aggregation of type "max", as described in: // MaxAgg represents an aggregation of type "max", as described in:
// https://www.elastic.co/guide/en/elasticsearch/reference/ // https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-metrics-max-aggregation.html
// current/search-aggregations-metrics-max-aggregation.html
type MaxAgg struct { type MaxAgg struct {
*BaseAgg `structs:",flatten"` *BaseAgg `structs:",flatten"`
} }
@ -193,8 +189,7 @@ func (agg *MaxAgg) Missing(val interface{}) *MaxAgg {
//----------------------------------------------------------------------------// //----------------------------------------------------------------------------//
// MinAgg represents an aggregation of type "min", as described in: // MinAgg represents an aggregation of type "min", as described in:
// https://www.elastic.co/guide/en/elasticsearch/reference/ // https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-metrics-min-aggregation.html
// current/search-aggregations-metrics-min-aggregation.html
type MinAgg struct { type MinAgg struct {
*BaseAgg `structs:",flatten"` *BaseAgg `structs:",flatten"`
} }
@ -217,8 +212,7 @@ func (agg *MinAgg) Missing(val interface{}) *MinAgg {
//----------------------------------------------------------------------------// //----------------------------------------------------------------------------//
// SumAgg represents an aggregation of type "sum", as described in: // SumAgg represents an aggregation of type "sum", as described in:
// https://www.elastic.co/guide/en/elasticsearch/reference/ // https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-metrics-sum-aggregation.html
// current/search-aggregations-metrics-sum-aggregation.html
type SumAgg struct { type SumAgg struct {
*BaseAgg `structs:",flatten"` *BaseAgg `structs:",flatten"`
} }
@ -241,8 +235,7 @@ func (agg *SumAgg) Missing(val interface{}) *SumAgg {
//----------------------------------------------------------------------------// //----------------------------------------------------------------------------//
// ValueCountAgg represents an aggregation of type "value_count", as described // ValueCountAgg represents an aggregation of type "value_count", as described
// in https://www.elastic.co/guide/en/elasticsearch/reference/ // in https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-metrics-valuecount-aggregation.html
// current/search-aggregations-metrics-valuecount-aggregation.html
type ValueCountAgg struct { type ValueCountAgg struct {
*BaseAgg `structs:",flatten"` *BaseAgg `structs:",flatten"`
} }
@ -258,8 +251,7 @@ func ValueCount(name, field string) *ValueCountAgg {
//----------------------------------------------------------------------------// //----------------------------------------------------------------------------//
// PercentilesAgg represents an aggregation of type "percentiles", as described // PercentilesAgg represents an aggregation of type "percentiles", as described
// in https://www.elastic.co/guide/en/elasticsearch/reference/ // in https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-metrics-percentile-aggregation.html
// current/search-aggregations-metrics-percentile-aggregation.html
type PercentilesAgg struct { type PercentilesAgg struct {
*BaseAgg `structs:",flatten"` *BaseAgg `structs:",flatten"`
@ -334,8 +326,7 @@ func (agg *PercentilesAgg) Map() map[string]interface{} {
//----------------------------------------------------------------------------// //----------------------------------------------------------------------------//
// StatsAgg represents an aggregation of type "stats", as described in: // StatsAgg represents an aggregation of type "stats", as described in:
// https://www.elastic.co/guide/en/elasticsearch/reference/ // https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-metrics-stats-aggregation.html
// current/search-aggregations-metrics-stats-aggregation.html
type StatsAgg struct { type StatsAgg struct {
*BaseAgg `structs:",flatten"` *BaseAgg `structs:",flatten"`
} }
@ -357,8 +348,7 @@ func (agg *StatsAgg) Missing(val interface{}) *StatsAgg {
// ---------------------------------------------------------------------------// // ---------------------------------------------------------------------------//
// StringStatsAgg represents an aggregation of type "string_stats", as described // StringStatsAgg represents an aggregation of type "string_stats", as described
// in https://www.elastic.co/guide/en/elasticsearch/reference/ // in https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-metrics-string-stats-aggregation.html
// current/search-aggregations-metrics-string-stats-aggregation.html
type StringStatsAgg struct { type StringStatsAgg struct {
*BaseAgg `structs:",flatten"` *BaseAgg `structs:",flatten"`
@ -399,8 +389,7 @@ func (agg *StringStatsAgg) Map() map[string]interface{} {
// ---------------------------------------------------------------------------// // ---------------------------------------------------------------------------//
// TopHitsAgg represents an aggregation of type "top_hits", as described // TopHitsAgg represents an aggregation of type "top_hits", as described
// in https://www.elastic.co/guide/en/elasticsearch/reference/ // in https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-metrics-top-hits-aggregation.html
// current/search-aggregations-metrics-top-hits-aggregation.html
type TopHitsAgg struct { type TopHitsAgg struct {
name string name string
from uint64 from uint64

View File

@ -20,7 +20,7 @@ func (agg *NestedAggregation) Name() string {
return agg.name return agg.name
} }
// NumberOfFragments sets the aggregations path // Path sets the aggregations path
func (agg *NestedAggregation) Path(p string) *NestedAggregation { func (agg *NestedAggregation) Path(p string) *NestedAggregation {
agg.path = p agg.path = p
return agg return agg

View File

@ -4,8 +4,8 @@ import (
"bytes" "bytes"
"encoding/json" "encoding/json"
"github.com/elastic/go-elasticsearch/v7" "github.com/elastic/go-elasticsearch/v8"
"github.com/elastic/go-elasticsearch/v7/esapi" "github.com/elastic/go-elasticsearch/v8/esapi"
) )
// CountRequest represents a request to get the number of matches for a search // CountRequest represents a request to get the number of matches for a search

View File

@ -1,8 +1,8 @@
package esquery package esquery
import ( import (
"github.com/elastic/go-elasticsearch/v7" "github.com/elastic/go-elasticsearch/v8"
"github.com/elastic/go-elasticsearch/v7/esapi" "github.com/elastic/go-elasticsearch/v8/esapi"
) )
// CustomQueryMap represents an arbitrary query map for custom queries. // CustomQueryMap represents an arbitrary query map for custom queries.
@ -21,7 +21,7 @@ func CustomQuery(m map[string]interface{}) *CustomQueryMap {
// Map returns the custom query as a map[string]interface{}, thus implementing // Map returns the custom query as a map[string]interface{}, thus implementing
// the Mappable interface. // the Mappable interface.
func (m *CustomQueryMap) Map() map[string]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 // Run executes the custom query using the provided ElasticSearch client. Zero

View File

@ -4,8 +4,8 @@ import (
"bytes" "bytes"
"encoding/json" "encoding/json"
"github.com/elastic/go-elasticsearch/v7" "github.com/elastic/go-elasticsearch/v8"
"github.com/elastic/go-elasticsearch/v7/esapi" "github.com/elastic/go-elasticsearch/v8/esapi"
) )
// DeleteRequest represents a request to ElasticSearch's Delete By Query API, // DeleteRequest represents a request to ElasticSearch's Delete By Query API,

4
es.go
View File

@ -32,8 +32,8 @@
// "context" // "context"
// "log" // "log"
// //
// "github.com/aquasecurity/esquery" // "github.com/aquasecurity/esquery/v8"
// "github.com/elastic/go-elasticsearch/v7" // "github.com/elastic/go-elasticsearch/v8"
// ) // )
// //
// func main() { // func main() {

4
go.mod
View File

@ -1,9 +1,9 @@
module github.com/okdanta/esquery module github.com/aquasecurity/esquery/v8
go 1.13 go 1.13
require ( require (
github.com/elastic/go-elasticsearch/v7 v7.6.0 github.com/elastic/go-elasticsearch/v8 v8.10.1
github.com/fatih/structs v1.1.0 github.com/fatih/structs v1.1.0
github.com/jgroeneveld/schema v1.0.0 // indirect github.com/jgroeneveld/schema v1.0.0 // indirect
github.com/jgroeneveld/trial v2.0.0+incompatible github.com/jgroeneveld/trial v2.0.0+incompatible

6
go.sum
View File

@ -1,5 +1,7 @@
github.com/elastic/go-elasticsearch/v7 v7.6.0 h1:sYpGLpEFHgLUKLsZUBfuaVI9QgHjS3JdH9fX4/z8QI8= github.com/elastic/elastic-transport-go/v8 v8.3.0 h1:DJGxovyQLXGr62e9nDMPSxRyWION0Bh6d9eCFBriiHo=
github.com/elastic/go-elasticsearch/v7 v7.6.0/go.mod h1:OJ4wdbtDNk5g503kvlHLyErCgQwwzmDtaFC4XyOxXA4= 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 h1:Q7juDM0QtcnhCpeyLGQKyg4TOIghuNXrkL32pHAUMxo=
github.com/fatih/structs v1.1.0/go.mod h1:9NiDSp5zOcgEDl+j00MP/WkGVPOlPRLejGD8Ga6PJ7M= github.com/fatih/structs v1.1.0/go.mod h1:9NiDSp5zOcgEDl+j00MP/WkGVPOlPRLejGD8Ga6PJ7M=
github.com/jgroeneveld/schema v1.0.0 h1:J0E10CrOkiSEsw6dfb1IfrDJD14pf6QLVJ3tRPl/syI= github.com/jgroeneveld/schema v1.0.0 h1:J0E10CrOkiSEsw6dfb1IfrDJD14pf6QLVJ3tRPl/syI=

View File

@ -252,8 +252,9 @@ func (a HighlightBoundaryScanner) String() string {
return "sentence" return "sentence"
case BoundaryScannerWord: case BoundaryScannerWord:
return "word" return "word"
default:
return ""
} }
return ""
} }
type HighlightEncoder uint8 type HighlightEncoder uint8
@ -281,7 +282,7 @@ func (a HighlightEncoder) String() string {
type HighlightFragmenter uint8 type HighlightFragmenter uint8
const ( const (
// FragmentSpan is the "span" value // FragmenterSpan is the "span" value
FragmenterSpan HighlightFragmenter = iota FragmenterSpan HighlightFragmenter = iota
// FragmenterSimple is the "simple" value // FragmenterSimple is the "simple" value
@ -336,6 +337,7 @@ func (a HighlightTagsSchema) String() string {
switch a { switch a {
case TagsSchemaStyled: case TagsSchemaStyled:
return "styled" return "styled"
default:
return ""
} }
return ""
} }

View File

@ -32,7 +32,7 @@ func (q *BoolQuery) Filter(filter ...Mappable) *BoolQuery {
return q 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. // can be called multiple times, queries will be appended to existing ones.
func (q *BoolQuery) MustNot(mustnot ...Mappable) *BoolQuery { func (q *BoolQuery) MustNot(mustnot ...Mappable) *BoolQuery {
q.mustNot = append(q.mustNot, mustnot...) q.mustNot = append(q.mustNot, mustnot...)

View File

@ -15,13 +15,13 @@ func (q *CombinedFieldsQuery) Map() map[string]interface{} {
} }
type combinedFieldsParams struct { type combinedFieldsParams struct {
Qry interface{} `structs:"query"` Qry interface{} `structs:"query"`
Fields []string `structs:"fields"` Fields []string `structs:"fields"`
Boost float32 `structs:"boost,omitempty"` Boost float32 `structs:"boost,omitempty"`
AutoGenerate *bool `structs:"auto_generate_synonyms_phrase_query,omitempty"` AutoGenerate *bool `structs:"auto_generate_synonyms_phrase_query,omitempty"`
Op MatchOperator `structs:"operator,string,omitempty"` Op MatchOperator `structs:"operator,string,omitempty"`
MinMatch string `structs:"minimum_should_match,omitempty"` MinMatch string `structs:"minimum_should_match,omitempty"`
ZeroTerms ZeroTerms `structs:"zero_terms_query,string,omitempty"` ZeroTerms ZeroTerms `structs:"zero_terms_query,string,omitempty"`
} }
// CombinedFields creates a new query of type "combined_fields" // CombinedFields creates a new query of type "combined_fields"
@ -57,7 +57,7 @@ func (q *CombinedFieldsQuery) AutoGenerateSynonymsPhraseQuery(b bool) *CombinedF
return q return q
} }
// Boost // Boost sets the query boost
func (q *CombinedFieldsQuery) Boost(l float32) *CombinedFieldsQuery { func (q *CombinedFieldsQuery) Boost(l float32) *CombinedFieldsQuery {
q.params.Boost = l q.params.Boost = l
return q return q
@ -83,4 +83,3 @@ func (q *CombinedFieldsQuery) ZeroTermsQuery(s ZeroTerms) *CombinedFieldsQuery {
q.params.ZeroTerms = s q.params.ZeroTerms = s
return q return q
} }

View File

@ -10,7 +10,7 @@ const (
// TypeMatch denotes a query of type "match" // TypeMatch denotes a query of type "match"
TypeMatch matchType = iota TypeMatch matchType = iota
// TypeMatchBool denotes a query of type "match_bool_prefix" // TypeMatchBoolPrefix denotes a query of type "match_bool_prefix"
TypeMatchBoolPrefix TypeMatchBoolPrefix
// TypeMatchPhrase denotes a query of type "match_phrase" // 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. // Boost sets the boost value of the query.
func (a *MatchQuery) Boost(b float32) *MatchQuery { func (q *MatchQuery) Boost(b float32) *MatchQuery {
a.params.Boost = b q.params.Boost = b
return a return q
} }
// MatchOperator is an enumeration type representing supported values for a // MatchOperator is an enumeration type representing supported values for a

View File

@ -100,13 +100,13 @@ func (q *MultiMatchQuery) PrefixLength(l uint16) *MultiMatchQuery {
return q return q
} }
// TieBreaker // TieBreaker sets the query tiebreaker
func (q *MultiMatchQuery) TieBreaker(l float32) *MultiMatchQuery { func (q *MultiMatchQuery) TieBreaker(l float32) *MultiMatchQuery {
q.params.TieBrk = l q.params.TieBrk = l
return q return q
} }
// Boost // Boost sets the query boost
func (q *MultiMatchQuery) Boost(l float32) *MultiMatchQuery { func (q *MultiMatchQuery) Boost(l float32) *MultiMatchQuery {
q.params.Boost = l q.params.Boost = l
return q return q
@ -164,27 +164,27 @@ func (q *MultiMatchQuery) ZeroTermsQuery(s ZeroTerms) *MultiMatchQuery {
return q 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. // multi match query's "type" parameter.
type MultiMatchType uint8 type MultiMatchType uint8
const ( const (
// TypeBestFields is the "best_fields" type // MatchTypeBestFields is the "best_fields" type
MatchTypeBestFields MultiMatchType = iota MatchTypeBestFields MultiMatchType = iota
// TypeMostFields is the "most_fields" type // MatchTypeMostFields is the "most_fields" type
MatchTypeMostFields MatchTypeMostFields
// TypeMostFields is the "cross_fields" type // MatchTypeCrossFields is the "cross_fields" type
MatchTypeCrossFields MatchTypeCrossFields
// TypeMostFields is the "phrase" type // MatchTypePhrase is the "phrase" type
MatchTypePhrase MatchTypePhrase
// TypeMostFields is the "phrase_prefix" type // MatchTypePhrasePrefix is the "phrase_prefix" type
MatchTypePhrasePrefix MatchTypePhrasePrefix
// TypeMostFields is the "bool_prefix" type // MatchTypeBoolPrefix is the "bool_prefix" type
MatchTypeBoolPrefix MatchTypeBoolPrefix
) )

View File

@ -122,7 +122,7 @@ func (a *RangeQuery) Gt(val interface{}) *RangeQuery {
return a 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 // value
func (a *RangeQuery) Gte(val interface{}) *RangeQuery { func (a *RangeQuery) Gte(val interface{}) *RangeQuery {
a.params.Gte = val 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 // Map returns a map representation of the query, thus implementing the
// Mappable interface. // Mappable interface.
func (q TermsQuery) Map() map[string]interface{} { func (q *TermsQuery) Map() map[string]interface{} {
innerMap := map[string]interface{}{q.field: q.values} innerMap := map[string]interface{}{q.field: q.values}
if q.boost > 0 { if q.boost > 0 {
innerMap["boost"] = q.boost 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 // Map returns a map representation of the query, thus implementing the
// Mappable interface. // Mappable interface.
func (q TermsSetQuery) Map() map[string]interface{} { func (q *TermsSetQuery) Map() map[string]interface{} {
return map[string]interface{}{ return map[string]interface{}{
"terms_set": map[string]interface{}{ "terms_set": map[string]interface{}{
q.field: structs.Map(q.params), 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 // https://www.elastic.co/guide/en/elasticsearch/reference/7.17/query-dsl-geo-distance-query.html
type GeoFilter struct { type GeoFilter struct {
params geoFilterParams params geoFilterParams

View File

@ -4,10 +4,10 @@ import (
"bytes" "bytes"
"encoding/json" "encoding/json"
"fmt" "fmt"
"github.com/elastic/go-elasticsearch/v8"
"time" "time"
"github.com/elastic/go-elasticsearch/v7" "github.com/elastic/go-elasticsearch/v8/esapi"
"github.com/elastic/go-elasticsearch/v7/esapi"
) )
// SearchRequest represents a request to ElasticSearch's Search API, described // SearchRequest represents a request to ElasticSearch's Search API, described