Skip to content

Commit

Permalink
feature: add escape, type, fuzzy_rewrite, tie_breaker to queryStringQ…
Browse files Browse the repository at this point in the history
…uery
  • Loading branch information
GokselKUCUKSAHIN committed Dec 28, 2024
1 parent d56b187 commit d1581ac
Show file tree
Hide file tree
Showing 16 changed files with 368 additions and 145 deletions.
20 changes: 10 additions & 10 deletions es/aggregations.go
Original file line number Diff line number Diff line change
Expand Up @@ -179,16 +179,6 @@ func AggCustom(agg Object) aggsType {
return aggsType(agg)
}

func (agg aggsType) putInTheField(key string, value any) aggsType {
for _, fieldObj := range agg {
if fieldObject, ok := fieldObj.(Object); ok {
fieldObject[key] = value
break
}
}
return agg
}

// Aggs adds a nested aggregation to the aggsType object.
//
// This method adds a nested aggregation under the "aggs" field with the given name.
Expand Down Expand Up @@ -386,3 +376,13 @@ func (o Object) Aggs(name string, agg aggsType) Object {
o["aggs"] = aggs
return o
}

func (agg aggsType) putInTheField(key string, value any) aggsType {
for _, fieldObj := range agg {
if fieldObject, ok := fieldObj.(Object); ok {
fieldObject[key] = value
break
}
}
return agg
}
20 changes: 10 additions & 10 deletions es/base_query.go
Original file line number Diff line number Diff line change
Expand Up @@ -223,16 +223,6 @@ func Sort(field string) sortType {
}
}

func (s sortType) putInTheField(key string, value any) sortType {
for _, fieldObj := range s {
if fieldObject, ok := fieldObj.(Object); ok {
fieldObject[key] = value
break
}
}
return s
}

// Order sets the "order" parameter in a sortType object.
//
// This method specifies the order in which the results should be sorted.
Expand Down Expand Up @@ -295,3 +285,13 @@ func (o Object) Sort(sorts ...sortType) Object {
o["sort"] = sorts
return o
}

func (s sortType) putInTheField(key string, value any) sortType {
for _, fieldObj := range s {
if fieldObject, ok := fieldObj.(Object); ok {
fieldObject[key] = value
break
}
}
return s
}
33 changes: 14 additions & 19 deletions es/enums/range-relation/range_relation.go
Original file line number Diff line number Diff line change
@@ -1,35 +1,30 @@
// Package rangerelation provides utilities for working with range relations.
package rangerelation

// RangeRelation represents the relationship between two ranges in a query.
//
// RangeRelation is a string type used to specify the spatial or logical relationship
// between two ranges, such as whether one range is entirely within another,
// contains another, or intersects with another.
// RangeRelation is a string type that specifies how a range interacts with another range.
// It is commonly used in range queries, especially in geospatial or numeric data contexts.
//
// Constants:
// - Within: Specifies that the range is entirely within another range.
// - Contains: Specifies that the range entirely contains another range.
// - Intersects: Specifies that the range overlaps or intersects with another range.
//
// Example usage:
//
// var relation RangeRelation = Within
//
// // Use relation in a query configuration
//
// Constants:
// - Within: Indicates that one range is entirely within another.
// - Contains: Indicates that one range entirely contains another.
// - Intersects: Indicates that the ranges overlap or intersect at least partially.
package rangerelation

// RangeRelation represents the relationship between two ranges.
//
// RangeRelation is a string type that specifies the spatial or logical relationship
// between ranges, which is commonly used in geospatial queries or similar scenarios.
// // Use relation in a range query configuration.
type RangeRelation string

const (
// Within indicates that one range is entirely within another.
// Within specifies that the range is entirely within another range.
Within RangeRelation = "within"

// Contains indicates that one range entirely contains another.
// Contains specifies that the range entirely contains another range.
Contains RangeRelation = "contains"

// Intersects indicates that the ranges overlap or intersect at least partially.
// Intersects specifies that the range overlaps or intersects with another range.
Intersects RangeRelation = "intersects"
)

Expand Down
46 changes: 46 additions & 0 deletions es/enums/text-query-type/text_query_type.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package textquerytype

// TextQueryType represents the type of text query to use in query string queries.
//
// TextQueryType is a string type that defines how the query text is analyzed and matched
// against fields in the search index. It allows specifying various query strategies
// based on the desired behavior.
//
// Constants:
// - Bestfields: Use the best matching fields for scoring.
// - Mostfields: Combine scores from all matching fields.
// - Crossfields: Treat matching fields as a single field.
// - Phrase: Match terms as a phrase.
// - Phraseprefix: Match terms as a phrase with a prefix.
// - Boolprefix: Use a boolean query with prefix matching.
//
// Example usage:
//
// var queryType TextQueryType = Bestfields
//
// // Use queryType in a query string configuration.
type TextQueryType string

const (
// Bestfields indicates that the best matching fields should be used for scoring.
Bestfields TextQueryType = "best_fields"

// Mostfields indicates that the scores from all matching fields should be combined.
Mostfields TextQueryType = "most_fields"

// Crossfields indicates that matching fields should be treated as a single field.
Crossfields TextQueryType = "cross_fields"

// Phrase indicates that terms should be matched as a phrase.
Phrase TextQueryType = "phrase"

// Phraseprefix indicates that terms should be matched as a phrase with a prefix.
Phraseprefix TextQueryType = "phrase_prefix"

// Boolprefix indicates that a boolean query with prefix matching should be used.
Boolprefix TextQueryType = "bool_prefix"
)

func (textQueryType TextQueryType) String() string {
return string(textQueryType)
}
29 changes: 29 additions & 0 deletions es/enums/text-query-type/text_query_type_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package textquerytype_test

import (
"testing"

TextQueryType "github.com/Trendyol/es-query-builder/es/enums/text-query-type"

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

func Test_TextQueryTypeString(t *testing.T) {
tests := []struct {
textQueryType TextQueryType.TextQueryType
result string
}{
{TextQueryType.Bestfields, "best_fields"},
{TextQueryType.Mostfields, "most_fields"},
{TextQueryType.Crossfields, "cross_fields"},
{TextQueryType.Phrase, "phrase"},
{TextQueryType.Phraseprefix, "phrase_prefix"},
{TextQueryType.Boolprefix, "bool_prefix"},
}

for _, test := range tests {
t.Run(test.result, func(t *testing.T) {
assert.Equal(t, test.result, test.textQueryType.String())
})
}
}
14 changes: 7 additions & 7 deletions es/exists_query.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,6 @@ func Exists(key string) existsType {
}
}

func (e existsType) putInTheField(key string, value any) existsType {
if exists, ok := e["exists"].(Object); ok {
exists[key] = value
}
return e
}

// Boost sets the "boost" parameter in an existsType query.
//
// This method allows you to specify a boost factor for the exists query,
Expand Down Expand Up @@ -108,3 +101,10 @@ func ExistsIf(key string, condition bool) existsType {
}
return Exists(key)
}

func (e existsType) putInTheField(key string, value any) existsType {
if exists, ok := e["exists"].(Object); ok {
exists[key] = value
}
return e
}
14 changes: 7 additions & 7 deletions es/match_all_query.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,6 @@ func MatchAll() matchAllType {
}
}

func (m matchAllType) putInTheField(key string, value any) matchAllType {
if matchAll, ok := m["match_all"].(Object); ok {
matchAll[key] = value
}
return m
}

// 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
Expand All @@ -48,3 +41,10 @@ func (m matchAllType) putInTheField(key string, value any) matchAllType {
func (m matchAllType) Boost(boost float64) matchAllType {
return m.putInTheField("boost", boost)
}

func (m matchAllType) putInTheField(key string, value any) matchAllType {
if matchAll, ok := m["match_all"].(Object); ok {
matchAll[key] = value
}
return m
}
24 changes: 12 additions & 12 deletions es/match_none_query.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,18 +30,6 @@ func MatchNone[T any](key string, query T) matchNoneType {
}
}

func (m matchNoneType) putInTheField(key string, value any) matchNoneType {
if matchNone, ok := m["match_none"].(Object); ok {
for _, fieldObj := range matchNone {
if fieldObject, foOk := fieldObj.(Object); foOk {
fieldObject[key] = value
break
}
}
}
return m
}

// Boost sets the "boost" field in the match_none query.
//
// This method configures the match_none query to use a specified boost factor, which influences
Expand All @@ -62,3 +50,15 @@ func (m matchNoneType) putInTheField(key string, value any) matchNoneType {
func (m matchNoneType) Boost(boost float64) matchNoneType {
return m.putInTheField("boost", boost)
}

func (m matchNoneType) putInTheField(key string, value any) matchNoneType {
if matchNone, ok := m["match_none"].(Object); ok {
for _, fieldObj := range matchNone {
if fieldObject, foOk := fieldObj.(Object); foOk {
fieldObject[key] = value
break
}
}
}
return m
}
24 changes: 12 additions & 12 deletions es/match_query.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,18 +35,6 @@ func Match[T any](key string, query T) matchType {
}
}

func (m matchType) putInTheField(key string, value any) matchType {
if match, ok := m["match"].(Object); ok {
for _, fieldObj := range match {
if fieldObject, foOk := fieldObj.(Object); foOk {
fieldObject[key] = value
break
}
}
}
return m
}

// 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")
Expand Down Expand Up @@ -279,3 +267,15 @@ func (m matchType) AutoGenerateSynonymsPhraseQuery(autoGenerateSynonymsPhraseQue
func (m matchType) ZeroTermsQuery(zeroTermsQuery ZeroTermsQuery.ZeroTermsQuery) matchType {
return m.putInTheField("zero_terms_query", zeroTermsQuery)
}

func (m matchType) putInTheField(key string, value any) matchType {
if match, ok := m["match"].(Object); ok {
for _, fieldObj := range match {
if fieldObject, foOk := fieldObj.(Object); foOk {
fieldObject[key] = value
break
}
}
}
return m
}
14 changes: 7 additions & 7 deletions es/nested_query.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,13 +32,6 @@ func Nested[T any](path string, nestedQuery T) nestedType {
}
}

func (n nestedType) putInNested(key string, value any) nestedType {
if nestedObject, ok := n["nested"].(Object); ok {
nestedObject[key] = value
}
return n
}

// InnerHits sets the "inner_hits" field for the nested query.
//
// This method specifies the inner hits for the nested query, allowing you to control
Expand Down Expand Up @@ -82,3 +75,10 @@ func (n nestedType) InnerHits(innerHits Object) nestedType {
func (n nestedType) ScoreMode(scoreMode ScoreMode.ScoreMode) nestedType {
return n.putInNested("score_mode", scoreMode)
}

func (n nestedType) putInNested(key string, value any) nestedType {
if nestedObject, ok := n["nested"].(Object); ok {
nestedObject[key] = value
}
return n
}
Loading

0 comments on commit d1581ac

Please sign in to comment.