-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathexists_query.go
110 lines (104 loc) · 3.32 KB
/
exists_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
package es
type existsType Object
// Exists creates a new es.existsType object to check if a field exists.
//
// This function initializes an es.existsType object that specifies a query to check
// if a particular field exists in the documents. The key parameter represents
// the name of the field to check for existence.
//
// Example usage:
//
// e := es.Exists("title")
// // e now contains an es.existsType object that checks for the existence of the "title" field.
//
// Parameters:
// - key: A string representing the name of the field to check for existence.
//
// Returns:
//
// An es.existsType object that includes the "exists" query for the specified field.
func Exists(key string) existsType {
return existsType{
"exists": Object{
"field": key,
},
}
}
// Boost sets the "boost" parameter in an es.existsType query.
//
// This method allows you to specify a boost factor for the exists query,
// which influences the relevance score of matching documents. A higher
// boost value increases the importance of the query in the overall score,
// resulting in higher scores for documents that satisfy the exists condition.
//
// Example usage:
//
// e := es.Exists().Boost(2.0)
// // e now includes a "boost" parameter set to 2.0.
//
// Parameters:
// - boost: A float64 value representing the boost factor for the exists
// query.
//
// Returns:
//
// The updated es.existsType object with the "boost" parameter set.
func (e existsType) Boost(boost float64) existsType {
return e.putInTheField("boost", boost)
}
// ExistsFunc creates an es.existsType object based on a condition evaluated by a function.
//
// This function conditionally creates an es.existsType object if the provided function
// returns true for the given key. If the function returns false, it returns nil
// instead of creating an es.existsType object.
//
// Example usage:
//
// e := es.ExistsFunc("title", func(key string) bool {
// return key != ""
// })
// // e is either an es.existsType object or nil based on the condition.
//
// Parameters:
// - key: A string representing the name of the field to check for existence.
// - f: A function that takes a key and returns a boolean indicating whether
// to create the es.existsType object.
//
// Returns:
//
// An es.existsType object if the condition is true; otherwise, nil.
func ExistsFunc(key string, f func(key string) bool) existsType {
if !f(key) {
return nil
}
return Exists(key)
}
// ExistsIf creates an es.existsType object based on a boolean condition.
//
// This function creates an es.existsType object if the provided condition is true.
// If the condition is false, it returns nil instead of creating an es.existsType object.
//
// Example usage:
//
// e := es.ExistsIf("title", true)
// // e is an es.existsType object if the condition is true; otherwise, it is nil.
//
// Parameters:
// - key: A string representing the name of the field to check for existence.
// - condition: A boolean value that determines whether to create the es.existsType object.
//
// Returns:
//
// An es.existsType object if the condition is true; otherwise, nil.
func ExistsIf(key string, condition bool) existsType {
if !condition {
return nil
}
return Exists(key)
}
func (e existsType) putInTheField(key string, value any) existsType {
if exists, ok := e["exists"].(Object); ok {
exists[key] = value
}
return e
}