Skip to content

Commit

Permalink
Merge pull request #21 from Trendyol/feature/missing_match_parameters
Browse files Browse the repository at this point in the history
feature: Add remaining `Match Query` parameters
  • Loading branch information
GokselKUCUKSAHIN authored Nov 10, 2024
2 parents d57a697 + b47d584 commit 4aa4ccc
Show file tree
Hide file tree
Showing 7 changed files with 464 additions and 15 deletions.
2 changes: 1 addition & 1 deletion es/enums/match/operator/operator.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ package operator
//
// Example usage:
//
// match := Match("field", "value").Operator(Operator.And)
// match := es.Match("field", "value").Operator(Operator.And)
// // match now has the "operator" field set to "and" in the matchType object.
//
// Parameters:
Expand Down
5 changes: 3 additions & 2 deletions es/enums/match/operator/operator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,12 @@ package operator_test
import (
"testing"

Operator "github.com/Trendyol/es-query-builder/es/enums/match/operator"
"github.com/Trendyol/es-query-builder/test/assert"

Operator "github.com/Trendyol/es-query-builder/es/enums/match/operator"
)

func Test_ScoreModeString(t *testing.T) {
func Test_OperatorString(t *testing.T) {
tests := []struct {
operator Operator.Operator
result string
Expand Down
33 changes: 33 additions & 0 deletions es/enums/match/zero-terms-query/zero_terms_query.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package zerotermsquery

// ZeroTermsQuery sets the "zero_terms_query" field in the matchType object.
//
// This method specifies the behavior of the match query when no terms remain after analysis,
// such as when all terms are stop words. It updates the matchType object to include the provided
// zeroTermsQuery value, which determines how the query should respond in this scenario.
//
// Example usage:
//
// match := es.Match("field", "value").ZeroTermsQuery(ZeroTermsQuery.All)
// // match now has the "zero_terms_query" field set to "all" in the matchType object.
//
// Parameters:
// - zeroTermsQuery: A ZeroTermsQuery value indicating the behavior for zero-term queries.
// It can be either ZeroTermsQuery.All or ZeroTermsQuery.None.
//
// Returns:
//
// The updated matchType object with the "zero_terms_query" field set to the specified value.
type ZeroTermsQuery string

const (
// All indicates that all documents should be matched when no terms remain.
All ZeroTermsQuery = "all"

// None indicates that no documents should be matched when no terms remain.
None ZeroTermsQuery = "none"
)

func (zeroTermsQuery ZeroTermsQuery) String() string {
return string(zeroTermsQuery)
}
25 changes: 25 additions & 0 deletions es/enums/match/zero-terms-query/zero_terms_query_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package zerotermsquery_test

import (
"testing"

"github.com/Trendyol/es-query-builder/test/assert"

ZeroTermsQuery "github.com/Trendyol/es-query-builder/es/enums/match/zero-terms-query"
)

func Test_ZeroTermsQueryString(t *testing.T) {
tests := []struct {
zeroTermsQuery ZeroTermsQuery.ZeroTermsQuery
result string
}{
{ZeroTermsQuery.All, "all"},
{ZeroTermsQuery.None, "none"},
}

for _, test := range tests {
t.Run(test.result, func(t *testing.T) {
assert.Equal(t, test.result, test.zeroTermsQuery.String())
})
}
}
3 changes: 1 addition & 2 deletions es/match_all_query.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,7 @@ func (m matchAllType) putInTheField(key string, value any) matchAllType {
// Boost sets the "boost" field in the match_all query.
//
// This method configures the match_all query to use a specified boost factor, which influences
// the relevance scoring of the matched documents. It calls putInTheField to add or update
// the "boost" key in the match_all query object.
// the relevance scoring of the matched documents.
//
// Example usage:
//
Expand Down
188 changes: 180 additions & 8 deletions es/match_query.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package es

import Operator "github.com/Trendyol/es-query-builder/es/enums/match/operator"
import (
Operator "github.com/Trendyol/es-query-builder/es/enums/match/operator"
ZeroTermsQuery "github.com/Trendyol/es-query-builder/es/enums/match/zero-terms-query"
)

type matchType Object

Expand All @@ -12,7 +15,7 @@ type matchType Object
//
// Example usage:
//
// m := Match("title", "es-query-builder")
// m := es.Match("title", "es-query-builder")
// // m now contains a matchType object that matches the query "es-query-builder" in the "title" field.
//
// Parameters:
Expand Down Expand Up @@ -47,12 +50,11 @@ func (m matchType) putInTheField(key string, value any) matchType {
// Operator sets the "operator" field in the match query.
//
// This method configures the match query to use a specified operator (e.g., "AND" or "OR")
// for the matching process. It calls putInTheField to add or update the "operator" key
// in the match query object.
// for the matching process.
//
// Example usage:
//
// m := Match("title", "es-query-builder").Operator("AND")
// m := es.Match("title", "es-query-builder").Operator("AND")
// // m now has an "operator" field set to "AND" in the match query object.
//
// Parameters:
Expand All @@ -68,12 +70,11 @@ func (m matchType) Operator(operator Operator.Operator) matchType {
// Boost sets the "boost" field in the match query.
//
// This method configures the match query to use a specified boost factor, which influences
// the relevance scoring of the matched documents. It calls putInTheField to add or update
// the "boost" key in the match query object.
// the relevance scoring of the matched documents.
//
// Example usage:
//
// m := Match("title", "es-query-builder").Boost(1.5)
// m := es.Match("title", "es-query-builder").Boost(1.5)
// // m now has a "boost" field set to 1.5 in the match query object.
//
// Parameters:
Expand All @@ -85,3 +86,174 @@ func (m matchType) Operator(operator Operator.Operator) matchType {
func (m matchType) Boost(boost float64) matchType {
return m.putInTheField("boost", boost)
}

// CutoffFrequency sets the "cutoff_frequency" field in the match query.
//
// This method configures the match query to use a specified cutoff frequency, which is useful
// for controlling how often terms should appear in the document for it to be considered a match.
// A lower cutoff frequency increases precision, while a higher one allows more terms to be matched.
//
// Example usage:
//
// m := es.Match("title", "es-query-builder").CutoffFrequency(0.001)
// // m now has a "cutoff_frequency" field set to 0.001 in the match query object.
//
// Parameters:
// - cutoffFrequency: A float64 value representing the cutoff frequency threshold to be used in the match query.
//
// Returns:
//
// The updated matchType object with the "cutoff_frequency" field set to the specified value.
func (m matchType) CutoffFrequency(cutoffFrequency float64) matchType {
return m.putInTheField("cutoff_frequency", cutoffFrequency)
}

// Fuzziness sets the "fuzziness" field in the match query.
//
// This method configures the match query to use a specified fuzziness level, which determines
// the allowed edit distance (e.g., number of character changes) for a term to be considered a match.
// Common values include "AUTO", or integers representing the number of edits (e.g., 1 or 2).
//
// Example usage:
//
// m := es.Match("title", "es-query-builder").Fuzziness("AUTO")
// // m now has a "fuzziness" field set to "AUTO" in the match query object.
//
// Parameters:
// - fuzziness: A value of any type (typically a string or integer) representing the fuzziness level to be applied to the match query.
//
// Returns:
//
// The updated matchType object with the "fuzziness" field set to the specified value.
func (m matchType) Fuzziness(fuzziness any) matchType {
return m.putInTheField("fuzziness", fuzziness)
}

// FuzzyRewrite sets the "fuzzy_rewrite" field in the match query.
//
// This method configures the match query to use a specified fuzzy rewrite method,
// which controls how multi-term queries are rewritten. Common values include "constant_score",
// "scoring_boolean", and other rewrite options that influence the scoring and performance of
// fuzzy matching.
//
// Example usage:
//
// m := es.Match("title", "es-query-builder").FuzzyRewrite("constant_score")
// // m now has a "fuzzy_rewrite" field set to "constant_score" in the match query object.
//
// Parameters:
// - fuzzyRewrite: A string value representing the rewrite method to be used for fuzzy matching in the match query.
//
// Returns:
//
// The updated matchType object with the "fuzzy_rewrite" field set to the specified value.
func (m matchType) FuzzyRewrite(fuzzyRewrite string) matchType {
return m.putInTheField("fuzzy_rewrite", fuzzyRewrite)
}

// FuzzyTranspositions sets the "fuzzy_transpositions" field in the match query.
//
// This method configures the match query to allow or disallow transpositions (e.g., swapping two adjacent characters)
// when performing fuzzy matching. Setting this field to true enables transpositions, which can increase the match rate
// for terms with common typos or character swaps.
//
// Example usage:
//
// m := es.Match("title", "es-query-builder").FuzzyTranspositions(true)
// // m now has a "fuzzy_transpositions" field set to true in the match query object.
//
// Parameters:
// - fuzzyTranspositions: A boolean value indicating whether transpositions should be allowed in fuzzy matching.
//
// Returns:
//
// The updated matchType object with the "fuzzy_transpositions" field set to the specified value.
func (m matchType) FuzzyTranspositions(fuzzyTranspositions bool) matchType {
return m.putInTheField("fuzzy_transpositions", fuzzyTranspositions)
}

// Lenient sets the "lenient" field in the match query.
//
// This method configures the match query to use lenient parsing, allowing it to skip errors
// for data type mismatches. When set to true, documents that contain mismatched data types
// (e.g., text in a numeric field) will not cause errors and will be ignored instead.
//
// Example usage:
//
// m := es.Match("title", "es-query-builder").Lenient(true)
// // m now has a "lenient" field set to true in the match query object.
//
// Parameters:
// - lenient: A boolean value indicating whether lenient parsing should be enabled.
//
// Returns:
//
// The updated matchType object with the "lenient" field set to the specified value.
func (m matchType) Lenient(lenient bool) matchType {
return m.putInTheField("lenient", lenient)
}

// MaxExpansions sets the "max_expansions" field in the match query.
//
// This method configures the match query to limit the maximum number of terms that can be expanded
// for multi-term queries, such as those involving fuzzy matching. Higher values allow more terms to
// be considered, but may impact performance.
//
// Example usage:
//
// m := es.Match("title", "es-query-builder").MaxExpansions(50)
// // m now has a "max_expansions" field set to 50 in the match query object.
//
// Parameters:
// - maxExpansions: An integer representing the maximum number of term expansions to be allowed in the match query.
//
// Returns:
//
// The updated matchType object with the "max_expansions" field set to the specified value.
func (m matchType) MaxExpansions(maxExpansions int) matchType {
return m.putInTheField("max_expansions", maxExpansions)
}

// PrefixLength sets the "prefix_length" field in the match query.
//
// This method configures the match query to specify a minimum prefix length for fuzzy matching,
// which defines the number of initial characters in a term that must match exactly before
// considering fuzziness. Increasing this value can improve performance by reducing the number
// of fuzzy matches, but may also limit the flexibility of the query.
//
// Example usage:
//
// m := es.Match("title", "es-query-builder").PrefixLength(2)
// // m now has a "prefix_length" field set to 2 in the match query object.
//
// Parameters:
// - prefixLength: An integer representing the number of initial characters that must match exactly in fuzzy matching.
//
// Returns:
//
// The updated matchType object with the "prefix_length" field set to the specified value.
func (m matchType) PrefixLength(prefixLength int) matchType {
return m.putInTheField("prefix_length", prefixLength)
}

// ZeroTermsQuery sets the "zero_terms_query" field in the match query.
//
// This method configures the behavior of the match query when no terms remain after analysis
// (for example, if all terms are stop words). The specified zero_terms_query value determines
// how to handle this scenario, with options like "all" to match all documents or "none" to
// match none.
//
// Example usage:
//
// m := es.Match("title", "es-query-builder").ZeroTermsQuery(zerotermsquery.All)
// // m now has a "zero_terms_query" field set to "all" in the match query object.
//
// Parameters:
// - zeroTermsQuery: A zerotermsquery.ZeroTermsQuery value that specifies the behavior for zero-term queries.
//
// Returns:
//
// The updated matchType object with the "zero_terms_query" field set to the specified value.
func (m matchType) ZeroTermsQuery(zeroTermsQuery ZeroTermsQuery.ZeroTermsQuery) matchType {
return m.putInTheField("zero_terms_query", zeroTermsQuery)
}
Loading

0 comments on commit 4aa4ccc

Please sign in to comment.