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 (
|
|
|
|
"bytes"
|
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
|
|
|
|
2020-02-18 16:43:19 +00:00
|
|
|
"github.com/elastic/go-elasticsearch/v7"
|
|
|
|
"github.com/elastic/go-elasticsearch/v7/esapi"
|
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
|
|
|
)
|
|
|
|
|
|
|
|
type ESQuery struct {
|
|
|
|
Query json.Marshaler `json:"query"`
|
|
|
|
}
|
|
|
|
|
|
|
|
func encode(q json.Marshaler, b *bytes.Buffer) (err error) {
|
|
|
|
b.Reset()
|
|
|
|
err = json.NewEncoder(b).Encode(q)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("failed encoding query to JSON: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-02-18 16:43:19 +00:00
|
|
|
func Search(
|
|
|
|
api *elasticsearch.Client,
|
|
|
|
q json.Marshaler,
|
|
|
|
o ...func(*esapi.SearchRequest),
|
|
|
|
) (res *esapi.Response, err error) {
|
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
|
|
|
var b bytes.Buffer
|
|
|
|
err = encode(ESQuery{q}, &b)
|
|
|
|
if err != nil {
|
|
|
|
return res, err
|
|
|
|
}
|
|
|
|
|
|
|
|
opts := append([]func(*esapi.SearchRequest){api.Search.WithBody(&b)}, o...)
|
|
|
|
|
|
|
|
return api.Search(opts...)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (q ESQuery) MarshalJSON() ([]byte, error) {
|
|
|
|
return json.Marshal(map[string]json.Marshaler{
|
|
|
|
"query": q.Query,
|
|
|
|
})
|
|
|
|
}
|