Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 1 addition & 4 deletions es/aggregation_avg.go
Original file line number Diff line number Diff line change
Expand Up @@ -139,8 +139,5 @@ func (avg avgAggType) Aggs(aggs ...aggsType) avgAggType {
}

func (avg avgAggType) putInTheField(key string, value any) avgAggType {
if avgAgg, ok := avg["avg"].(Object); ok {
avgAgg[key] = value
}
return avg
return genericPutInTheField(avg, "avg", key, value)
}
5 changes: 1 addition & 4 deletions es/aggregation_cardinality.go
Original file line number Diff line number Diff line change
Expand Up @@ -139,8 +139,5 @@ func (cardinality cardinalityAggType) Aggs(aggs ...aggsType) cardinalityAggType
}

func (cardinality cardinalityAggType) putInTheField(key string, value any) cardinalityAggType {
if cardinalityAgg, ok := cardinality["cardinality"].(Object); ok {
cardinalityAgg[key] = value
}
return cardinality
return genericPutInTheField(cardinality, "cardinality", key, value)
}
5 changes: 1 addition & 4 deletions es/aggregation_extended_stats.go
Original file line number Diff line number Diff line change
Expand Up @@ -135,8 +135,5 @@ func (extendedStats extendedStatsAggType) Aggs(aggs ...aggsType) extendedStatsAg
}

func (extendedStats extendedStatsAggType) putInTheField(key string, value any) extendedStatsAggType {
if extendedStatsAgg, ok := extendedStats["extended_stats"].(Object); ok {
extendedStatsAgg[key] = value
}
return extendedStats
return genericPutInTheField(extendedStats, "extended_stats", key, value)
}
5 changes: 1 addition & 4 deletions es/aggregation_max.go
Original file line number Diff line number Diff line change
Expand Up @@ -134,8 +134,5 @@ func (max maxAggType) Aggs(aggs ...aggsType) maxAggType {
}

func (max maxAggType) putInTheField(key string, value any) maxAggType {
if maxAgg, ok := max["max"].(Object); ok {
maxAgg[key] = value
}
return max
return genericPutInTheField(max, "max", key, value)
}
5 changes: 1 addition & 4 deletions es/aggregation_min.go
Original file line number Diff line number Diff line change
Expand Up @@ -134,8 +134,5 @@ func (min minAggType) Aggs(aggs ...aggsType) minAggType {
}

func (min minAggType) putInTheField(key string, value any) minAggType {
if minAgg, ok := min["min"].(Object); ok {
minAgg[key] = value
}
return min
return genericPutInTheField(min, "min", key, value)
}
5 changes: 1 addition & 4 deletions es/aggregation_multi_terms.go
Original file line number Diff line number Diff line change
Expand Up @@ -257,8 +257,5 @@ func (multiTerms multiTermsAggType) Aggs(aggs ...aggsType) multiTermsAggType {
}

func (multiTerms multiTermsAggType) putInTheField(key string, value any) multiTermsAggType {
if terms, ok := multiTerms["multi_terms"].(Object); ok {
terms[key] = value
}
return multiTerms
return genericPutInTheField(multiTerms, "multi_terms", key, value)
}
5 changes: 1 addition & 4 deletions es/aggregation_stats.go
Original file line number Diff line number Diff line change
Expand Up @@ -125,8 +125,5 @@ func (stats statsAggType) Aggs(aggs ...aggsType) statsAggType {
}

func (stats statsAggType) putInTheField(key string, value any) statsAggType {
if statsAgg, ok := stats["stats"].(Object); ok {
statsAgg[key] = value
}
return stats
return genericPutInTheField(stats, "stats", key, value)
}
5 changes: 1 addition & 4 deletions es/aggregation_sum.go
Original file line number Diff line number Diff line change
Expand Up @@ -126,8 +126,5 @@ func (sum sumAggType) Aggs(aggs ...aggsType) sumAggType {
}

func (sum sumAggType) putInTheField(key string, value any) sumAggType {
if sumAgg, ok := sum["sum"].(Object); ok {
sumAgg[key] = value
}
return sum
return genericPutInTheField(sum, "sum", key, value)
}
5 changes: 1 addition & 4 deletions es/aggregation_terms.go
Original file line number Diff line number Diff line change
Expand Up @@ -232,8 +232,5 @@ func (terms termsAggType) Aggs(aggs ...aggsType) termsAggType {
}

func (terms termsAggType) putInTheField(key string, value any) termsAggType {
if termsAgg, ok := terms["terms"].(Object); ok {
termsAgg[key] = value
}
return terms
return genericPutInTheField(terms, "terms", key, value)
}
5 changes: 1 addition & 4 deletions es/constant_score_query.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,8 +78,5 @@ func (cs constantScoreType) Boost(boost float64) constantScoreType {
}

func (cs constantScoreType) putInTheField(key string, value any) constantScoreType {
if constantScore, ok := cs["constant_score"].(Object); ok {
constantScore[key] = value
}
return cs
return genericPutInTheField(cs, "constant_score", key, value)
}
5 changes: 1 addition & 4 deletions es/exists_query.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,8 +103,5 @@ func ExistsIf(key string, condition bool) existsType {
}

func (e existsType) putInTheField(key string, value any) existsType {
if exists, ok := e["exists"].(Object); ok {
exists[key] = value
}
return e
return genericPutInTheField(e, "exists", key, value)
}
42 changes: 42 additions & 0 deletions es/generic_put_in_the_field.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package es

// genericPutInTheField safely inserts a key-value pair into a nested es.Object
// within the root map. It returns the root unchanged if parentKey doesn't exist
// or is not an es.Object.
func genericPutInTheField[T ~map[string]any](root T, parentKey, key string, value any) T {
if container, ok := root[parentKey].(Object); ok {
container[key] = value
}
return root
}

// genericPutInTheFieldOfFirstChild inserts a key-value pair into the first child
// es.Object found within a parent container. It traverses the parent's immediate children
// and modifies the first es.Object it encounters. This is useful for nested query structures
// where parameters need to be added to the first nested element (e.g., adding fields to
// the first query clause in a bool filter array).
func genericPutInTheFieldOfFirstChild[T ~map[string]any](root T, parentKey, key string, value any) T {
if container, ok := root[parentKey].(Object); ok {
for _, item := range container {
if child, chOk := item.(Object); chOk {
child[key] = value
return root
}
}
}
return root
}

// genericPutInTheFieldOfFirstObject inserts a key-value pair into the first es.Object
// found in the root map. It iterates through the root's values and modifies the first
// es.Object it encounters. Useful for query types that don't have a specific parent key
// wrapper (e.g., query_string, simple_query_string, sort).
func genericPutInTheFieldOfFirstObject[T ~map[string]any](root T, key string, value any) T {
for _, fieldObj := range root {
if fieldObject, ok := fieldObj.(Object); ok {
fieldObject[key] = value
break
}
}
return root
}
Loading
Loading