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
46 changes: 46 additions & 0 deletions es/enums/boost-mode/boost_mode.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package boostmode

// BoostMode represents the different boost modes for function_score queries.
//
// BoostMode is a string type used to specify how the computed score from the
// function_score query is combined with the query score. It provides various
// options for combining the function score with the original query score.
//
// Example usage:
//
// var mode BoostMode = Multiply
//
// // Use mode in a function_score query configuration
//
// Constants:
// - Multiply: Multiply the query score with the function score (default).
// - Replace: Replace the query score with the function score.
// - Sum: Add the query score and the function score.
// - Avg: Average the query score and the function score.
// - Max: Use the maximum of the query score and the function score.
// - Min: Use the minimum of the query score and the function score.
type BoostMode string

const (
// Multiply indicates that the query score should be multiplied with the function score.
Multiply BoostMode = "multiply"

// Replace indicates that the query score should be replaced with the function score.
Replace BoostMode = "replace"

// Sum indicates that the query score and the function score should be added.
Sum BoostMode = "sum"

// Avg indicates that the query score and the function score should be averaged.
Avg BoostMode = "avg"

// Max indicates that the maximum of the query score and the function score should be used.
Max BoostMode = "max"

// Min indicates that the minimum of the query score and the function score should be used.
Min BoostMode = "min"
)

func (boostMode BoostMode) String() string {
return string(boostMode)
}
29 changes: 29 additions & 0 deletions es/enums/boost-mode/boost_mode_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package boostmode_test

import (
"testing"

BoostMode "github.com/Trendyol/es-query-builder/es/enums/boost-mode"

"github.com/Trendyol/es-query-builder/test/assert"
)

func Test_BoostModeString(t *testing.T) {
tests := []struct {
mode BoostMode.BoostMode
result string
}{
{BoostMode.Multiply, "multiply"},
{BoostMode.Replace, "replace"},
{BoostMode.Sum, "sum"},
{BoostMode.Avg, "avg"},
{BoostMode.Max, "max"},
{BoostMode.Min, "min"},
}

for _, test := range tests {
t.Run(test.result, func(t *testing.T) {
assert.Equal(t, test.result, test.mode.String())
})
}
}
61 changes: 61 additions & 0 deletions es/enums/modifier/modifier.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
package modifier

// Modifier represents the different modifier functions for field_value_factor in function_score queries.
//
// Modifier is a string type used to specify the mathematical function applied to the field value
// before it is used to compute the score in a field_value_factor function.
//
// Example usage:
//
// var mod Modifier = Log1p
//
// // Use mod in a field_value_factor configuration
//
// Constants:
// - None: Do not apply any multiplier to the field value.
// - Log: Take the common logarithm of the field value.
// - Log1p: Add 1 to the field value and take the common logarithm.
// - Log2p: Add 2 to the field value and take the common logarithm.
// - Ln: Take the natural logarithm of the field value.
// - Ln1p: Add 1 to the field value and take the natural logarithm.
// - Ln2p: Add 2 to the field value and take the natural logarithm.
// - Square: Square the field value (multiply it with itself).
// - Sqrt: Take the square root of the field value.
// - Reciprocal: Reciprocate the field value (1/x).
type Modifier string

const (
// None indicates that no modifier should be applied to the field value.
None Modifier = "none"

// Log indicates that the common logarithm should be applied to the field value.
Log Modifier = "log"

// Log1p indicates that 1 should be added to the field value before taking the common logarithm.
Log1p Modifier = "log1p"

// Log2p indicates that 2 should be added to the field value before taking the common logarithm.
Log2p Modifier = "log2p"

// Ln indicates that the natural logarithm should be applied to the field value.
Ln Modifier = "ln"

// Ln1p indicates that 1 should be added to the field value before taking the natural logarithm.
Ln1p Modifier = "ln1p"

// Ln2p indicates that 2 should be added to the field value before taking the natural logarithm.
Ln2p Modifier = "ln2p"

// Square indicates that the field value should be squared.
Square Modifier = "square"

// Sqrt indicates that the square root should be applied to the field value.
Sqrt Modifier = "sqrt"

// Reciprocal indicates that the reciprocal (1/x) should be applied to the field value.
Reciprocal Modifier = "reciprocal"
)

func (modifier Modifier) String() string {
return string(modifier)
}
33 changes: 33 additions & 0 deletions es/enums/modifier/modifier_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package modifier_test

import (
"testing"

Modifier "github.com/Trendyol/es-query-builder/es/enums/modifier"

"github.com/Trendyol/es-query-builder/test/assert"
)

func Test_ModifierString(t *testing.T) {
tests := []struct {
modifier Modifier.Modifier
result string
}{
{Modifier.None, "none"},
{Modifier.Log, "log"},
{Modifier.Log1p, "log1p"},
{Modifier.Log2p, "log2p"},
{Modifier.Ln, "ln"},
{Modifier.Ln1p, "ln1p"},
{Modifier.Ln2p, "ln2p"},
{Modifier.Square, "square"},
{Modifier.Sqrt, "sqrt"},
{Modifier.Reciprocal, "reciprocal"},
}

for _, test := range tests {
t.Run(test.result, func(t *testing.T) {
assert.Equal(t, test.result, test.modifier.String())
})
}
}
38 changes: 38 additions & 0 deletions es/enums/multi-values-mode/multi_values_mode.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package multivaluesmode

// MultiValuesMode represents the different modes for handling multi-valued fields in decay functions.
//
// MultiValuesMode is a string type used to specify how a decay function should handle
// fields that contain multiple values. It determines which value is chosen for the
// distance calculation.
//
// Example usage:
//
// var mode MultiValuesMode = Min
//
// // Use mode in a decay function configuration
//
// Constants:
// - Min: Use the minimum distance from the origin.
// - Max: Use the maximum distance from the origin.
// - Avg: Use the average distance from the origin.
// - Sum: Use the sum of all distances from the origin.
type MultiValuesMode string

const (
// Min indicates that the minimum distance should be used.
Min MultiValuesMode = "min"

// Max indicates that the maximum distance should be used.
Max MultiValuesMode = "max"

// Avg indicates that the average distance should be used.
Avg MultiValuesMode = "avg"

// Sum indicates that the sum of distances should be used.
Sum MultiValuesMode = "sum"
)

func (multiValuesMode MultiValuesMode) String() string {
return string(multiValuesMode)
}
27 changes: 27 additions & 0 deletions es/enums/multi-values-mode/multi_values_mode_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package multivaluesmode_test

import (
"testing"

MultiValuesMode "github.com/Trendyol/es-query-builder/es/enums/multi-values-mode"

"github.com/Trendyol/es-query-builder/test/assert"
)

func Test_MultiValuesModeString(t *testing.T) {
tests := []struct {
mode MultiValuesMode.MultiValuesMode
result string
}{
{MultiValuesMode.Min, "min"},
{MultiValuesMode.Max, "max"},
{MultiValuesMode.Avg, "avg"},
{MultiValuesMode.Sum, "sum"},
}

for _, test := range tests {
t.Run(test.result, func(t *testing.T) {
assert.Equal(t, test.result, test.mode.String())
})
}
}
134 changes: 134 additions & 0 deletions es/function_score_decay.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,134 @@
package es

import MultiValuesMode "github.com/Trendyol/es-query-builder/es/enums/multi-values-mode"

type decayFunctionType Object

// Decay creates a new es.decayFunctionType object with the specified field.
//
// This function initializes a decay function configuration for use with decay functions
// (gauss, linear, exp) in a function_score query. The field parameter represents the name
// of the field on which the decay function operates.
//
// Example usage:
//
// d := es.Decay("date").Origin("now").Scale("10d")
// // d now contains a decay configuration for the "date" field.
//
// Parameters:
// - field: A string representing the field name for the decay function.
//
// Returns:
//
// An es.decayFunctionType object with the specified field.
func Decay(field string) decayFunctionType {
return decayFunctionType{
field: Object{},
}
}

// Origin sets the "origin" parameter in the decay function field configuration.
//
// This method specifies the point of origin used to calculate distance. The origin must
// be provided for numeric and geo fields. For date fields, the default is "now".
//
// Example usage:
//
// d := es.Decay("date").Origin("now")
// // d now includes an "origin" parameter set to "now" for the "date" field.
//
// Parameters:
// - origin: A value representing the origin point. The type depends on the field type.
//
// Returns:
//
// The updated es.decayFunctionType object with the "origin" parameter set.
func (d decayFunctionType) Origin(origin any) decayFunctionType {
return d.putInTheField("origin", origin)
}

// Scale sets the "scale" parameter in the decay function field configuration.
//
// This method specifies the distance from the origin at which the computed score equals
// the decay parameter. For geo fields, the scale can be defined as a number with a unit
// (e.g., "1km", "12mi"). For date fields, it can be defined as a duration (e.g., "10d").
//
// Example usage:
//
// d := es.Decay("date").Origin("now").Scale("10d")
// // d now includes a "scale" parameter set to "10d" for the "date" field.
//
// Parameters:
// - scale: A value representing the scale distance.
//
// Returns:
//
// The updated es.decayFunctionType object with the "scale" parameter set.
func (d decayFunctionType) Scale(scale any) decayFunctionType {
return d.putInTheField("scale", scale)
}

// Offset sets the "offset" parameter in the decay function field configuration.
//
// This method specifies a distance from the origin within which no decay is applied.
// Documents within this offset distance from the origin receive a score of 1.0.
//
// Example usage:
//
// d := es.Decay("date").Origin("now").Scale("10d").Offset("5d")
// // d now includes an "offset" parameter set to "5d" for the "date" field.
//
// Parameters:
// - offset: A value representing the offset distance.
//
// Returns:
//
// The updated es.decayFunctionType object with the "offset" parameter set.
func (d decayFunctionType) Offset(offset any) decayFunctionType {
return d.putInTheField("offset", offset)
}

// DecayValue sets the "decay" parameter in the decay function field configuration.
//
// This method specifies the score at the scale distance from the origin. The default
// value is 0.5 if not specified.
//
// Example usage:
//
// d := es.Decay("date").Origin("now").Scale("10d").DecayValue(0.5)
// // d now includes a "decay" parameter set to 0.5 for the "date" field.
//
// Parameters:
// - decay: A float64 value representing the decay score at the scale distance.
//
// Returns:
//
// The updated es.decayFunctionType object with the "decay" parameter set.
func (d decayFunctionType) DecayValue(decay float64) decayFunctionType {
return d.putInTheField("decay", decay)
}

// MultiValueMode sets the "multi_value_mode" parameter in the decay function configuration.
//
// This method specifies how the decay function should handle fields that contain multiple values.
// It determines which value is chosen for the distance calculation.
//
// Example usage:
//
// d := es.Decay("location").Origin("0,0").Scale("5km").MultiValueMode(MultiValuesMode.Min)
// // d now includes a "multi_value_mode" parameter set to "min".
//
// Parameters:
// - multiValueMode: A MultiValuesMode.MultiValuesMode value representing the mode.
//
// Returns:
//
// The updated es.decayFunctionType object with the "multi_value_mode" parameter set.
func (d decayFunctionType) MultiValueMode(multiValueMode MultiValuesMode.MultiValuesMode) decayFunctionType {
d["multi_value_mode"] = multiValueMode
return d
}

func (d decayFunctionType) putInTheField(key string, value any) decayFunctionType {
return genericPutInTheFieldOfFirstObject(d, key, value)
}
Loading
Loading