Build in a way to detect the ES version so it can be used for version-specific things
This commit is contained in:
parent
f5716dce83
commit
f9192a7ca8
24
goes.go
24
goes.go
@ -34,7 +34,7 @@ func (err *SearchError) Error() string {
|
||||
// This function is pretty useless for now but might be useful in a near future
|
||||
// if wee need more features like connection pooling or load balancing.
|
||||
func NewClient(host string, port string) *Client {
|
||||
return &Client{host, port, http.DefaultClient}
|
||||
return &Client{host, port, http.DefaultClient, ""}
|
||||
}
|
||||
|
||||
// WithHTTPClient sets the http.Client to be used with the connection. Returns the original client.
|
||||
@ -43,6 +43,28 @@ func (c *Client) WithHTTPClient(cl *http.Client) *Client {
|
||||
return c
|
||||
}
|
||||
|
||||
// Version returns the detected version of the connected ES server
|
||||
func (c *Client) Version() (string, error) {
|
||||
// Use cached version if it was already fetched
|
||||
if c.version != "" {
|
||||
return c.version, nil
|
||||
}
|
||||
|
||||
// Get the version if it was not cached
|
||||
r := Request{Method: "GET"}
|
||||
res, err := c.Do(&r)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
if version, ok := res.Raw["version"].(map[string]interface{}); ok {
|
||||
if number, ok := version["number"].(string); ok {
|
||||
c.version = number
|
||||
return number, nil
|
||||
}
|
||||
}
|
||||
return "", errors.New("No version returned by ElasticSearch Server")
|
||||
}
|
||||
|
||||
// CreateIndex creates a new index represented by a name and a mapping
|
||||
func (c *Client) CreateIndex(name string, mapping interface{}) (*Response, error) {
|
||||
r := Request{
|
||||
|
27
goes_test.go
27
goes_test.go
@ -5,7 +5,6 @@
|
||||
package goes
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"net/http"
|
||||
"net/url"
|
||||
@ -18,9 +17,8 @@ import (
|
||||
)
|
||||
|
||||
var (
|
||||
ESHost = "localhost"
|
||||
ESPort = "9200"
|
||||
ESVersion = "0.0.0"
|
||||
ESHost = "localhost"
|
||||
ESPort = "9200"
|
||||
)
|
||||
|
||||
// Hook up gocheck into the gotest runner.
|
||||
@ -40,24 +38,11 @@ func (s *GoesTestSuite) SetUpTest(c *C) {
|
||||
if p != "" {
|
||||
ESPort = p
|
||||
}
|
||||
ESVersion = getESVersion(c, ESHost, ESPort)
|
||||
}
|
||||
|
||||
func getESVersion(c *C, host, port string) string {
|
||||
res, err := http.Get(fmt.Sprintf("http://%s:%s/", host, port))
|
||||
c.Assert(err, Equals, nil)
|
||||
defer res.Body.Close()
|
||||
decoder := json.NewDecoder(res.Body)
|
||||
|
||||
var info map[string]interface{}
|
||||
err = decoder.Decode(&info)
|
||||
c.Assert(err, Equals, nil)
|
||||
return info["version"].(map[string]interface{})["number"].(string)
|
||||
}
|
||||
|
||||
func (s *GoesTestSuite) TestNewClient(c *C) {
|
||||
conn := NewClient(ESHost, ESPort)
|
||||
c.Assert(conn, DeepEquals, &Client{ESHost, ESPort, http.DefaultClient})
|
||||
c.Assert(conn, DeepEquals, &Client{ESHost, ESPort, http.DefaultClient, ""})
|
||||
}
|
||||
|
||||
func (s *GoesTestSuite) TestWithHTTPClient(c *C) {
|
||||
@ -70,7 +55,7 @@ func (s *GoesTestSuite) TestWithHTTPClient(c *C) {
|
||||
}
|
||||
conn := NewClient(ESHost, ESPort).WithHTTPClient(cl)
|
||||
|
||||
c.Assert(conn, DeepEquals, &Client{ESHost, ESPort, cl})
|
||||
c.Assert(conn, DeepEquals, &Client{ESHost, ESPort, cl, ""})
|
||||
c.Assert(conn.Client.Transport.(*http.Transport).DisableCompression, Equals, true)
|
||||
c.Assert(conn.Client.Transport.(*http.Transport).ResponseHeaderTimeout, Equals, 1*time.Second)
|
||||
}
|
||||
@ -945,7 +930,7 @@ func (s *GoesTestSuite) TestScroll(c *C) {
|
||||
c.Assert(err, IsNil)
|
||||
|
||||
var query map[string]interface{}
|
||||
if ESVersion > "5" {
|
||||
if version, _ := conn.Version(); version > "5" {
|
||||
query = map[string]interface{}{
|
||||
"query": map[string]interface{}{
|
||||
"bool": map[string]interface{}{
|
||||
@ -1219,7 +1204,7 @@ func (s *GoesTestSuite) TestUpdate(c *C) {
|
||||
|
||||
// Now that we have an ordinary document indexed, try updating it
|
||||
var query map[string]interface{}
|
||||
if ESVersion > "5" {
|
||||
if version, _ := conn.Version(); version > "5" {
|
||||
query = map[string]interface{}{
|
||||
"script": map[string]interface{}{
|
||||
"inline": "ctx._source.counter += params.count",
|
||||
|
@ -20,6 +20,9 @@ type Client struct {
|
||||
// Client is the http client used to make requests, allowing settings things
|
||||
// such as timeouts etc
|
||||
Client *http.Client
|
||||
|
||||
// Detected version of ES
|
||||
version string
|
||||
}
|
||||
|
||||
// Response holds an elasticsearch response
|
||||
|
Loading…
Reference in New Issue
Block a user