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.
37 lines
1.0 KiB
Go
37 lines
1.0 KiB
Go
package esquery
|
|
|
|
import "github.com/fatih/structs"
|
|
|
|
// ConstantScoreQuery represents a compound query of type "constant_score", as
|
|
// described in
|
|
// https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-constant-score-query.html
|
|
type ConstantScoreQuery struct {
|
|
filter Mappable
|
|
boost float32
|
|
}
|
|
|
|
// ConstantScore creates a new query of type "contant_score" with the provided
|
|
// filter query.
|
|
func ConstantScore(filter Mappable) *ConstantScoreQuery {
|
|
return &ConstantScoreQuery{
|
|
filter: filter,
|
|
}
|
|
}
|
|
|
|
// Boost sets the boost value of the query.
|
|
func (q *ConstantScoreQuery) Boost(b float32) *ConstantScoreQuery {
|
|
q.boost = b
|
|
return q
|
|
}
|
|
|
|
// Map returns a map representation of the query, thus implementing the
|
|
// Mappable interface.
|
|
func (q *ConstantScoreQuery) Map() map[string]interface{} {
|
|
return map[string]interface{}{
|
|
"constant_score": structs.Map(struct {
|
|
Filter map[string]interface{} `structs:"filter"`
|
|
Boost float32 `structs:"boost,omitempty"`
|
|
}{q.filter.Map(), q.boost}),
|
|
}
|
|
}
|