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 "testing"
|
|
|
|
|
2020-02-27 14:19:07 +00:00
|
|
|
func TestCustomQuery(t *testing.T) {
|
|
|
|
m := map[string]interface{}{
|
|
|
|
"geo_distance": map[string]interface{}{
|
|
|
|
"distance": "200km",
|
|
|
|
"pin.location": map[string]interface{}{
|
|
|
|
"lat": 40,
|
|
|
|
"lon": -70,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
runMapTests(t, []mapTest{
|
|
|
|
{
|
|
|
|
"custom query",
|
|
|
|
CustomQuery(m),
|
|
|
|
m,
|
|
|
|
},
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
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 TestCustomAgg(t *testing.T) {
|
|
|
|
m := map[string]interface{}{
|
|
|
|
"genres": map[string]interface{}{
|
|
|
|
"terms": map[string]interface{}{
|
|
|
|
"field": "genre",
|
|
|
|
},
|
|
|
|
"t_shirts": map[string]interface{}{
|
|
|
|
"filter": map[string]interface{}{
|
|
|
|
"term": map[string]interface{}{
|
|
|
|
"type": "t-shirt",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
"aggs": map[string]interface{}{
|
|
|
|
"avg_price": map[string]interface{}{
|
|
|
|
"avg": map[string]interface{}{
|
|
|
|
"field": "price",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
runMapTests(t, []mapTest{
|
|
|
|
{
|
|
|
|
"custom aggregation",
|
2020-02-27 14:19:07 +00:00
|
|
|
CustomAgg("custom_agg", m),
|
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
|
|
|
m,
|
|
|
|
},
|
|
|
|
})
|
|
|
|
}
|