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
|
|
|
// BoolQuery represents a compound query of type "bool", as described in
|
|
|
|
// https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-bool-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 BoolQuery struct {
|
|
|
|
must []Mappable
|
|
|
|
filter []Mappable
|
|
|
|
mustNot []Mappable
|
|
|
|
should []Mappable
|
|
|
|
minimumShouldMatch int16
|
|
|
|
boost float32
|
|
|
|
}
|
|
|
|
|
2020-02-27 14:19:07 +00:00
|
|
|
// Bool creates a new compound query of type "bool".
|
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 Bool() *BoolQuery {
|
|
|
|
return &BoolQuery{}
|
|
|
|
}
|
|
|
|
|
2020-02-27 14:19:07 +00:00
|
|
|
// Must adds one or more queries of type "must" to the bool query. Must can be
|
|
|
|
// called multiple times, queries will be appended to existing ones.
|
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 *BoolQuery) Must(must ...Mappable) *BoolQuery {
|
|
|
|
q.must = append(q.must, must...)
|
|
|
|
return q
|
|
|
|
}
|
|
|
|
|
2020-02-27 14:19:07 +00:00
|
|
|
// Filter adds one or more queries of type "filter" to the bool query. Filter
|
|
|
|
// can be called multiple times, queries will be appended to existing ones.
|
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 *BoolQuery) Filter(filter ...Mappable) *BoolQuery {
|
|
|
|
q.filter = append(q.filter, filter...)
|
|
|
|
return q
|
|
|
|
}
|
|
|
|
|
2023-11-01 21:30:33 +00:00
|
|
|
// MustNot adds one or more queries of type "must_not" to the bool query. MustNot
|
2020-02-27 14:19:07 +00:00
|
|
|
// can be called multiple times, queries will be appended to existing ones.
|
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 *BoolQuery) MustNot(mustnot ...Mappable) *BoolQuery {
|
|
|
|
q.mustNot = append(q.mustNot, mustnot...)
|
|
|
|
return q
|
|
|
|
}
|
|
|
|
|
2020-02-27 14:19:07 +00:00
|
|
|
// Should adds one or more queries of type "should" to the bool query. Should can be
|
|
|
|
// called multiple times, queries will be appended to existing ones.
|
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 *BoolQuery) Should(should ...Mappable) *BoolQuery {
|
|
|
|
q.should = append(q.should, should...)
|
|
|
|
return q
|
|
|
|
}
|
|
|
|
|
2020-02-27 14:19:07 +00:00
|
|
|
// MinimumShouldMatch sets the number or percentage of should clauses returned
|
|
|
|
// documents must match.
|
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 *BoolQuery) MinimumShouldMatch(val int16) *BoolQuery {
|
|
|
|
q.minimumShouldMatch = val
|
|
|
|
return q
|
|
|
|
}
|
|
|
|
|
2020-02-27 14:19:07 +00:00
|
|
|
// Boost sets the boost value for 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 *BoolQuery) Boost(val float32) *BoolQuery {
|
|
|
|
q.boost = val
|
|
|
|
return q
|
|
|
|
}
|
|
|
|
|
2020-02-27 14:19:07 +00:00
|
|
|
// Map returns a map representation of the bool 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 *BoolQuery) Map() map[string]interface{} {
|
|
|
|
var data struct {
|
|
|
|
Must []map[string]interface{} `structs:"must,omitempty"`
|
|
|
|
Filter []map[string]interface{} `structs:"filter,omitempty"`
|
|
|
|
MustNot []map[string]interface{} `structs:"must_not,omitempty"`
|
|
|
|
Should []map[string]interface{} `structs:"should,omitempty"`
|
|
|
|
MinimumShouldMatch int16 `structs:"minimum_should_match,omitempty"`
|
|
|
|
Boost float32 `structs:"boost,omitempty"`
|
|
|
|
}
|
|
|
|
|
|
|
|
data.MinimumShouldMatch = q.minimumShouldMatch
|
|
|
|
data.Boost = q.boost
|
|
|
|
|
|
|
|
if len(q.must) > 0 {
|
|
|
|
data.Must = make([]map[string]interface{}, len(q.must))
|
|
|
|
for i, m := range q.must {
|
|
|
|
data.Must[i] = m.Map()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(q.filter) > 0 {
|
|
|
|
data.Filter = make([]map[string]interface{}, len(q.filter))
|
|
|
|
for i, m := range q.filter {
|
|
|
|
data.Filter[i] = m.Map()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(q.mustNot) > 0 {
|
|
|
|
data.MustNot = make([]map[string]interface{}, len(q.mustNot))
|
|
|
|
for i, m := range q.mustNot {
|
|
|
|
data.MustNot[i] = m.Map()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(q.should) > 0 {
|
|
|
|
data.Should = make([]map[string]interface{}, len(q.should))
|
|
|
|
for i, m := range q.should {
|
|
|
|
data.Should[i] = m.Map()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return map[string]interface{}{
|
|
|
|
"bool": structs.Map(data),
|
|
|
|
}
|
|
|
|
}
|