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"
@ -18,6 +20,7 @@ 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,7 +944,21 @@ 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{}
if ESVersion > "5" {
query = map[string]interface{}{
"query": map[string]interface{}{
"bool": map[string]interface{}{
"filter": map[string]interface{}{
"term": map[string]interface{}{
"user": "foo",
},
},
},
},
}
} else {
query = map[string]interface{}{
"query": map[string]interface{}{ "query": map[string]interface{}{
"filtered": map[string]interface{}{ "filtered": map[string]interface{}{
"filter": map[string]interface{}{ "filter": map[string]interface{}{
@ -939,6 +969,7 @@ func (s *GoesTestSuite) TestScroll(c *C) {
}, },
}, },
} }
}
scan, err := conn.Scan(query, []string{indexName}, []string{docType}, "1m", 1) scan, err := conn.Scan(query, []string{indexName}, []string{docType}, "1m", 1)
c.Assert(err, IsNil) c.Assert(err, IsNil)
@ -1187,7 +1218,24 @@ 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{}
if ESVersion > "5" {
query = map[string]interface{}{
"script": map[string]interface{}{
"inline": "ctx._source.counter += params.count",
"lang": "painless",
"params": map[string]interface{}{
"count": 5,
},
},
"upsert": map[string]interface{}{
"message": "candybar",
"user": "admin",
"counter": 1,
},
}
} else {
query = map[string]interface{}{
"script": "ctx._source.counter += count", "script": "ctx._source.counter += count",
"lang": "groovy", "lang": "groovy",
"params": map[string]interface{}{ "params": map[string]interface{}{
@ -1199,8 +1247,10 @@ func (s *GoesTestSuite) TestUpdate(c *C) {
"counter": 1, "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