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
|
// 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.
|
// if wee need more features like connection pooling or load balancing.
|
||||||
func NewClient(host string, port string) *Client {
|
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.
|
// 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
|
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
|
// CreateIndex creates a new index represented by a name and a mapping
|
||||||
func (c *Client) CreateIndex(name string, mapping interface{}) (*Response, error) {
|
func (c *Client) CreateIndex(name string, mapping interface{}) (*Response, error) {
|
||||||
r := Request{
|
r := Request{
|
||||||
|
23
goes_test.go
23
goes_test.go
@ -5,7 +5,6 @@
|
|||||||
package goes
|
package goes
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"encoding/json"
|
|
||||||
"fmt"
|
"fmt"
|
||||||
"net/http"
|
"net/http"
|
||||||
"net/url"
|
"net/url"
|
||||||
@ -20,7 +19,6 @@ import (
|
|||||||
var (
|
var (
|
||||||
ESHost = "localhost"
|
ESHost = "localhost"
|
||||||
ESPort = "9200"
|
ESPort = "9200"
|
||||||
ESVersion = "0.0.0"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
// Hook up gocheck into the gotest runner.
|
// Hook up gocheck into the gotest runner.
|
||||||
@ -40,24 +38,11 @@ func (s *GoesTestSuite) SetUpTest(c *C) {
|
|||||||
if p != "" {
|
if p != "" {
|
||||||
ESPort = 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) {
|
func (s *GoesTestSuite) TestNewClient(c *C) {
|
||||||
conn := NewClient(ESHost, ESPort)
|
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) {
|
func (s *GoesTestSuite) TestWithHTTPClient(c *C) {
|
||||||
@ -70,7 +55,7 @@ func (s *GoesTestSuite) TestWithHTTPClient(c *C) {
|
|||||||
}
|
}
|
||||||
conn := NewClient(ESHost, ESPort).WithHTTPClient(cl)
|
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).DisableCompression, Equals, true)
|
||||||
c.Assert(conn.Client.Transport.(*http.Transport).ResponseHeaderTimeout, Equals, 1*time.Second)
|
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)
|
c.Assert(err, IsNil)
|
||||||
|
|
||||||
var query map[string]interface{}
|
var query map[string]interface{}
|
||||||
if ESVersion > "5" {
|
if version, _ := conn.Version(); version > "5" {
|
||||||
query = map[string]interface{}{
|
query = map[string]interface{}{
|
||||||
"query": map[string]interface{}{
|
"query": map[string]interface{}{
|
||||||
"bool": 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
|
// Now that we have an ordinary document indexed, try updating it
|
||||||
var query map[string]interface{}
|
var query map[string]interface{}
|
||||||
if ESVersion > "5" {
|
if version, _ := conn.Version(); version > "5" {
|
||||||
query = map[string]interface{}{
|
query = map[string]interface{}{
|
||||||
"script": map[string]interface{}{
|
"script": map[string]interface{}{
|
||||||
"inline": "ctx._source.counter += params.count",
|
"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
|
// Client is the http client used to make requests, allowing settings things
|
||||||
// such as timeouts etc
|
// such as timeouts etc
|
||||||
Client *http.Client
|
Client *http.Client
|
||||||
|
|
||||||
|
// Detected version of ES
|
||||||
|
version string
|
||||||
}
|
}
|
||||||
|
|
||||||
// Response holds an elasticsearch response
|
// Response holds an elasticsearch response
|
||||||
|
Loading…
Reference in New Issue
Block a user