Skip to content

Commit 4aa4ccc

Browse files
Merge pull request #21 from Trendyol/feature/missing_match_parameters
feature: Add remaining `Match Query` parameters
2 parents d57a697 + b47d584 commit 4aa4ccc

File tree

7 files changed

+464
-15
lines changed

7 files changed

+464
-15
lines changed

es/enums/match/operator/operator.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ package operator
77
//
88
// Example usage:
99
//
10-
// match := Match("field", "value").Operator(Operator.And)
10+
// match := es.Match("field", "value").Operator(Operator.And)
1111
// // match now has the "operator" field set to "and" in the matchType object.
1212
//
1313
// Parameters:

es/enums/match/operator/operator_test.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,12 @@ package operator_test
33
import (
44
"testing"
55

6-
Operator "github.com/Trendyol/es-query-builder/es/enums/match/operator"
76
"github.com/Trendyol/es-query-builder/test/assert"
7+
8+
Operator "github.com/Trendyol/es-query-builder/es/enums/match/operator"
89
)
910

10-
func Test_ScoreModeString(t *testing.T) {
11+
func Test_OperatorString(t *testing.T) {
1112
tests := []struct {
1213
operator Operator.Operator
1314
result string
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package zerotermsquery
2+
3+
// ZeroTermsQuery sets the "zero_terms_query" field in the matchType object.
4+
//
5+
// This method specifies the behavior of the match query when no terms remain after analysis,
6+
// such as when all terms are stop words. It updates the matchType object to include the provided
7+
// zeroTermsQuery value, which determines how the query should respond in this scenario.
8+
//
9+
// Example usage:
10+
//
11+
// match := es.Match("field", "value").ZeroTermsQuery(ZeroTermsQuery.All)
12+
// // match now has the "zero_terms_query" field set to "all" in the matchType object.
13+
//
14+
// Parameters:
15+
// - zeroTermsQuery: A ZeroTermsQuery value indicating the behavior for zero-term queries.
16+
// It can be either ZeroTermsQuery.All or ZeroTermsQuery.None.
17+
//
18+
// Returns:
19+
//
20+
// The updated matchType object with the "zero_terms_query" field set to the specified value.
21+
type ZeroTermsQuery string
22+
23+
const (
24+
// All indicates that all documents should be matched when no terms remain.
25+
All ZeroTermsQuery = "all"
26+
27+
// None indicates that no documents should be matched when no terms remain.
28+
None ZeroTermsQuery = "none"
29+
)
30+
31+
func (zeroTermsQuery ZeroTermsQuery) String() string {
32+
return string(zeroTermsQuery)
33+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package zerotermsquery_test
2+
3+
import (
4+
"testing"
5+
6+
"github.com/Trendyol/es-query-builder/test/assert"
7+
8+
ZeroTermsQuery "github.com/Trendyol/es-query-builder/es/enums/match/zero-terms-query"
9+
)
10+
11+
func Test_ZeroTermsQueryString(t *testing.T) {
12+
tests := []struct {
13+
zeroTermsQuery ZeroTermsQuery.ZeroTermsQuery
14+
result string
15+
}{
16+
{ZeroTermsQuery.All, "all"},
17+
{ZeroTermsQuery.None, "none"},
18+
}
19+
20+
for _, test := range tests {
21+
t.Run(test.result, func(t *testing.T) {
22+
assert.Equal(t, test.result, test.zeroTermsQuery.String())
23+
})
24+
}
25+
}

es/match_all_query.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,7 @@ func (m matchAllType) putInTheField(key string, value any) matchAllType {
3232
// Boost sets the "boost" field in the match_all query.
3333
//
3434
// This method configures the match_all query to use a specified boost factor, which influences
35-
// the relevance scoring of the matched documents. It calls putInTheField to add or update
36-
// the "boost" key in the match_all query object.
35+
// the relevance scoring of the matched documents.
3736
//
3837
// Example usage:
3938
//

es/match_query.go

Lines changed: 180 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
package es
22

3-
import Operator "github.com/Trendyol/es-query-builder/es/enums/match/operator"
3+
import (
4+
Operator "github.com/Trendyol/es-query-builder/es/enums/match/operator"
5+
ZeroTermsQuery "github.com/Trendyol/es-query-builder/es/enums/match/zero-terms-query"
6+
)
47

58
type matchType Object
69

@@ -12,7 +15,7 @@ type matchType Object
1215
//
1316
// Example usage:
1417
//
15-
// m := Match("title", "es-query-builder")
18+
// m := es.Match("title", "es-query-builder")
1619
// // m now contains a matchType object that matches the query "es-query-builder" in the "title" field.
1720
//
1821
// Parameters:
@@ -47,12 +50,11 @@ func (m matchType) putInTheField(key string, value any) matchType {
4750
// Operator sets the "operator" field in the match query.
4851
//
4952
// This method configures the match query to use a specified operator (e.g., "AND" or "OR")
50-
// for the matching process. It calls putInTheField to add or update the "operator" key
51-
// in the match query object.
53+
// for the matching process.
5254
//
5355
// Example usage:
5456
//
55-
// m := Match("title", "es-query-builder").Operator("AND")
57+
// m := es.Match("title", "es-query-builder").Operator("AND")
5658
// // m now has an "operator" field set to "AND" in the match query object.
5759
//
5860
// Parameters:
@@ -68,12 +70,11 @@ func (m matchType) Operator(operator Operator.Operator) matchType {
6870
// Boost sets the "boost" field in the match query.
6971
//
7072
// This method configures the match query to use a specified boost factor, which influences
71-
// the relevance scoring of the matched documents. It calls putInTheField to add or update
72-
// the "boost" key in the match query object.
73+
// the relevance scoring of the matched documents.
7374
//
7475
// Example usage:
7576
//
76-
// m := Match("title", "es-query-builder").Boost(1.5)
77+
// m := es.Match("title", "es-query-builder").Boost(1.5)
7778
// // m now has a "boost" field set to 1.5 in the match query object.
7879
//
7980
// Parameters:
@@ -85,3 +86,174 @@ func (m matchType) Operator(operator Operator.Operator) matchType {
8586
func (m matchType) Boost(boost float64) matchType {
8687
return m.putInTheField("boost", boost)
8788
}
89+
90+
// CutoffFrequency sets the "cutoff_frequency" field in the match query.
91+
//
92+
// This method configures the match query to use a specified cutoff frequency, which is useful
93+
// for controlling how often terms should appear in the document for it to be considered a match.
94+
// A lower cutoff frequency increases precision, while a higher one allows more terms to be matched.
95+
//
96+
// Example usage:
97+
//
98+
// m := es.Match("title", "es-query-builder").CutoffFrequency(0.001)
99+
// // m now has a "cutoff_frequency" field set to 0.001 in the match query object.
100+
//
101+
// Parameters:
102+
// - cutoffFrequency: A float64 value representing the cutoff frequency threshold to be used in the match query.
103+
//
104+
// Returns:
105+
//
106+
// The updated matchType object with the "cutoff_frequency" field set to the specified value.
107+
func (m matchType) CutoffFrequency(cutoffFrequency float64) matchType {
108+
return m.putInTheField("cutoff_frequency", cutoffFrequency)
109+
}
110+
111+
// Fuzziness sets the "fuzziness" field in the match query.
112+
//
113+
// This method configures the match query to use a specified fuzziness level, which determines
114+
// the allowed edit distance (e.g., number of character changes) for a term to be considered a match.
115+
// Common values include "AUTO", or integers representing the number of edits (e.g., 1 or 2).
116+
//
117+
// Example usage:
118+
//
119+
// m := es.Match("title", "es-query-builder").Fuzziness("AUTO")
120+
// // m now has a "fuzziness" field set to "AUTO" in the match query object.
121+
//
122+
// Parameters:
123+
// - fuzziness: A value of any type (typically a string or integer) representing the fuzziness level to be applied to the match query.
124+
//
125+
// Returns:
126+
//
127+
// The updated matchType object with the "fuzziness" field set to the specified value.
128+
func (m matchType) Fuzziness(fuzziness any) matchType {
129+
return m.putInTheField("fuzziness", fuzziness)
130+
}
131+
132+
// FuzzyRewrite sets the "fuzzy_rewrite" field in the match query.
133+
//
134+
// This method configures the match query to use a specified fuzzy rewrite method,
135+
// which controls how multi-term queries are rewritten. Common values include "constant_score",
136+
// "scoring_boolean", and other rewrite options that influence the scoring and performance of
137+
// fuzzy matching.
138+
//
139+
// Example usage:
140+
//
141+
// m := es.Match("title", "es-query-builder").FuzzyRewrite("constant_score")
142+
// // m now has a "fuzzy_rewrite" field set to "constant_score" in the match query object.
143+
//
144+
// Parameters:
145+
// - fuzzyRewrite: A string value representing the rewrite method to be used for fuzzy matching in the match query.
146+
//
147+
// Returns:
148+
//
149+
// The updated matchType object with the "fuzzy_rewrite" field set to the specified value.
150+
func (m matchType) FuzzyRewrite(fuzzyRewrite string) matchType {
151+
return m.putInTheField("fuzzy_rewrite", fuzzyRewrite)
152+
}
153+
154+
// FuzzyTranspositions sets the "fuzzy_transpositions" field in the match query.
155+
//
156+
// This method configures the match query to allow or disallow transpositions (e.g., swapping two adjacent characters)
157+
// when performing fuzzy matching. Setting this field to true enables transpositions, which can increase the match rate
158+
// for terms with common typos or character swaps.
159+
//
160+
// Example usage:
161+
//
162+
// m := es.Match("title", "es-query-builder").FuzzyTranspositions(true)
163+
// // m now has a "fuzzy_transpositions" field set to true in the match query object.
164+
//
165+
// Parameters:
166+
// - fuzzyTranspositions: A boolean value indicating whether transpositions should be allowed in fuzzy matching.
167+
//
168+
// Returns:
169+
//
170+
// The updated matchType object with the "fuzzy_transpositions" field set to the specified value.
171+
func (m matchType) FuzzyTranspositions(fuzzyTranspositions bool) matchType {
172+
return m.putInTheField("fuzzy_transpositions", fuzzyTranspositions)
173+
}
174+
175+
// Lenient sets the "lenient" field in the match query.
176+
//
177+
// This method configures the match query to use lenient parsing, allowing it to skip errors
178+
// for data type mismatches. When set to true, documents that contain mismatched data types
179+
// (e.g., text in a numeric field) will not cause errors and will be ignored instead.
180+
//
181+
// Example usage:
182+
//
183+
// m := es.Match("title", "es-query-builder").Lenient(true)
184+
// // m now has a "lenient" field set to true in the match query object.
185+
//
186+
// Parameters:
187+
// - lenient: A boolean value indicating whether lenient parsing should be enabled.
188+
//
189+
// Returns:
190+
//
191+
// The updated matchType object with the "lenient" field set to the specified value.
192+
func (m matchType) Lenient(lenient bool) matchType {
193+
return m.putInTheField("lenient", lenient)
194+
}
195+
196+
// MaxExpansions sets the "max_expansions" field in the match query.
197+
//
198+
// This method configures the match query to limit the maximum number of terms that can be expanded
199+
// for multi-term queries, such as those involving fuzzy matching. Higher values allow more terms to
200+
// be considered, but may impact performance.
201+
//
202+
// Example usage:
203+
//
204+
// m := es.Match("title", "es-query-builder").MaxExpansions(50)
205+
// // m now has a "max_expansions" field set to 50 in the match query object.
206+
//
207+
// Parameters:
208+
// - maxExpansions: An integer representing the maximum number of term expansions to be allowed in the match query.
209+
//
210+
// Returns:
211+
//
212+
// The updated matchType object with the "max_expansions" field set to the specified value.
213+
func (m matchType) MaxExpansions(maxExpansions int) matchType {
214+
return m.putInTheField("max_expansions", maxExpansions)
215+
}
216+
217+
// PrefixLength sets the "prefix_length" field in the match query.
218+
//
219+
// This method configures the match query to specify a minimum prefix length for fuzzy matching,
220+
// which defines the number of initial characters in a term that must match exactly before
221+
// considering fuzziness. Increasing this value can improve performance by reducing the number
222+
// of fuzzy matches, but may also limit the flexibility of the query.
223+
//
224+
// Example usage:
225+
//
226+
// m := es.Match("title", "es-query-builder").PrefixLength(2)
227+
// // m now has a "prefix_length" field set to 2 in the match query object.
228+
//
229+
// Parameters:
230+
// - prefixLength: An integer representing the number of initial characters that must match exactly in fuzzy matching.
231+
//
232+
// Returns:
233+
//
234+
// The updated matchType object with the "prefix_length" field set to the specified value.
235+
func (m matchType) PrefixLength(prefixLength int) matchType {
236+
return m.putInTheField("prefix_length", prefixLength)
237+
}
238+
239+
// ZeroTermsQuery sets the "zero_terms_query" field in the match query.
240+
//
241+
// This method configures the behavior of the match query when no terms remain after analysis
242+
// (for example, if all terms are stop words). The specified zero_terms_query value determines
243+
// how to handle this scenario, with options like "all" to match all documents or "none" to
244+
// match none.
245+
//
246+
// Example usage:
247+
//
248+
// m := es.Match("title", "es-query-builder").ZeroTermsQuery(zerotermsquery.All)
249+
// // m now has a "zero_terms_query" field set to "all" in the match query object.
250+
//
251+
// Parameters:
252+
// - zeroTermsQuery: A zerotermsquery.ZeroTermsQuery value that specifies the behavior for zero-term queries.
253+
//
254+
// Returns:
255+
//
256+
// The updated matchType object with the "zero_terms_query" field set to the specified value.
257+
func (m matchType) ZeroTermsQuery(zeroTermsQuery ZeroTermsQuery.ZeroTermsQuery) matchType {
258+
return m.putInTheField("zero_terms_query", zeroTermsQuery)
259+
}

0 commit comments

Comments
 (0)