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.
50 lines
1.1 KiB
Go
50 lines
1.1 KiB
Go
package esquery
|
|
|
|
import "github.com/fatih/structs"
|
|
|
|
// MatchAllQuery represents a query of type "match_all" or "match_none", as
|
|
// described in
|
|
// https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-match-all-query.html
|
|
type MatchAllQuery struct {
|
|
all bool
|
|
params matchAllParams
|
|
}
|
|
|
|
type matchAllParams struct {
|
|
Boost float32 `structs:"boost,omitempty"`
|
|
}
|
|
|
|
// Map returns a map representation of the query, thus implementing the
|
|
// Mappable interface.
|
|
func (q *MatchAllQuery) Map() map[string]interface{} {
|
|
var mType string
|
|
switch q.all {
|
|
case true:
|
|
mType = "match_all"
|
|
default:
|
|
mType = "match_none"
|
|
}
|
|
|
|
return map[string]interface{}{
|
|
mType: structs.Map(q.params),
|
|
}
|
|
}
|
|
|
|
// MatchAll creates a new query of type "match_all".
|
|
func MatchAll() *MatchAllQuery {
|
|
return &MatchAllQuery{all: true}
|
|
}
|
|
|
|
// Boost assigns a score boost for documents matching the query.
|
|
func (q *MatchAllQuery) Boost(b float32) *MatchAllQuery {
|
|
if q.all {
|
|
q.params.Boost = b
|
|
}
|
|
return q
|
|
}
|
|
|
|
// MatchNone creates a new query of type "match_none".
|
|
func MatchNone() *MatchAllQuery {
|
|
return &MatchAllQuery{all: false}
|
|
}
|