Skip to content

Commit

Permalink
feat: adds specialised terms with string... values (#6)
Browse files Browse the repository at this point in the history
  • Loading branch information
alekseizorin authored Oct 12, 2023
1 parent d0f0612 commit bd6436a
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 0 deletions.
42 changes: 42 additions & 0 deletions query_term_level.go
Original file line number Diff line number Diff line change
Expand Up @@ -465,6 +465,48 @@ func (q TermsQuery) Map() map[string]interface{} {

//----------------------------------------------------------------------------//

// TermsQuery represents a query of type "terms" with string values, as described in:
// https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-terms-query.html
type StringTermsQuery struct {
field string
values []string
boost float32
}

// Terms creates a new query of type "terms" on the provided field, and
// optionally with the provided term values.
func StringTerms(field string, values ...string) *StringTermsQuery {
return &StringTermsQuery{
field: field,
values: values,
}
}

// Values sets the term values for the query.
func (q *StringTermsQuery) Values(values ...string) *StringTermsQuery {
q.values = values
return q
}

// Boost sets the boost value of the query.
func (q *StringTermsQuery) Boost(b float32) *StringTermsQuery {
q.boost = b
return q
}

// Map returns a map representation of the query, thus implementing the
// Mappable interface.
func (q *StringTermsQuery) Map() map[string]interface{} {
innerMap := map[string]any{q.field: q.values}
if q.boost > 0 {
innerMap["boost"] = q.boost
}

return map[string]interface{}{"terms": innerMap}
}

//----------------------------------------------------------------------------//

// TermsSetQuery represents a query of type "terms_set", as described in:
// https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-terms-set-query.html
type TermsSetQuery struct {
Expand Down
10 changes: 10 additions & 0 deletions query_term_level_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,16 @@ func TestTermLevel(t *testing.T) {
},
},
},
{
"string_terms",
StringTerms("user").Values([]string{"1", "2"}...).Boost(1.3),
map[string]interface{}{
"terms": map[string]interface{}{
"user": []string{"1", "2"},
"boost": 1.3,
},
},
},
{
"terms_set",
TermsSet("programming_languages", "go", "rust", "COBOL").MinimumShouldMatchField("required_matches"),
Expand Down

0 comments on commit bd6436a

Please sign in to comment.