Refactor API, add aggregations and custom queries
This commit introduces a refactor of the codebase and the API, to make
it more user friendly. Queries can now directly be executed via the
`Run()` method. Internally, the library no longer uses JSON generation
as a major mechanism, instead all types need to implement a `Mappable`
interface which simply turns each type in a `map[string]interface{}`,
which is what the ElasticSearch client expects. This makes the code
easier to write, and makes writing tests less error prone, as JSON need
not be written directly.
Support for metrics aggregations is also added. However, aggregations of
type bucket, pipeline and matrix are not supported yet.
To make the library more useful in its current state, support is added
for running custom queries and aggregations, via the `CustomQuery()` and
`CustomAgg()` functions, which both accepts an arbitrary
`map[string]interface{}`.
2020-02-19 11:35:21 +00:00
|
|
|
package esquery
|
|
|
|
|
|
|
|
import "github.com/fatih/structs"
|
|
|
|
|
2020-02-27 14:19:07 +00:00
|
|
|
// 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
|
Refactor API, add aggregations and custom queries
This commit introduces a refactor of the codebase and the API, to make
it more user friendly. Queries can now directly be executed via the
`Run()` method. Internally, the library no longer uses JSON generation
as a major mechanism, instead all types need to implement a `Mappable`
interface which simply turns each type in a `map[string]interface{}`,
which is what the ElasticSearch client expects. This makes the code
easier to write, and makes writing tests less error prone, as JSON need
not be written directly.
Support for metrics aggregations is also added. However, aggregations of
type bucket, pipeline and matrix are not supported yet.
To make the library more useful in its current state, support is added
for running custom queries and aggregations, via the `CustomQuery()` and
`CustomAgg()` functions, which both accepts an arbitrary
`map[string]interface{}`.
2020-02-19 11:35:21 +00:00
|
|
|
type ConstantScoreQuery struct {
|
|
|
|
filter Mappable
|
|
|
|
boost float32
|
|
|
|
}
|
|
|
|
|
2020-02-27 14:19:07 +00:00
|
|
|
// ConstantScore creates a new query of type "contant_score" with the provided
|
|
|
|
// filter query.
|
Refactor API, add aggregations and custom queries
This commit introduces a refactor of the codebase and the API, to make
it more user friendly. Queries can now directly be executed via the
`Run()` method. Internally, the library no longer uses JSON generation
as a major mechanism, instead all types need to implement a `Mappable`
interface which simply turns each type in a `map[string]interface{}`,
which is what the ElasticSearch client expects. This makes the code
easier to write, and makes writing tests less error prone, as JSON need
not be written directly.
Support for metrics aggregations is also added. However, aggregations of
type bucket, pipeline and matrix are not supported yet.
To make the library more useful in its current state, support is added
for running custom queries and aggregations, via the `CustomQuery()` and
`CustomAgg()` functions, which both accepts an arbitrary
`map[string]interface{}`.
2020-02-19 11:35:21 +00:00
|
|
|
func ConstantScore(filter Mappable) *ConstantScoreQuery {
|
|
|
|
return &ConstantScoreQuery{
|
|
|
|
filter: filter,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-02-27 14:19:07 +00:00
|
|
|
// Boost sets the boost value of the query.
|
Refactor API, add aggregations and custom queries
This commit introduces a refactor of the codebase and the API, to make
it more user friendly. Queries can now directly be executed via the
`Run()` method. Internally, the library no longer uses JSON generation
as a major mechanism, instead all types need to implement a `Mappable`
interface which simply turns each type in a `map[string]interface{}`,
which is what the ElasticSearch client expects. This makes the code
easier to write, and makes writing tests less error prone, as JSON need
not be written directly.
Support for metrics aggregations is also added. However, aggregations of
type bucket, pipeline and matrix are not supported yet.
To make the library more useful in its current state, support is added
for running custom queries and aggregations, via the `CustomQuery()` and
`CustomAgg()` functions, which both accepts an arbitrary
`map[string]interface{}`.
2020-02-19 11:35:21 +00:00
|
|
|
func (q *ConstantScoreQuery) Boost(b float32) *ConstantScoreQuery {
|
|
|
|
q.boost = b
|
|
|
|
return q
|
|
|
|
}
|
|
|
|
|
2020-02-27 14:19:07 +00:00
|
|
|
// Map returns a map representation of the query, thus implementing the
|
|
|
|
// Mappable interface.
|
Refactor API, add aggregations and custom queries
This commit introduces a refactor of the codebase and the API, to make
it more user friendly. Queries can now directly be executed via the
`Run()` method. Internally, the library no longer uses JSON generation
as a major mechanism, instead all types need to implement a `Mappable`
interface which simply turns each type in a `map[string]interface{}`,
which is what the ElasticSearch client expects. This makes the code
easier to write, and makes writing tests less error prone, as JSON need
not be written directly.
Support for metrics aggregations is also added. However, aggregations of
type bucket, pipeline and matrix are not supported yet.
To make the library more useful in its current state, support is added
for running custom queries and aggregations, via the `CustomQuery()` and
`CustomAgg()` functions, which both accepts an arbitrary
`map[string]interface{}`.
2020-02-19 11:35:21 +00:00
|
|
|
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}),
|
|
|
|
}
|
|
|
|
}
|