Initial commit
This commit is the initial commit for a Go library providing an
idiomatic, easy-to-use query builder for ElasticSearch. The library can
build queries and execute them using the structures from the official Go
SDK provided by the ES project (https://github.com/elastic/go-elasticsearch).
The library currently provides the capabilities to create and execute
simple ElasticSearch queries, specifically Match queries (match,
match_bool_prefix, match_phrase and match_phrase_prefix), Match All
queries (match_all, match_none), and all of the Term-level queries (e.g.
range, regexp, etc.).
Unit tests are included for each support query, and the code is linted
using golangci-lint (see enabled linters in .golangci-lint). The unit
tests currently only verify the builder creates valid JSON queries and
does not attempt to actually run queries against a (mock) ES instance.
2020-02-18 12:00:15 +00:00
|
|
|
package esquery
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
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
|
|
|
"reflect"
|
Initial commit
This commit is the initial commit for a Go library providing an
idiomatic, easy-to-use query builder for ElasticSearch. The library can
build queries and execute them using the structures from the official Go
SDK provided by the ES project (https://github.com/elastic/go-elasticsearch).
The library currently provides the capabilities to create and execute
simple ElasticSearch queries, specifically Match queries (match,
match_bool_prefix, match_phrase and match_phrase_prefix), Match All
queries (match_all, match_none), and all of the Term-level queries (e.g.
range, regexp, etc.).
Unit tests are included for each support query, and the code is linted
using golangci-lint (see enabled linters in .golangci-lint). The unit
tests currently only verify the builder creates valid JSON queries and
does not attempt to actually run queries against a (mock) ES instance.
2020-02-18 12:00:15 +00:00
|
|
|
"testing"
|
2020-02-20 14:45:08 +00:00
|
|
|
|
|
|
|
"github.com/jgroeneveld/trial/assert"
|
Initial commit
This commit is the initial commit for a Go library providing an
idiomatic, easy-to-use query builder for ElasticSearch. The library can
build queries and execute them using the structures from the official Go
SDK provided by the ES project (https://github.com/elastic/go-elasticsearch).
The library currently provides the capabilities to create and execute
simple ElasticSearch queries, specifically Match queries (match,
match_bool_prefix, match_phrase and match_phrase_prefix), Match All
queries (match_all, match_none), and all of the Term-level queries (e.g.
range, regexp, etc.).
Unit tests are included for each support query, and the code is linted
using golangci-lint (see enabled linters in .golangci-lint). The unit
tests currently only verify the builder creates valid JSON queries and
does not attempt to actually run queries against a (mock) ES instance.
2020-02-18 12:00:15 +00:00
|
|
|
)
|
|
|
|
|
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 mapTest struct {
|
|
|
|
name string
|
|
|
|
q Mappable
|
|
|
|
exp map[string]interface{}
|
Initial commit
This commit is the initial commit for a Go library providing an
idiomatic, easy-to-use query builder for ElasticSearch. The library can
build queries and execute them using the structures from the official Go
SDK provided by the ES project (https://github.com/elastic/go-elasticsearch).
The library currently provides the capabilities to create and execute
simple ElasticSearch queries, specifically Match queries (match,
match_bool_prefix, match_phrase and match_phrase_prefix), Match All
queries (match_all, match_none), and all of the Term-level queries (e.g.
range, regexp, etc.).
Unit tests are included for each support query, and the code is linted
using golangci-lint (see enabled linters in .golangci-lint). The unit
tests currently only verify the builder creates valid JSON queries and
does not attempt to actually run queries against a (mock) ES instance.
2020-02-18 12:00:15 +00:00
|
|
|
}
|
|
|
|
|
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 runMapTests(t *testing.T, tests []mapTest) {
|
Initial commit
This commit is the initial commit for a Go library providing an
idiomatic, easy-to-use query builder for ElasticSearch. The library can
build queries and execute them using the structures from the official Go
SDK provided by the ES project (https://github.com/elastic/go-elasticsearch).
The library currently provides the capabilities to create and execute
simple ElasticSearch queries, specifically Match queries (match,
match_bool_prefix, match_phrase and match_phrase_prefix), Match All
queries (match_all, match_none), and all of the Term-level queries (e.g.
range, regexp, etc.).
Unit tests are included for each support query, and the code is linted
using golangci-lint (see enabled linters in .golangci-lint). The unit
tests currently only verify the builder creates valid JSON queries and
does not attempt to actually run queries against a (mock) ES instance.
2020-02-18 12:00:15 +00:00
|
|
|
for _, test := range tests {
|
|
|
|
t.Run(test.name, func(t *testing.T) {
|
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 := test.q.Map()
|
|
|
|
|
|
|
|
// convert both maps to JSON in order to compare them. we do not
|
|
|
|
// use reflect.DeepEqual on the maps as this doesn't always work
|
|
|
|
exp, got, ok := sameJSON(test.exp, m)
|
|
|
|
if !ok {
|
|
|
|
t.Errorf("expected %s, got %s", exp, got)
|
Initial commit
This commit is the initial commit for a Go library providing an
idiomatic, easy-to-use query builder for ElasticSearch. The library can
build queries and execute them using the structures from the official Go
SDK provided by the ES project (https://github.com/elastic/go-elasticsearch).
The library currently provides the capabilities to create and execute
simple ElasticSearch queries, specifically Match queries (match,
match_bool_prefix, match_phrase and match_phrase_prefix), Match All
queries (match_all, match_none), and all of the Term-level queries (e.g.
range, regexp, etc.).
Unit tests are included for each support query, and the code is linted
using golangci-lint (see enabled linters in .golangci-lint). The unit
tests currently only verify the builder creates valid JSON queries and
does not attempt to actually run queries against a (mock) ES instance.
2020-02-18 12:00:15 +00:00
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
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 sameJSON(a, b map[string]interface{}) (aJSON, bJSON []byte, ok bool) {
|
|
|
|
aJSON, aErr := json.Marshal(a)
|
|
|
|
bJSON, bErr := json.Marshal(b)
|
|
|
|
|
|
|
|
if aErr != nil || bErr != nil {
|
|
|
|
return aJSON, bJSON, false
|
|
|
|
}
|
|
|
|
|
|
|
|
ok = reflect.DeepEqual(aJSON, bJSON)
|
|
|
|
return aJSON, bJSON, ok
|
|
|
|
}
|
2020-02-20 14:45:08 +00:00
|
|
|
|
|
|
|
type jsonTest struct {
|
|
|
|
name string
|
|
|
|
q json.Marshaler
|
|
|
|
expJSON string
|
|
|
|
expErr error
|
|
|
|
}
|
|
|
|
|
|
|
|
func runJSONTests(t *testing.T, tests []jsonTest) {
|
|
|
|
for _, test := range tests {
|
|
|
|
t.Run(test.name, func(t *testing.T) {
|
|
|
|
b, err := test.q.MarshalJSON()
|
|
|
|
assert.Equal(t, test.expErr, err)
|
|
|
|
assert.Equal(t, test.expJSON, string(b))
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|