This commit performs the following modifications: - The library is properly documented in Godoc format. - The CustomQuery function is made to be a bit more versatile by allowing it to also be used standalone (i.e. instead of passing a `CustomQuery` as a parameter to the `Query` function, they now have their own `Run` method). - Queries and aggregations can now also be executed using the `RunSearch` method. This method is the same as the `Run` method, except that instead of an `*elasticSearch.Client` value, it accepts an `esapi.Search` value. This is provided for consuming code that needs to implement mock clients of ElasticSearch (e.g. for test purposes). The ElasticSearch client does not provide an interface type describing its API, so its Search function (which is actually a field of a function type) can be used instead. - Bugfix: the CustomAgg function was unusable as it did not accept a name parameter and thus did not implement the Aggregation interface. - Bugfix: the enumeration types are rewritten according to Go standards, and the `RangeRelation` type's default value is now empty. - The golint and godox linters are added.
40 lines
1.1 KiB
Go
40 lines
1.1 KiB
Go
package esquery
|
|
|
|
import "github.com/fatih/structs"
|
|
|
|
// DisMaxQuery represents a compound query of type "dis_max", as described in
|
|
// https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-dis-max-query.html
|
|
type DisMaxQuery struct {
|
|
queries []Mappable
|
|
tieBreaker float32
|
|
}
|
|
|
|
// DisMax creates a new compound query of type "dis_max" with the provided
|
|
// queries.
|
|
func DisMax(queries ...Mappable) *DisMaxQuery {
|
|
return &DisMaxQuery{
|
|
queries: queries,
|
|
}
|
|
}
|
|
|
|
// TieBreaker sets the "tie_breaker" value for the query.
|
|
func (q *DisMaxQuery) TieBreaker(b float32) *DisMaxQuery {
|
|
q.tieBreaker = b
|
|
return q
|
|
}
|
|
|
|
// Map returns a map representation of the dis_max query, thus implementing
|
|
// the Mappable interface.
|
|
func (q *DisMaxQuery) Map() map[string]interface{} {
|
|
inner := make([]map[string]interface{}, len(q.queries))
|
|
for i, iq := range q.queries {
|
|
inner[i] = iq.Map()
|
|
}
|
|
return map[string]interface{}{
|
|
"dis_max": structs.Map(struct {
|
|
Queries []map[string]interface{} `structs:"queries"`
|
|
TieBreaker float32 `structs:"tie_breaker,omitempty"`
|
|
}{inner, q.tieBreaker}),
|
|
}
|
|
}
|