-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathterm_query.go
141 lines (134 loc) · 4.45 KB
/
term_query.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
package es
type termType Object
// Term creates a new es.termType object with the specified key-value pair.
//
// This function initializes an es.termType object with a single term query, where the
// key is the field name and the value is the term to search for. This is typically
// used to construct a term query in search queries.
//
// Example usage:
//
// t := es.Term("category", "books")
// // t now contains an es.termType object with a term query for the "category" field.
//
// Parameters:
// - key: A string representing the field name for the term query.
// - value: The value to be searched for in the specified field. The type is generic.
//
// Returns:
//
// An es.termType object containing the specified term query.
func Term[T any](key string, value T) termType {
return termType{
"term": Object{
key: Object{
"value": value,
},
},
}
}
// CaseInsensitive sets the "case_insensitive" parameter in an es.termType query.
//
// This method allows you to specify whether the term query should be case-
// insensitive. When set to true, the term matching will ignore case,
// allowing for more flexible matches in the query results.
//
// Example usage:
//
// t := es.Term().CaseInsensitive(true)
// // t now includes a "case_insensitive" parameter set to true.
//
// Parameters:
// - caseInsensitive: A boolean value indicating whether the term query
// should be case-insensitive.
//
// Returns:
//
// The updated es.termType object with the "case_insensitive" parameter set.
func (t termType) CaseInsensitive(caseInsensitive bool) termType {
return t.putInTheField("case_insensitive", caseInsensitive)
}
// Boost sets the "boost" parameter in an es.termType query.
//
// This method allows you to specify a boost factor for the term query,
// which influences the relevance score of matching documents. A higher
// boost value increases the importance of the term in the query,
// resulting in higher scores for documents that match this term.
//
// Example usage:
//
// t := es.Term().Boost(1.5)
// // t now includes a "boost" parameter set to 1.5.
//
// Parameters:
// - boost: A float64 value representing the boost factor for the term
// query.
//
// Returns:
//
// The updated es.termType object with the "boost" parameter set.
func (t termType) Boost(boost float64) termType {
return t.putInTheField("boost", boost)
}
// TermFunc creates an es.termType object based on a condition evaluated by a function.
//
// This function conditionally creates an es.termType object if the provided function
// returns true for the given key-value pair. If the function returns false, it
// returns nil instead of creating an es.termType object.
//
// Example usage:
//
// t := es.TermFunc("category", "books", func(key, value string) bool {
// return value != ""
// })
// // t is either an es.termType object or nil based on the condition.
//
// Parameters:
// - key: A string representing the field name for the term query.
// - value: The value to be searched for in the specified field. The type is generic.
// - f: A function that takes a key and value, and returns a boolean indicating
// whether to create the es.termType object.
//
// Returns:
//
// An es.termType object if the condition is true; otherwise, nil.
func TermFunc[T any](key string, value T, f func(key string, value T) bool) termType {
if !f(key, value) {
return nil
}
return Term(key, value)
}
// TermIf creates an es.termType object based on a boolean condition.
//
// This function creates an es.termType object if the provided condition is true. If
// the condition is false, it returns nil instead of creating an es.termType object.
//
// Example usage:
//
// t := es.TermIf("category", "books", true)
// // t is an es.termType object if the condition is true; otherwise, it is nil.
//
// Parameters:
// - key: A string representing the field name for the term query.
// - value: The value to be searched for in the specified field. The type is generic.
// - condition: A boolean value that determines whether to create the es.termType object.
//
// Returns:
//
// An es.termType object if the condition is true; otherwise, nil.
func TermIf[T any](key string, value T, condition bool) termType {
if !condition {
return nil
}
return Term(key, value)
}
func (t termType) putInTheField(key string, value any) termType {
if term, ok := t["term"].(Object); ok {
for field := range term {
if fieldObject, foOk := term[field].(Object); foOk {
fieldObject[key] = value
}
}
}
return t
}