Some 5.x query changes

This commit is contained in:
Paul Bonser 2017-02-02 11:01:27 -06:00
parent a1af556756
commit f5716dce83

View File

@ -5,6 +5,8 @@
package goes package goes
import ( import (
"encoding/json"
"fmt"
"net/http" "net/http"
"net/url" "net/url"
"os" "os"
@ -16,8 +18,9 @@ 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.
@ -37,6 +40,19 @@ 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) {
@ -928,16 +944,31 @@ func (s *GoesTestSuite) TestScroll(c *C) {
_, err = conn.RefreshIndex(indexName) _, err = conn.RefreshIndex(indexName)
c.Assert(err, IsNil) c.Assert(err, IsNil)
query := map[string]interface{}{ var query map[string]interface{}
"query": map[string]interface{}{ if ESVersion > "5" {
"filtered": map[string]interface{}{ query = map[string]interface{}{
"filter": map[string]interface{}{ "query": map[string]interface{}{
"term": map[string]interface{}{ "bool": map[string]interface{}{
"user": "foo", "filter": map[string]interface{}{
"term": map[string]interface{}{
"user": "foo",
},
}, },
}, },
}, },
}, }
} else {
query = map[string]interface{}{
"query": map[string]interface{}{
"filtered": map[string]interface{}{
"filter": map[string]interface{}{
"term": map[string]interface{}{
"user": "foo",
},
},
},
},
}
} }
scan, err := conn.Scan(query, []string{indexName}, []string{docType}, "1m", 1) scan, err := conn.Scan(query, []string{indexName}, []string{docType}, "1m", 1)
@ -1187,20 +1218,39 @@ func (s *GoesTestSuite) TestUpdate(c *C) {
c.Assert(response, DeepEquals, expectedResponse) c.Assert(response, DeepEquals, expectedResponse)
// Now that we have an ordinary document indexed, try updating it // Now that we have an ordinary document indexed, try updating it
query := map[string]interface{}{ var query map[string]interface{}
"script": "ctx._source.counter += count", if ESVersion > "5" {
"lang": "groovy", query = map[string]interface{}{
"params": map[string]interface{}{ "script": map[string]interface{}{
"count": 5, "inline": "ctx._source.counter += params.count",
}, "lang": "painless",
"upsert": map[string]interface{}{ "params": map[string]interface{}{
"message": "candybar", "count": 5,
"user": "admin", },
"counter": 1, },
}, "upsert": map[string]interface{}{
"message": "candybar",
"user": "admin",
"counter": 1,
},
}
} else {
query = map[string]interface{}{
"script": "ctx._source.counter += count",
"lang": "groovy",
"params": map[string]interface{}{
"count": 5,
},
"upsert": map[string]interface{}{
"message": "candybar",
"user": "admin",
"counter": 1,
},
}
} }
response, err = conn.Update(d, query, extraArgs) response, err = conn.Update(d, query, extraArgs)
fmt.Println(response)
if err != nil && strings.Contains(err.(*SearchError).Msg, "dynamic scripting") { if err != nil && strings.Contains(err.(*SearchError).Msg, "dynamic scripting") {
c.Skip("Scripting is disabled on server, skipping this test") c.Skip("Scripting is disabled on server, skipping this test")
return return