Skip to content

Commit

Permalink
Merge pull request #5 from GokselKUCUKSAHIN/feature/match_none
Browse files Browse the repository at this point in the history
Feature/match none
  • Loading branch information
GokselKUCUKSAHIN committed Apr 12, 2024
2 parents 2b7baac + 2e876d9 commit 5abaf55
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 1 deletion.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ go get github.com/GokselKUCUKSAHIN/es-query-builder
- [ ] aggs
- [x] match
- [x] match_all
- [ ] match_none
- [x] match_none
- [x] minimum_should_match
- [x] boost

Expand Down
33 changes: 33 additions & 0 deletions es/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ type matchType Object

type matchAllType Object

type matchNoneType Object

type termType Object

type termsType Object
Expand Down Expand Up @@ -307,6 +309,37 @@ func (m matchType) Boost(boost float64) matchType {
return m.putInTheField("boost", boost)
}

func MatchNone[T any](key string, query T) matchNoneType {
return matchNoneType{
"match_none": Object{
key: Object{
"query": query,
},
},
}
}

func (m matchNoneType) putInTheField(key string, value any) matchNoneType {
if matchNone, exists := m["match_none"]; exists {
if matchNoneObject, mnoOk := matchNone.(Object); mnoOk {
for field := range matchNoneObject {
if fieldObject, foOk := matchNoneObject[field].(Object); foOk {
fieldObject[key] = value
}
}
}
}
return m
}

func (m matchNoneType) Operator(operator Operator.Operator) matchNoneType {
return m.putInTheField("operator", operator)
}

func (m matchNoneType) Boost(boost float64) matchNoneType {
return m.putInTheField("boost", boost)
}

func MatchAll() matchAllType {
return matchAllType{
"match_all": Object{},
Expand Down
31 changes: 31 additions & 0 deletions es/types_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -774,6 +774,37 @@ func Test_Match_should_create_json_with_match_field_inside_query(t *testing.T) {
"{\"query\":{\"match\":{\"message\":{\"boost\":2.14,\"operator\":\"or\",\"query\":\"this is a test\"}}}}", bodyJSON)
}

//// MatchNone ////

func Test_MatchNone_should_exist_on_es_package(t *testing.T) {
// Given When Then
assert.NotNil(t, es.MatchNone[any])
}

func Test_MatchNone_method_should_create_matchNoneType(t *testing.T) {
// Given
b := es.MatchNone("key", "value")

// Then
assert.NotNil(t, b)
assert.IsTypeString(t, "es.matchNoneType", b)
}

func Test_MatchNone_should_create_json_with_match_none_field_inside_query(t *testing.T) {
// Given
query := es.NewQuery(
es.MatchNone("fooBar", "lorem ipsum").
Boost(6.19).
Operator(Operator.And),
)

// When Then
assert.NotNil(t, query)
bodyJSON := assert.MarshalWithoutError(t, query)
assert.Equal(t,
"{\"query\":{\"match_none\":{\"fooBar\":{\"boost\":6.19,\"operator\":\"and\",\"query\":\"lorem ipsum\"}}}}", bodyJSON)
}

//// MatchAll ////

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

0 comments on commit 5abaf55

Please sign in to comment.