Skip to content

Commit

Permalink
Extend MatchQuery parameters
Browse files Browse the repository at this point in the history
  • Loading branch information
Leonid Moguchev authored and Leonid Moguchev committed Jan 14, 2024
1 parent a852853 commit b4ff80f
Show file tree
Hide file tree
Showing 2 changed files with 81 additions and 3 deletions.
64 changes: 61 additions & 3 deletions objects/q_match_query.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,10 @@ import (
)

type MatchQueryS struct {
Field string `json:"-"` //(Required, object) Field you wish to search.
Query string `json:"query"` //(Required) Text, number, boolean value or date you wish to find in the provided <field>.
Field string `json:"-"` // (Required, object) Field you wish to search.
Query string `json:"query"` // (Required) Text, number, boolean value or date you wish to find in the provided <field>.
Operator string `json:"operator,omitempty"` // (Optional, string) Boolean logic used to interpret text in the query value.
Fuzziness string `json:"fuzziness,omitempty"` // (Optional, string) Maximum edit distance allowed for matching.
}

func (mq MatchQueryS) QueryInfo() string {
Expand All @@ -24,13 +26,69 @@ func (mq MatchQueryS) MarshalJSON() ([]byte, error) {
)
}

type MatchOperator string

const (
MatchOperatorOR MatchOperator = "OR"
MatchOperatorAND MatchOperator = "AND"
)

type Fuzziness string

const (
FuzzinessAUTO Fuzziness = "AUTO"
)

// https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-match-query.html#match-field-params
type matchQueryFieldParameters struct {
/*
(Optional, string) Boolean logic used to interpret text in the query value. Valid values are:
* OR (Default)
* For example, a query value of capital of Hungary is interpreted as capital OR of OR Hungary.
* AND
* For example, a query value of capital of Hungary is interpreted as capital AND of AND Hungary.
*/
Operator MatchOperator

/*
(Optional, string) Maximum edit distance allowed for matching. See [Fuzziness](https://www.elastic.co/guide/en/elasticsearch/reference/current/common-options.html#fuzziness) for valid values and more information.
*/
Fuzziness Fuzziness
}

type MatchQueryFieldParameter func(prms *matchQueryFieldParameters)

// WithMatchOperator ...
func WithMatchOperator(op MatchOperator) MatchQueryFieldParameter {
return func(prms *matchQueryFieldParameters) {
prms.Operator = op
}
}

// WithFuzzinessParameter ...
func WithFuzzinessParameter(f Fuzziness) MatchQueryFieldParameter {
return func(prms *matchQueryFieldParameters) {
prms.Fuzziness = f
}
}

// Returns documents that match a provided text, number, date or boolean value. The provided text is analyzed before matching.
// [Match query]: https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-match-query.html
func MatchQuery(field string, query string) QueryResult {
func MatchQuery(field string, query string, prms ...MatchQueryFieldParameter) QueryResult {
matchQuery := MatchQueryS{
Field: field,
Query: query,
}

var parameters matchQueryFieldParameters
for _, prm := range prms {
prm(&parameters)
}

matchQuery.Operator = string(parameters.Operator)
matchQuery.Fuzziness = string(parameters.Fuzziness)

return QueryResult{
Ok: matchQuery,
Err: nil,
Expand Down
20 changes: 20 additions & 0 deletions objects/q_match_query_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package objects

import (
"testing"

"github.com/stretchr/testify/require"
)

func Test_MatchQueryS_MarshalJSON(t *testing.T) {
q := MatchQuery("field_name", "some match query",
WithMatchOperator(MatchOperatorAND),
WithFuzzinessParameter(FuzzinessAUTO),
)

body, err := q.Ok.MarshalJSON()
require.NoError(t, err)

const expected = `{"match":{"field_name":{"query":"some match query","operator":"AND","fuzziness":"AUTO"}}}`
require.Equal(t, expected, string(body))
}

0 comments on commit b4ff80f

Please sign in to comment.