From b4ff80ffb73ec6a1d6237942e7c4f6a914223c0f Mon Sep 17 00:00:00 2001 From: Leonid Moguchev Date: Sun, 14 Jan 2024 12:30:46 +0300 Subject: [PATCH] Extend MatchQuery parameters --- objects/q_match_query.go | 64 +++++++++++++++++++++++++++++++++-- objects/q_match_query_test.go | 20 +++++++++++ 2 files changed, 81 insertions(+), 3 deletions(-) create mode 100644 objects/q_match_query_test.go diff --git a/objects/q_match_query.go b/objects/q_match_query.go index 8516e6f..7a23393 100644 --- a/objects/q_match_query.go +++ b/objects/q_match_query.go @@ -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 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 . + 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 { @@ -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(¶meters) + } + + matchQuery.Operator = string(parameters.Operator) + matchQuery.Fuzziness = string(parameters.Fuzziness) + return QueryResult{ Ok: matchQuery, Err: nil, diff --git a/objects/q_match_query_test.go b/objects/q_match_query_test.go new file mode 100644 index 0000000..c968958 --- /dev/null +++ b/objects/q_match_query_test.go @@ -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)) +}