Skip to content

Commit

Permalink
feature: add boost flag to terms query
Browse files Browse the repository at this point in the history
  • Loading branch information
GokselKUCUKSAHIN committed Oct 26, 2024
1 parent 1a08d8e commit ef16f06
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 0 deletions.
31 changes: 31 additions & 0 deletions es/terms_query.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,37 @@ func Terms(key string, values ...any) termsType {
}
}

func (t termsType) putInTheField(key string, value any) termsType {
if terms, ok := t["terms"].(Object); ok {
terms[key] = value
}
return t
}

// Boost sets the "boost" parameter in a termsType query.
//
// This method allows you to specify a boost factor for the terms query,
// which influences the relevance score of documents matching any of the
// specified terms. A higher boost value increases the importance of the
// terms in the query, resulting in higher scores for documents that match
// any of these terms.
//
// Example usage:
//
// t := Terms().Boost(1.5)
// // t now includes a "boost" parameter set to 1.5.
//
// Parameters:
// - boost: A float64 value representing the boost factor for the terms
// query.
//
// Returns:
//
// The updated termsType object with the "boost" parameter set.
func (t termsType) Boost(boost float64) termsType {
return t.putInTheField("boost", boost)
}

// TermsArray creates a new termsType object with the specified key and values as a slice.
//
// This function initializes a termsType object for a terms query, where the key
Expand Down
21 changes: 21 additions & 0 deletions es/terms_query_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,27 @@ func Test_Terms_method_should_create_termsType(t *testing.T) {
assert.IsTypeString(t, "es.termsType", b)
}

func Test_Terms_should_have_Boost_method(t *testing.T) {
// Given
terms := es.Terms("key", "value1", "value2", "value3")

// When Then
assert.NotNil(t, terms.Boost)
}

func Test_Terms_Boost_should_create_json_with_boost_field_inside_terms(t *testing.T) {
// Given
query := es.NewQuery(
es.Terms("sector.name", "a1", "b2", "c3").
Boost(2.718),
)

// When Then
assert.NotNil(t, query)
bodyJSON := assert.MarshalWithoutError(t, query)
assert.Equal(t, "{\"query\":{\"terms\":{\"boost\":2.718,\"sector.name\":[\"a1\",\"b2\",\"c3\"]}}}", bodyJSON)
}

//// TermsArray ////

func Test_TermsArray_should_exist_on_es_package(t *testing.T) {
Expand Down

0 comments on commit ef16f06

Please sign in to comment.