Skip to content

Commit

Permalink
feature: add boost and ignore_unmapped for NestedQuery
Browse files Browse the repository at this point in the history
  • Loading branch information
GokselKUCUKSAHIN committed Dec 28, 2024
1 parent b08c465 commit 81dceab
Show file tree
Hide file tree
Showing 2 changed files with 82 additions and 0 deletions.
42 changes: 42 additions & 0 deletions es/nested_query.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,48 @@ func (n nestedType) ScoreMode(scoreMode ScoreMode.ScoreMode) nestedType {
return n.putInNested("score_mode", scoreMode)
}

// Boost sets the "boost" field for the nested query.
//
// This method applies a boost factor to the nested query, influencing the relevance scoring
// of documents that match the query. It applies the boost to all nested fields in the query.
//
// Example usage:
//
// n := es.Nested("address").Boost(2.0)
// // n now has a "boost" field set to 2.0 in the nested query for the "address" field.
//
// Parameters:
// - boost: A float64 value representing the boost factor to be applied to the nested query.
//
// Returns:
//
// The updated nestedType object with the "boost" field set to the specified value.
func (n nestedType) Boost(boost float64) nestedType {
return n.putInNested("boost", boost)
}

// IgnoreUnmapped sets the "ignore_unmapped" field for the nested query.
//
// This method specifies whether to ignore unmapped fields in the nested query.
// If set to true, the query will not fail if a field is not mapped in the index.
//
// Example usage:
//
// n := es.Nested("address").IgnoreUnmapped(true)
// // n now has an "ignore_unmapped" field set to true in the nested query for the "address" field.
//
// Parameters:
// - ignoreUnmapped: A boolean value indicating whether to ignore unmapped fields.
// - true: Ignore unmapped fields and prevent query failures.
// - false: Do not ignore unmapped fields (default behavior).
//
// Returns:
//
// The updated nestedType object with the "ignore_unmapped" field set to the specified value.
func (n nestedType) IgnoreUnmapped(ignoreUnmapped bool) nestedType {
return n.putInNested("ignore_unmapped", ignoreUnmapped)
}

func (n nestedType) putInNested(key string, value any) nestedType {
if nestedObject, ok := n["nested"].(Object); ok {
nestedObject[key] = value
Expand Down
40 changes: 40 additions & 0 deletions es/nested_query_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,3 +78,43 @@ func Test_ScoreMod_should_add_score_mode_field_into_Nested(t *testing.T) {
bodyJSON := assert.MarshalWithoutError(t, query)
assert.Equal(t, "{\"query\":{\"nested\":{\"path\":\"nested.path\",\"query\":{},\"score_mode\":\"sum\"}}}", bodyJSON)
}

func Test_Nested_should_have_Boost_method(t *testing.T) {
// Given
n := es.Nested("path", es.Object{})

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

func Test_Boost_should_add_boost_field_into_Nested(t *testing.T) {
// Given
query := es.NewQuery(
es.Nested("nested.path", es.Object{}).Boost(5.56),
)

// When Then
assert.NotNil(t, query)
bodyJSON := assert.MarshalWithoutError(t, query)
assert.Equal(t, "{\"query\":{\"nested\":{\"boost\":5.56,\"path\":\"nested.path\",\"query\":{}}}}", bodyJSON)
}

func Test_Nested_should_have_IgnoreUnmapped_method(t *testing.T) {
// Given
n := es.Nested("path", es.Object{})

// When Then
assert.NotNil(t, n.IgnoreUnmapped)
}

func Test_IgnoreUnmapped_should_add_ignore_unmapped_field_into_Nested(t *testing.T) {
// Given
query := es.NewQuery(
es.Nested("nested.path", es.Object{}).IgnoreUnmapped(true),
)

// When Then
assert.NotNil(t, query)
bodyJSON := assert.MarshalWithoutError(t, query)
assert.Equal(t, "{\"query\":{\"nested\":{\"ignore_unmapped\":true,\"path\":\"nested.path\",\"query\":{}}}}", bodyJSON)
}

0 comments on commit 81dceab

Please sign in to comment.