2013-06-15 14:18:48 +08:00
|
|
|
// Copyright 2013 Belogik. All rights reserved.
|
|
|
|
// Use of this source code is governed by a BSD-style
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
|
|
|
package goes
|
|
|
|
|
|
|
|
import (
|
2014-05-30 05:01:39 +08:00
|
|
|
"net/http"
|
2013-06-15 14:18:48 +08:00
|
|
|
"net/url"
|
|
|
|
"os"
|
2014-11-13 00:35:37 +08:00
|
|
|
"strings"
|
2013-06-15 14:18:48 +08:00
|
|
|
"testing"
|
|
|
|
"time"
|
2015-06-24 04:05:28 +08:00
|
|
|
|
2016-07-27 03:55:16 +08:00
|
|
|
. "github.com/go-check/check"
|
2013-06-15 14:18:48 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
2017-02-03 01:54:24 +08:00
|
|
|
ESHost = "localhost"
|
|
|
|
ESPort = "9200"
|
2013-06-15 14:18:48 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
// Hook up gocheck into the gotest runner.
|
|
|
|
func Test(t *testing.T) { TestingT(t) }
|
|
|
|
|
|
|
|
type GoesTestSuite struct{}
|
|
|
|
|
|
|
|
var _ = Suite(&GoesTestSuite{})
|
|
|
|
|
|
|
|
func (s *GoesTestSuite) SetUpTest(c *C) {
|
|
|
|
h := os.Getenv("TEST_ELASTICSEARCH_HOST")
|
|
|
|
if h != "" {
|
2016-09-28 03:19:07 +08:00
|
|
|
ESHost = h
|
2013-06-15 14:18:48 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
p := os.Getenv("TEST_ELASTICSEARCH_PORT")
|
|
|
|
if p != "" {
|
2016-09-28 03:19:07 +08:00
|
|
|
ESPort = p
|
2013-06-15 14:18:48 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-09-28 02:56:13 +08:00
|
|
|
func (s *GoesTestSuite) TestNewClient(c *C) {
|
2016-09-28 03:19:07 +08:00
|
|
|
conn := NewClient(ESHost, ESPort)
|
2017-03-03 08:58:52 +08:00
|
|
|
c.Assert(conn, DeepEquals, &Client{&url.URL{Scheme: "http", Host: ESHost + ":" + ESPort}, http.DefaultClient, ""})
|
2014-05-30 05:01:39 +08:00
|
|
|
}
|
|
|
|
|
2016-09-28 02:56:13 +08:00
|
|
|
func (s *GoesTestSuite) TestWithHTTPClient(c *C) {
|
2014-05-30 05:01:39 +08:00
|
|
|
tr := &http.Transport{
|
|
|
|
DisableCompression: true,
|
|
|
|
ResponseHeaderTimeout: 1 * time.Second,
|
|
|
|
}
|
|
|
|
cl := &http.Client{
|
|
|
|
Transport: tr,
|
|
|
|
}
|
2016-09-28 03:19:07 +08:00
|
|
|
conn := NewClient(ESHost, ESPort).WithHTTPClient(cl)
|
2014-05-30 05:01:39 +08:00
|
|
|
|
2017-03-03 08:58:52 +08:00
|
|
|
c.Assert(conn.Host, DeepEquals, &url.URL{Scheme: "http", Host: ESHost + ":" + ESPort})
|
|
|
|
c.Assert(conn.Client, DeepEquals, cl)
|
2014-05-30 05:01:39 +08:00
|
|
|
c.Assert(conn.Client.Transport.(*http.Transport).DisableCompression, Equals, true)
|
|
|
|
c.Assert(conn.Client.Transport.(*http.Transport).ResponseHeaderTimeout, Equals, 1*time.Second)
|
2013-06-15 14:18:48 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s *GoesTestSuite) TestUrl(c *C) {
|
|
|
|
r := Request{
|
|
|
|
Query: "q",
|
|
|
|
IndexList: []string{"i"},
|
|
|
|
TypeList: []string{},
|
2016-09-28 03:30:17 +08:00
|
|
|
Method: "GET",
|
|
|
|
API: "_search",
|
2013-06-15 14:18:48 +08:00
|
|
|
}
|
|
|
|
|
2016-09-28 05:41:27 +08:00
|
|
|
c.Assert(r.URL().String(), Equals, "/i/_search")
|
2013-06-15 14:18:48 +08:00
|
|
|
|
|
|
|
r.IndexList = []string{"a", "b"}
|
2016-09-28 05:41:27 +08:00
|
|
|
c.Assert(r.URL().String(), Equals, "/a,b/_search")
|
2013-06-15 14:18:48 +08:00
|
|
|
|
|
|
|
r.TypeList = []string{"c", "d"}
|
2016-09-28 05:41:27 +08:00
|
|
|
c.Assert(r.URL().String(), Equals, "/a,b/c,d/_search")
|
2013-06-15 14:18:48 +08:00
|
|
|
|
|
|
|
r.ExtraArgs = make(url.Values, 1)
|
|
|
|
r.ExtraArgs.Set("version", "1")
|
2016-09-28 05:41:27 +08:00
|
|
|
c.Assert(r.URL().String(), Equals, "/a,b/c,d/_search?version=1")
|
2013-06-15 14:18:48 +08:00
|
|
|
|
2016-09-28 03:30:17 +08:00
|
|
|
r.ID = "1234"
|
|
|
|
r.API = ""
|
2016-09-28 05:41:27 +08:00
|
|
|
c.Assert(r.URL().String(), Equals, "/a,b/c,d/1234/?version=1")
|
2013-06-15 14:18:48 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s *GoesTestSuite) TestEsDown(c *C) {
|
2016-09-28 02:56:13 +08:00
|
|
|
conn := NewClient("a.b.c.d", "1234")
|
2013-06-15 14:18:48 +08:00
|
|
|
|
|
|
|
var query = map[string]interface{}{"query": "foo"}
|
|
|
|
|
|
|
|
r := Request{
|
|
|
|
Query: query,
|
|
|
|
IndexList: []string{"i"},
|
2016-09-28 03:30:17 +08:00
|
|
|
Method: "GET",
|
|
|
|
API: "_search",
|
2013-06-15 14:18:48 +08:00
|
|
|
}
|
2016-09-28 05:41:27 +08:00
|
|
|
_, err := conn.Do(&r)
|
2013-06-15 14:18:48 +08:00
|
|
|
|
2015-12-05 03:27:48 +08:00
|
|
|
c.Assert(err, ErrorMatches, ".* no such host")
|
2013-06-15 14:18:48 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s *GoesTestSuite) TestRunMissingIndex(c *C) {
|
2016-09-28 03:19:07 +08:00
|
|
|
conn := NewClient(ESHost, ESPort)
|
2013-06-15 14:18:48 +08:00
|
|
|
|
|
|
|
var query = map[string]interface{}{"query": "foo"}
|
|
|
|
|
|
|
|
r := Request{
|
|
|
|
Query: query,
|
|
|
|
IndexList: []string{"i"},
|
2016-09-28 03:30:17 +08:00
|
|
|
Method: "GET",
|
|
|
|
API: "_search",
|
2013-06-15 14:18:48 +08:00
|
|
|
}
|
2016-09-28 05:41:27 +08:00
|
|
|
_, err := conn.Do(&r)
|
2013-06-15 14:18:48 +08:00
|
|
|
|
2017-02-03 09:19:51 +08:00
|
|
|
c.Assert(err.Error(), Matches, "\\[40.\\] .*i.*")
|
2013-06-15 14:18:48 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s *GoesTestSuite) TestCreateIndex(c *C) {
|
|
|
|
indexName := "testcreateindexgoes"
|
|
|
|
|
2016-09-28 03:19:07 +08:00
|
|
|
conn := NewClient(ESHost, ESPort)
|
2013-06-15 14:18:48 +08:00
|
|
|
defer conn.DeleteIndex(indexName)
|
|
|
|
|
|
|
|
mapping := map[string]interface{}{
|
|
|
|
"settings": map[string]interface{}{
|
|
|
|
"index.number_of_shards": 1,
|
|
|
|
"index.number_of_replicas": 0,
|
|
|
|
},
|
|
|
|
"mappings": map[string]interface{}{
|
|
|
|
"_default_": map[string]interface{}{
|
|
|
|
"_source": map[string]interface{}{
|
|
|
|
"enabled": false,
|
|
|
|
},
|
|
|
|
"_all": map[string]interface{}{
|
|
|
|
"enabled": false,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
resp, err := conn.CreateIndex(indexName, mapping)
|
|
|
|
|
|
|
|
c.Assert(err, IsNil)
|
|
|
|
c.Assert(resp.Acknowledged, Equals, true)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *GoesTestSuite) TestDeleteIndexInexistantIndex(c *C) {
|
2016-09-28 03:19:07 +08:00
|
|
|
conn := NewClient(ESHost, ESPort)
|
2013-06-15 14:18:48 +08:00
|
|
|
resp, err := conn.DeleteIndex("foobar")
|
|
|
|
|
2017-02-03 08:19:27 +08:00
|
|
|
c.Assert(err.Error(), Matches, "\\[404\\] .*foobar.*")
|
2015-07-15 00:29:04 +08:00
|
|
|
resp.Raw = nil // Don't make us have to duplicate this.
|
2017-02-03 08:19:27 +08:00
|
|
|
c.Assert(resp.Status, Equals, uint64(404))
|
|
|
|
c.Assert(resp.Error, Matches, ".*foobar.*")
|
2013-06-15 14:18:48 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s *GoesTestSuite) TestDeleteIndexExistingIndex(c *C) {
|
2016-09-28 03:19:07 +08:00
|
|
|
conn := NewClient(ESHost, ESPort)
|
2013-06-15 14:18:48 +08:00
|
|
|
|
|
|
|
indexName := "testdeleteindexexistingindex"
|
|
|
|
|
|
|
|
_, err := conn.CreateIndex(indexName, map[string]interface{}{})
|
2017-02-03 09:20:07 +08:00
|
|
|
defer conn.DeleteIndex(indexName)
|
2013-06-15 14:18:48 +08:00
|
|
|
|
|
|
|
c.Assert(err, IsNil)
|
|
|
|
|
|
|
|
resp, err := conn.DeleteIndex(indexName)
|
|
|
|
c.Assert(err, IsNil)
|
|
|
|
|
2015-07-15 00:29:04 +08:00
|
|
|
expectedResponse := &Response{
|
|
|
|
Acknowledged: true,
|
|
|
|
Status: 200,
|
|
|
|
}
|
2014-11-13 16:21:27 +08:00
|
|
|
resp.Raw = nil
|
2013-06-15 14:18:48 +08:00
|
|
|
c.Assert(resp, DeepEquals, expectedResponse)
|
|
|
|
}
|
|
|
|
|
2015-10-02 19:25:29 +08:00
|
|
|
func (s *GoesTestSuite) TestUpdateIndexSettings(c *C) {
|
2016-09-28 03:19:07 +08:00
|
|
|
conn := NewClient(ESHost, ESPort)
|
2015-10-02 19:25:29 +08:00
|
|
|
indexName := "testupdateindex"
|
|
|
|
|
2017-02-03 09:20:07 +08:00
|
|
|
// Just in case
|
|
|
|
conn.DeleteIndex(indexName)
|
|
|
|
|
2015-10-02 19:25:29 +08:00
|
|
|
_, err := conn.CreateIndex(indexName, map[string]interface{}{})
|
|
|
|
c.Assert(err, IsNil)
|
2017-02-03 09:20:07 +08:00
|
|
|
defer conn.DeleteIndex(indexName)
|
2015-10-02 19:25:29 +08:00
|
|
|
|
2015-10-02 19:59:55 +08:00
|
|
|
_, err = conn.UpdateIndexSettings(indexName, map[string]interface{}{
|
2015-10-02 19:25:29 +08:00
|
|
|
"index": map[string]interface{}{
|
|
|
|
"number_of_replicas": 0,
|
|
|
|
},
|
|
|
|
})
|
|
|
|
c.Assert(err, IsNil)
|
|
|
|
|
|
|
|
_, err = conn.DeleteIndex(indexName)
|
|
|
|
c.Assert(err, IsNil)
|
|
|
|
}
|
|
|
|
|
2013-06-15 14:18:48 +08:00
|
|
|
func (s *GoesTestSuite) TestRefreshIndex(c *C) {
|
2016-09-28 03:19:07 +08:00
|
|
|
conn := NewClient(ESHost, ESPort)
|
2013-06-15 14:18:48 +08:00
|
|
|
indexName := "testrefreshindex"
|
|
|
|
|
|
|
|
_, err := conn.CreateIndex(indexName, map[string]interface{}{})
|
|
|
|
c.Assert(err, IsNil)
|
2017-02-03 09:20:07 +08:00
|
|
|
defer conn.DeleteIndex(indexName)
|
2013-06-15 14:18:48 +08:00
|
|
|
|
2014-03-16 01:18:01 +08:00
|
|
|
_, err = conn.RefreshIndex(indexName)
|
2013-06-15 14:18:48 +08:00
|
|
|
c.Assert(err, IsNil)
|
|
|
|
|
|
|
|
_, err = conn.DeleteIndex(indexName)
|
|
|
|
c.Assert(err, IsNil)
|
|
|
|
}
|
|
|
|
|
2014-11-21 18:06:24 +08:00
|
|
|
func (s *GoesTestSuite) TestOptimize(c *C) {
|
2016-09-28 03:19:07 +08:00
|
|
|
conn := NewClient(ESHost, ESPort)
|
2014-11-21 18:06:24 +08:00
|
|
|
indexName := "testoptimize"
|
|
|
|
|
|
|
|
conn.DeleteIndex(indexName)
|
|
|
|
_, err := conn.CreateIndex(indexName, map[string]interface{}{})
|
|
|
|
c.Assert(err, IsNil)
|
2017-02-03 09:20:07 +08:00
|
|
|
defer conn.DeleteIndex(indexName)
|
2014-11-21 18:06:24 +08:00
|
|
|
|
|
|
|
// we must wait for a bit otherwise ES crashes
|
|
|
|
time.Sleep(1 * time.Second)
|
|
|
|
|
2015-01-27 09:43:50 +08:00
|
|
|
response, err := conn.Optimize([]string{indexName}, url.Values{"max_num_segments": []string{"1"}})
|
2014-11-21 18:06:24 +08:00
|
|
|
c.Assert(err, IsNil)
|
|
|
|
|
|
|
|
c.Assert(response.All.Indices[indexName].Primaries["docs"].Count, Equals, 0)
|
|
|
|
|
|
|
|
_, err = conn.DeleteIndex(indexName)
|
|
|
|
c.Assert(err, IsNil)
|
|
|
|
}
|
|
|
|
|
2013-06-15 14:18:48 +08:00
|
|
|
func (s *GoesTestSuite) TestBulkSend(c *C) {
|
|
|
|
indexName := "testbulkadd"
|
|
|
|
docType := "tweet"
|
|
|
|
|
|
|
|
tweets := []Document{
|
2014-08-18 04:26:17 +08:00
|
|
|
{
|
2016-09-28 03:19:07 +08:00
|
|
|
ID: "123",
|
2014-07-03 20:28:37 +08:00
|
|
|
Index: indexName,
|
2013-06-15 14:18:48 +08:00
|
|
|
Type: docType,
|
2016-09-28 03:19:07 +08:00
|
|
|
BulkCommand: BulkCommandIndex,
|
2013-06-15 14:18:48 +08:00
|
|
|
Fields: map[string]interface{}{
|
|
|
|
"user": "foo",
|
|
|
|
"message": "some foo message",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
|
2014-08-18 04:26:17 +08:00
|
|
|
{
|
2016-09-28 03:19:07 +08:00
|
|
|
ID: nil,
|
2013-06-15 14:18:48 +08:00
|
|
|
Index: indexName,
|
|
|
|
Type: docType,
|
2016-09-28 03:19:07 +08:00
|
|
|
BulkCommand: BulkCommandIndex,
|
2013-06-15 14:18:48 +08:00
|
|
|
Fields: map[string]interface{}{
|
|
|
|
"user": "bar",
|
|
|
|
"message": "some bar message",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2016-09-28 03:19:07 +08:00
|
|
|
conn := NewClient(ESHost, ESPort)
|
2013-06-15 14:18:48 +08:00
|
|
|
|
2015-02-27 22:48:17 +08:00
|
|
|
conn.DeleteIndex(indexName)
|
2013-06-15 14:18:48 +08:00
|
|
|
_, err := conn.CreateIndex(indexName, nil)
|
|
|
|
c.Assert(err, IsNil)
|
2017-02-03 09:20:07 +08:00
|
|
|
defer conn.DeleteIndex(indexName)
|
2013-06-15 14:18:48 +08:00
|
|
|
|
2014-07-03 20:28:37 +08:00
|
|
|
response, err := conn.BulkSend(tweets)
|
2013-06-15 14:18:48 +08:00
|
|
|
i := Item{
|
2016-09-28 03:19:07 +08:00
|
|
|
ID: "123",
|
2013-06-15 14:18:48 +08:00
|
|
|
Type: docType,
|
|
|
|
Version: 1,
|
|
|
|
Index: indexName,
|
2014-10-13 15:43:58 +08:00
|
|
|
Status: 201, //201 for indexing ( https://issues.apache.org/jira/browse/CONNECTORS-634 )
|
2013-06-15 14:18:48 +08:00
|
|
|
}
|
2016-09-28 03:19:07 +08:00
|
|
|
c.Assert(response.Items[0][BulkCommandIndex], Equals, i)
|
2013-06-15 14:18:48 +08:00
|
|
|
c.Assert(err, IsNil)
|
|
|
|
|
|
|
|
_, err = conn.RefreshIndex(indexName)
|
|
|
|
c.Assert(err, IsNil)
|
|
|
|
|
|
|
|
var query = map[string]interface{}{
|
|
|
|
"query": map[string]interface{}{
|
|
|
|
"match_all": map[string]interface{}{},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2014-05-07 20:57:37 +08:00
|
|
|
searchResults, err := conn.Search(query, []string{indexName}, []string{}, url.Values{})
|
2013-06-15 14:18:48 +08:00
|
|
|
c.Assert(err, IsNil)
|
|
|
|
|
|
|
|
var expectedTotal uint64 = 2
|
|
|
|
c.Assert(searchResults.Hits.Total, Equals, expectedTotal)
|
|
|
|
|
2016-09-28 03:19:07 +08:00
|
|
|
extraDocID := ""
|
2013-06-15 14:18:48 +08:00
|
|
|
checked := 0
|
|
|
|
for _, hit := range searchResults.Hits.Hits {
|
|
|
|
if hit.Source["user"] == "foo" {
|
2016-09-28 03:19:07 +08:00
|
|
|
c.Assert(hit.ID, Equals, "123")
|
2013-06-15 14:18:48 +08:00
|
|
|
checked++
|
|
|
|
}
|
|
|
|
|
|
|
|
if hit.Source["user"] == "bar" {
|
2016-09-28 03:19:07 +08:00
|
|
|
c.Assert(len(hit.ID) > 0, Equals, true)
|
|
|
|
extraDocID = hit.ID
|
2013-06-15 14:18:48 +08:00
|
|
|
checked++
|
|
|
|
}
|
|
|
|
}
|
|
|
|
c.Assert(checked, Equals, 2)
|
|
|
|
|
|
|
|
docToDelete := []Document{
|
2014-08-18 04:26:17 +08:00
|
|
|
{
|
2016-09-28 03:19:07 +08:00
|
|
|
ID: "123",
|
2013-06-15 14:18:48 +08:00
|
|
|
Index: indexName,
|
|
|
|
Type: docType,
|
2016-09-28 03:19:07 +08:00
|
|
|
BulkCommand: BulkCommandDelete,
|
2013-06-15 14:18:48 +08:00
|
|
|
},
|
2014-08-18 04:26:17 +08:00
|
|
|
{
|
2016-09-28 03:19:07 +08:00
|
|
|
ID: extraDocID,
|
2013-06-15 14:18:48 +08:00
|
|
|
Index: indexName,
|
|
|
|
Type: docType,
|
2016-09-28 03:19:07 +08:00
|
|
|
BulkCommand: BulkCommandDelete,
|
2013-06-15 14:18:48 +08:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2014-07-03 20:28:37 +08:00
|
|
|
response, err = conn.BulkSend(docToDelete)
|
2013-06-15 14:18:48 +08:00
|
|
|
i = Item{
|
2016-09-28 03:19:07 +08:00
|
|
|
ID: "123",
|
2013-06-15 14:18:48 +08:00
|
|
|
Type: docType,
|
|
|
|
Version: 2,
|
|
|
|
Index: indexName,
|
2014-10-13 15:43:58 +08:00
|
|
|
Status: 200, //200 for updates
|
2013-06-15 14:18:48 +08:00
|
|
|
}
|
2016-09-28 03:19:07 +08:00
|
|
|
c.Assert(response.Items[0][BulkCommandDelete], Equals, i)
|
2013-06-15 14:18:48 +08:00
|
|
|
|
|
|
|
c.Assert(err, IsNil)
|
|
|
|
|
|
|
|
_, err = conn.RefreshIndex(indexName)
|
|
|
|
c.Assert(err, IsNil)
|
|
|
|
|
2014-05-07 20:57:37 +08:00
|
|
|
searchResults, err = conn.Search(query, []string{indexName}, []string{}, url.Values{})
|
2013-06-15 14:18:48 +08:00
|
|
|
c.Assert(err, IsNil)
|
|
|
|
|
|
|
|
expectedTotal = 0
|
|
|
|
c.Assert(searchResults.Hits.Total, Equals, expectedTotal)
|
|
|
|
|
|
|
|
_, err = conn.DeleteIndex(indexName)
|
|
|
|
c.Assert(err, IsNil)
|
|
|
|
}
|
|
|
|
|
2013-06-18 12:54:31 +08:00
|
|
|
func (s *GoesTestSuite) TestStats(c *C) {
|
2016-09-28 03:19:07 +08:00
|
|
|
conn := NewClient(ESHost, ESPort)
|
2013-06-18 14:04:52 +08:00
|
|
|
indexName := "teststats"
|
2013-06-15 14:18:48 +08:00
|
|
|
|
2013-06-18 14:04:52 +08:00
|
|
|
conn.DeleteIndex(indexName)
|
2013-06-15 14:18:48 +08:00
|
|
|
_, err := conn.CreateIndex(indexName, map[string]interface{}{})
|
|
|
|
c.Assert(err, IsNil)
|
2017-02-03 09:20:07 +08:00
|
|
|
defer conn.DeleteIndex(indexName)
|
2013-06-15 14:18:48 +08:00
|
|
|
|
|
|
|
// we must wait for a bit otherwise ES crashes
|
|
|
|
time.Sleep(1 * time.Second)
|
|
|
|
|
2013-06-18 14:04:52 +08:00
|
|
|
response, err := conn.Stats([]string{indexName}, url.Values{})
|
2013-06-15 14:18:48 +08:00
|
|
|
c.Assert(err, IsNil)
|
|
|
|
|
|
|
|
c.Assert(response.All.Indices[indexName].Primaries["docs"].Count, Equals, 0)
|
|
|
|
|
|
|
|
_, err = conn.DeleteIndex(indexName)
|
|
|
|
c.Assert(err, IsNil)
|
|
|
|
}
|
|
|
|
|
2014-08-18 04:25:25 +08:00
|
|
|
func (s *GoesTestSuite) TestIndexWithFieldsInStruct(c *C) {
|
|
|
|
indexName := "testindexwithfieldsinstruct"
|
|
|
|
docType := "tweet"
|
2016-09-28 03:19:07 +08:00
|
|
|
docID := "1234"
|
2014-08-18 04:25:25 +08:00
|
|
|
|
2016-09-28 03:19:07 +08:00
|
|
|
conn := NewClient(ESHost, ESPort)
|
2014-08-18 04:25:25 +08:00
|
|
|
// just in case
|
|
|
|
conn.DeleteIndex(indexName)
|
|
|
|
|
|
|
|
_, err := conn.CreateIndex(indexName, map[string]interface{}{})
|
|
|
|
c.Assert(err, IsNil)
|
|
|
|
defer conn.DeleteIndex(indexName)
|
|
|
|
|
|
|
|
d := Document{
|
|
|
|
Index: indexName,
|
|
|
|
Type: docType,
|
2016-09-28 03:19:07 +08:00
|
|
|
ID: docID,
|
2014-08-18 04:25:25 +08:00
|
|
|
Fields: struct {
|
|
|
|
user string
|
|
|
|
message string
|
|
|
|
}{
|
|
|
|
"foo",
|
|
|
|
"bar",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2017-02-03 09:09:04 +08:00
|
|
|
response, err := conn.Index(d, nil)
|
2014-08-18 04:25:25 +08:00
|
|
|
c.Assert(err, IsNil)
|
|
|
|
|
2015-06-24 04:05:28 +08:00
|
|
|
expectedResponse := &Response{
|
2015-07-15 00:29:04 +08:00
|
|
|
Status: 201,
|
2014-08-18 04:25:25 +08:00
|
|
|
Index: indexName,
|
2016-09-28 03:19:07 +08:00
|
|
|
ID: docID,
|
2014-08-18 04:25:25 +08:00
|
|
|
Type: docType,
|
|
|
|
Version: 1,
|
|
|
|
}
|
|
|
|
|
2014-11-13 16:21:27 +08:00
|
|
|
response.Raw = nil
|
2017-02-03 09:09:04 +08:00
|
|
|
response.Shards = Shard{}
|
2014-08-18 04:25:25 +08:00
|
|
|
c.Assert(response, DeepEquals, expectedResponse)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *GoesTestSuite) TestIndexWithFieldsNotInMapOrStruct(c *C) {
|
|
|
|
indexName := "testindexwithfieldsnotinmaporstruct"
|
|
|
|
docType := "tweet"
|
2016-09-28 03:19:07 +08:00
|
|
|
docID := "1234"
|
2014-08-18 04:25:25 +08:00
|
|
|
|
2016-09-28 03:19:07 +08:00
|
|
|
conn := NewClient(ESHost, ESPort)
|
2014-08-18 04:25:25 +08:00
|
|
|
// just in case
|
|
|
|
conn.DeleteIndex(indexName)
|
|
|
|
|
|
|
|
_, err := conn.CreateIndex(indexName, map[string]interface{}{})
|
|
|
|
c.Assert(err, IsNil)
|
|
|
|
defer conn.DeleteIndex(indexName)
|
|
|
|
|
|
|
|
d := Document{
|
|
|
|
Index: indexName,
|
|
|
|
Type: docType,
|
2016-09-28 03:19:07 +08:00
|
|
|
ID: docID,
|
2014-08-18 04:25:25 +08:00
|
|
|
Fields: "test",
|
|
|
|
}
|
|
|
|
|
2017-02-03 09:09:04 +08:00
|
|
|
_, err = conn.Index(d, nil)
|
2014-08-18 04:25:25 +08:00
|
|
|
c.Assert(err, Not(IsNil))
|
|
|
|
}
|
|
|
|
|
2013-06-15 14:18:48 +08:00
|
|
|
func (s *GoesTestSuite) TestIndexIdDefined(c *C) {
|
|
|
|
indexName := "testindexiddefined"
|
|
|
|
docType := "tweet"
|
2016-09-28 03:19:07 +08:00
|
|
|
docID := "1234"
|
2013-06-15 14:18:48 +08:00
|
|
|
|
2016-09-28 03:19:07 +08:00
|
|
|
conn := NewClient(ESHost, ESPort)
|
2013-06-15 14:18:48 +08:00
|
|
|
// just in case
|
|
|
|
conn.DeleteIndex(indexName)
|
|
|
|
|
|
|
|
_, err := conn.CreateIndex(indexName, map[string]interface{}{})
|
|
|
|
c.Assert(err, IsNil)
|
|
|
|
defer conn.DeleteIndex(indexName)
|
|
|
|
|
|
|
|
d := Document{
|
|
|
|
Index: indexName,
|
|
|
|
Type: docType,
|
2016-09-28 03:19:07 +08:00
|
|
|
ID: docID,
|
2013-06-15 14:18:48 +08:00
|
|
|
Fields: map[string]interface{}{
|
|
|
|
"user": "foo",
|
|
|
|
"message": "bar",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2017-02-03 08:54:29 +08:00
|
|
|
response, err := conn.Index(d, nil)
|
2013-06-15 14:18:48 +08:00
|
|
|
c.Assert(err, IsNil)
|
|
|
|
|
2015-06-24 04:05:28 +08:00
|
|
|
expectedResponse := &Response{
|
2015-07-15 00:29:04 +08:00
|
|
|
Status: 201,
|
2013-06-15 14:18:48 +08:00
|
|
|
Index: indexName,
|
2016-09-28 03:19:07 +08:00
|
|
|
ID: docID,
|
2013-06-15 14:18:48 +08:00
|
|
|
Type: docType,
|
|
|
|
Version: 1,
|
|
|
|
}
|
|
|
|
|
2014-11-13 16:21:27 +08:00
|
|
|
response.Raw = nil
|
2017-02-03 08:54:29 +08:00
|
|
|
response.Shards = Shard{}
|
2013-06-15 14:18:48 +08:00
|
|
|
c.Assert(response, DeepEquals, expectedResponse)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *GoesTestSuite) TestIndexIdNotDefined(c *C) {
|
|
|
|
indexName := "testindexidnotdefined"
|
|
|
|
docType := "tweet"
|
|
|
|
|
2016-09-28 03:19:07 +08:00
|
|
|
conn := NewClient(ESHost, ESPort)
|
2013-06-15 14:18:48 +08:00
|
|
|
// just in case
|
|
|
|
conn.DeleteIndex(indexName)
|
|
|
|
|
|
|
|
_, err := conn.CreateIndex(indexName, map[string]interface{}{})
|
|
|
|
c.Assert(err, IsNil)
|
|
|
|
defer conn.DeleteIndex(indexName)
|
|
|
|
|
|
|
|
d := Document{
|
|
|
|
Index: indexName,
|
|
|
|
Type: docType,
|
|
|
|
Fields: map[string]interface{}{
|
|
|
|
"user": "foo",
|
|
|
|
"message": "bar",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
response, err := conn.Index(d, url.Values{})
|
|
|
|
c.Assert(err, IsNil)
|
|
|
|
|
|
|
|
c.Assert(response.Index, Equals, indexName)
|
|
|
|
c.Assert(response.Type, Equals, docType)
|
|
|
|
c.Assert(response.Version, Equals, 1)
|
2016-09-28 03:19:07 +08:00
|
|
|
c.Assert(response.ID != "", Equals, true)
|
2013-06-15 14:18:48 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s *GoesTestSuite) TestDelete(c *C) {
|
|
|
|
indexName := "testdelete"
|
|
|
|
docType := "tweet"
|
2016-09-28 03:19:07 +08:00
|
|
|
docID := "1234"
|
2013-06-15 14:18:48 +08:00
|
|
|
|
2016-09-28 03:19:07 +08:00
|
|
|
conn := NewClient(ESHost, ESPort)
|
2013-06-15 14:18:48 +08:00
|
|
|
// just in case
|
|
|
|
conn.DeleteIndex(indexName)
|
|
|
|
|
|
|
|
_, err := conn.CreateIndex(indexName, map[string]interface{}{})
|
|
|
|
c.Assert(err, IsNil)
|
|
|
|
defer conn.DeleteIndex(indexName)
|
|
|
|
|
|
|
|
d := Document{
|
|
|
|
Index: indexName,
|
|
|
|
Type: docType,
|
2016-09-28 03:19:07 +08:00
|
|
|
ID: docID,
|
2013-06-15 14:18:48 +08:00
|
|
|
Fields: map[string]interface{}{
|
|
|
|
"user": "foo",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
_, err = conn.Index(d, url.Values{})
|
|
|
|
c.Assert(err, IsNil)
|
|
|
|
|
|
|
|
response, err := conn.Delete(d, url.Values{})
|
|
|
|
c.Assert(err, IsNil)
|
|
|
|
|
2015-06-24 04:05:28 +08:00
|
|
|
expectedResponse := &Response{
|
2015-07-15 00:29:04 +08:00
|
|
|
Status: 200,
|
|
|
|
Found: true,
|
|
|
|
Index: indexName,
|
|
|
|
Type: docType,
|
2016-09-28 03:19:07 +08:00
|
|
|
ID: docID,
|
2013-06-15 14:18:48 +08:00
|
|
|
// XXX : even after a DELETE the version number seems to be incremented
|
|
|
|
Version: 2,
|
|
|
|
}
|
2014-11-13 16:21:27 +08:00
|
|
|
response.Raw = nil
|
2017-02-03 08:03:38 +08:00
|
|
|
response.Shards = Shard{}
|
2013-06-15 14:18:48 +08:00
|
|
|
c.Assert(response, DeepEquals, expectedResponse)
|
|
|
|
|
|
|
|
response, err = conn.Delete(d, url.Values{})
|
|
|
|
c.Assert(err, IsNil)
|
|
|
|
|
2015-06-24 04:05:28 +08:00
|
|
|
expectedResponse = &Response{
|
2015-07-15 00:29:04 +08:00
|
|
|
Status: 404,
|
|
|
|
Found: false,
|
|
|
|
Index: indexName,
|
|
|
|
Type: docType,
|
2016-09-28 03:19:07 +08:00
|
|
|
ID: docID,
|
2013-06-15 14:18:48 +08:00
|
|
|
// XXX : even after a DELETE the version number seems to be incremented
|
|
|
|
Version: 3,
|
|
|
|
}
|
2014-11-13 16:21:27 +08:00
|
|
|
response.Raw = nil
|
2017-02-03 08:03:38 +08:00
|
|
|
response.Shards = Shard{}
|
2013-06-15 14:18:48 +08:00
|
|
|
c.Assert(response, DeepEquals, expectedResponse)
|
|
|
|
}
|
|
|
|
|
2014-09-12 04:49:57 +08:00
|
|
|
func (s *GoesTestSuite) TestDeleteByQuery(c *C) {
|
|
|
|
indexName := "testdeletebyquery"
|
|
|
|
docType := "tweet"
|
2016-09-28 03:19:07 +08:00
|
|
|
docID := "1234"
|
2014-09-12 04:49:57 +08:00
|
|
|
|
2016-09-28 03:19:07 +08:00
|
|
|
conn := NewClient(ESHost, ESPort)
|
2017-02-03 08:11:37 +08:00
|
|
|
version, _ := conn.Version()
|
|
|
|
|
2014-09-12 04:49:57 +08:00
|
|
|
// just in case
|
|
|
|
conn.DeleteIndex(indexName)
|
|
|
|
|
|
|
|
_, err := conn.CreateIndex(indexName, map[string]interface{}{})
|
|
|
|
c.Assert(err, IsNil)
|
|
|
|
defer conn.DeleteIndex(indexName)
|
|
|
|
|
|
|
|
d := Document{
|
|
|
|
Index: indexName,
|
|
|
|
Type: docType,
|
2016-09-28 03:19:07 +08:00
|
|
|
ID: docID,
|
2014-09-12 04:49:57 +08:00
|
|
|
Fields: map[string]interface{}{
|
|
|
|
"user": "foo",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
_, err = conn.Index(d, url.Values{})
|
|
|
|
c.Assert(err, IsNil)
|
|
|
|
|
|
|
|
_, err = conn.RefreshIndex(indexName)
|
|
|
|
c.Assert(err, IsNil)
|
|
|
|
|
|
|
|
query := map[string]interface{}{
|
|
|
|
"query": map[string]interface{}{
|
|
|
|
"bool": map[string]interface{}{
|
|
|
|
"must": []map[string]interface{}{
|
|
|
|
{
|
|
|
|
"match_all": map[string]interface{}{},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
//should be 1 doc before delete by query
|
|
|
|
response, err := conn.Search(query, []string{indexName}, []string{docType}, url.Values{})
|
|
|
|
c.Assert(err, IsNil)
|
|
|
|
c.Assert(response.Hits.Total, Equals, uint64(1))
|
|
|
|
|
2017-02-03 08:11:37 +08:00
|
|
|
response, err = conn.DeleteByQuery(query, []string{indexName}, []string{docType}, url.Values{})
|
|
|
|
|
|
|
|
// There's no delete by query in ES 2.x
|
|
|
|
if version > "2" && version < "5" {
|
|
|
|
c.Assert(err, ErrorMatches, ".* does not support delete by query")
|
|
|
|
return
|
|
|
|
}
|
2014-09-12 04:49:57 +08:00
|
|
|
|
|
|
|
c.Assert(err, IsNil)
|
|
|
|
|
2015-06-24 04:05:28 +08:00
|
|
|
expectedResponse := &Response{
|
2015-07-15 00:29:04 +08:00
|
|
|
Status: 200,
|
2014-09-12 04:49:57 +08:00
|
|
|
Found: false,
|
|
|
|
Index: "",
|
|
|
|
Type: "",
|
2016-09-28 03:19:07 +08:00
|
|
|
ID: "",
|
2014-09-12 04:49:57 +08:00
|
|
|
Version: 0,
|
|
|
|
}
|
2014-11-13 16:21:27 +08:00
|
|
|
response.Raw = nil
|
2017-02-03 08:03:38 +08:00
|
|
|
response.Shards = Shard{}
|
2017-02-03 08:11:37 +08:00
|
|
|
response.Took = 0
|
2014-09-12 04:49:57 +08:00
|
|
|
c.Assert(response, DeepEquals, expectedResponse)
|
|
|
|
|
2017-02-03 08:11:37 +08:00
|
|
|
_, err = conn.RefreshIndex(indexName)
|
|
|
|
c.Assert(err, IsNil)
|
|
|
|
|
2014-09-12 04:49:57 +08:00
|
|
|
//should be 0 docs after delete by query
|
|
|
|
response, err = conn.Search(query, []string{indexName}, []string{docType}, url.Values{})
|
|
|
|
c.Assert(err, IsNil)
|
|
|
|
c.Assert(response.Hits.Total, Equals, uint64(0))
|
|
|
|
}
|
|
|
|
|
2013-06-15 14:18:48 +08:00
|
|
|
func (s *GoesTestSuite) TestGet(c *C) {
|
|
|
|
indexName := "testget"
|
|
|
|
docType := "tweet"
|
2016-09-28 03:19:07 +08:00
|
|
|
docID := "111"
|
2013-06-15 14:18:48 +08:00
|
|
|
source := map[string]interface{}{
|
|
|
|
"f1": "foo",
|
|
|
|
"f2": "foo",
|
|
|
|
}
|
|
|
|
|
2016-09-28 03:19:07 +08:00
|
|
|
conn := NewClient(ESHost, ESPort)
|
2017-02-03 08:44:49 +08:00
|
|
|
version, _ := conn.Version()
|
2013-06-15 14:18:48 +08:00
|
|
|
conn.DeleteIndex(indexName)
|
|
|
|
|
|
|
|
_, err := conn.CreateIndex(indexName, map[string]interface{}{})
|
|
|
|
c.Assert(err, IsNil)
|
|
|
|
defer conn.DeleteIndex(indexName)
|
|
|
|
|
|
|
|
d := Document{
|
|
|
|
Index: indexName,
|
|
|
|
Type: docType,
|
2016-09-28 03:19:07 +08:00
|
|
|
ID: docID,
|
2013-06-15 14:18:48 +08:00
|
|
|
Fields: source,
|
|
|
|
}
|
|
|
|
|
|
|
|
_, err = conn.Index(d, url.Values{})
|
|
|
|
c.Assert(err, IsNil)
|
|
|
|
|
2016-09-28 03:19:07 +08:00
|
|
|
response, err := conn.Get(indexName, docType, docID, url.Values{})
|
2013-06-15 14:18:48 +08:00
|
|
|
c.Assert(err, IsNil)
|
|
|
|
|
2015-06-24 04:05:28 +08:00
|
|
|
expectedResponse := &Response{
|
2015-07-15 00:29:04 +08:00
|
|
|
Status: 200,
|
2013-06-15 14:18:48 +08:00
|
|
|
Index: indexName,
|
|
|
|
Type: docType,
|
2016-09-28 03:19:07 +08:00
|
|
|
ID: docID,
|
2013-06-15 14:18:48 +08:00
|
|
|
Version: 1,
|
2014-07-03 20:28:55 +08:00
|
|
|
Found: true,
|
2013-06-15 14:18:48 +08:00
|
|
|
Source: source,
|
|
|
|
}
|
|
|
|
|
2014-11-13 16:21:27 +08:00
|
|
|
response.Raw = nil
|
2013-06-15 14:18:48 +08:00
|
|
|
c.Assert(response, DeepEquals, expectedResponse)
|
|
|
|
|
2015-06-24 04:05:28 +08:00
|
|
|
expectedResponse = &Response{
|
2015-07-15 00:29:04 +08:00
|
|
|
Status: 200,
|
2013-06-15 14:18:48 +08:00
|
|
|
Index: indexName,
|
|
|
|
Type: docType,
|
2016-09-28 03:19:07 +08:00
|
|
|
ID: docID,
|
2013-06-15 14:18:48 +08:00
|
|
|
Version: 1,
|
2014-07-03 20:28:55 +08:00
|
|
|
Found: true,
|
2013-06-15 14:18:48 +08:00
|
|
|
Fields: map[string]interface{}{
|
2014-03-16 01:18:01 +08:00
|
|
|
"f1": []interface{}{"foo"},
|
2013-06-15 14:18:48 +08:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2017-02-03 08:44:49 +08:00
|
|
|
fields := make(url.Values, 1)
|
|
|
|
// The fields param is no longer supported in ES 5.x
|
|
|
|
if version < "5" {
|
|
|
|
fields.Set("fields", "f1")
|
|
|
|
} else {
|
|
|
|
expectedResponse.Source = map[string]interface{}{"f1": "foo"}
|
|
|
|
expectedResponse.Fields = nil
|
|
|
|
fields.Set("_source", "f1")
|
|
|
|
}
|
|
|
|
response, err = conn.Get(indexName, docType, docID, fields)
|
|
|
|
c.Assert(err, IsNil)
|
|
|
|
|
2014-11-13 16:21:27 +08:00
|
|
|
response.Raw = nil
|
2013-06-15 14:18:48 +08:00
|
|
|
c.Assert(response, DeepEquals, expectedResponse)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *GoesTestSuite) TestSearch(c *C) {
|
|
|
|
indexName := "testsearch"
|
|
|
|
docType := "tweet"
|
2016-09-28 03:19:07 +08:00
|
|
|
docID := "1234"
|
2013-06-15 14:18:48 +08:00
|
|
|
source := map[string]interface{}{
|
|
|
|
"user": "foo",
|
|
|
|
"message": "bar",
|
|
|
|
}
|
|
|
|
|
2016-09-28 03:19:07 +08:00
|
|
|
conn := NewClient(ESHost, ESPort)
|
2013-06-15 14:18:48 +08:00
|
|
|
conn.DeleteIndex(indexName)
|
|
|
|
|
|
|
|
_, err := conn.CreateIndex(indexName, map[string]interface{}{})
|
|
|
|
c.Assert(err, IsNil)
|
2013-06-23 13:18:32 +08:00
|
|
|
defer conn.DeleteIndex(indexName)
|
2013-06-15 14:18:48 +08:00
|
|
|
|
|
|
|
d := Document{
|
|
|
|
Index: indexName,
|
|
|
|
Type: docType,
|
2016-09-28 03:19:07 +08:00
|
|
|
ID: docID,
|
2013-06-15 14:18:48 +08:00
|
|
|
Fields: source,
|
|
|
|
}
|
|
|
|
|
|
|
|
_, err = conn.Index(d, url.Values{})
|
|
|
|
c.Assert(err, IsNil)
|
|
|
|
|
|
|
|
_, err = conn.RefreshIndex(indexName)
|
|
|
|
c.Assert(err, IsNil)
|
|
|
|
|
|
|
|
// I can feel my eyes bleeding
|
|
|
|
query := map[string]interface{}{
|
|
|
|
"query": map[string]interface{}{
|
|
|
|
"bool": map[string]interface{}{
|
|
|
|
"must": []map[string]interface{}{
|
2014-08-18 04:26:17 +08:00
|
|
|
{
|
2013-06-15 14:18:48 +08:00
|
|
|
"match_all": map[string]interface{}{},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
2016-09-28 03:19:07 +08:00
|
|
|
response, _ := conn.Search(query, []string{indexName}, []string{docType}, url.Values{})
|
2013-06-15 14:18:48 +08:00
|
|
|
|
|
|
|
expectedHits := Hits{
|
|
|
|
Total: 1,
|
|
|
|
MaxScore: 1.0,
|
|
|
|
Hits: []Hit{
|
2014-08-18 04:26:17 +08:00
|
|
|
{
|
2013-06-15 14:18:48 +08:00
|
|
|
Index: indexName,
|
|
|
|
Type: docType,
|
2016-09-28 03:19:07 +08:00
|
|
|
ID: docID,
|
2013-06-15 14:18:48 +08:00
|
|
|
Score: 1.0,
|
|
|
|
Source: source,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
c.Assert(response.Hits, DeepEquals, expectedHits)
|
|
|
|
}
|
2013-06-23 14:59:23 +08:00
|
|
|
|
2015-01-27 09:43:50 +08:00
|
|
|
func (s *GoesTestSuite) TestCount(c *C) {
|
|
|
|
indexName := "testcount"
|
|
|
|
docType := "tweet"
|
2016-09-28 03:19:07 +08:00
|
|
|
docID := "1234"
|
2015-01-27 09:43:50 +08:00
|
|
|
source := map[string]interface{}{
|
|
|
|
"user": "foo",
|
|
|
|
"message": "bar",
|
|
|
|
}
|
|
|
|
|
2016-09-28 03:19:07 +08:00
|
|
|
conn := NewClient(ESHost, ESPort)
|
2015-01-27 09:43:50 +08:00
|
|
|
conn.DeleteIndex(indexName)
|
|
|
|
|
|
|
|
_, err := conn.CreateIndex(indexName, map[string]interface{}{})
|
|
|
|
c.Assert(err, IsNil)
|
|
|
|
defer conn.DeleteIndex(indexName)
|
|
|
|
|
|
|
|
d := Document{
|
|
|
|
Index: indexName,
|
|
|
|
Type: docType,
|
2016-09-28 03:19:07 +08:00
|
|
|
ID: docID,
|
2015-01-27 09:43:50 +08:00
|
|
|
Fields: source,
|
|
|
|
}
|
|
|
|
|
|
|
|
_, err = conn.Index(d, url.Values{})
|
|
|
|
c.Assert(err, IsNil)
|
|
|
|
|
|
|
|
_, err = conn.RefreshIndex(indexName)
|
|
|
|
c.Assert(err, IsNil)
|
|
|
|
|
|
|
|
// I can feel my eyes bleeding
|
|
|
|
query := map[string]interface{}{
|
|
|
|
"query": map[string]interface{}{
|
|
|
|
"bool": map[string]interface{}{
|
|
|
|
"must": []map[string]interface{}{
|
|
|
|
{
|
|
|
|
"match_all": map[string]interface{}{},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
2016-09-28 03:19:07 +08:00
|
|
|
response, _ := conn.Count(query, []string{indexName}, []string{docType}, url.Values{})
|
2015-01-27 09:43:50 +08:00
|
|
|
|
|
|
|
c.Assert(response.Count, Equals, 1)
|
|
|
|
}
|
|
|
|
|
2013-06-23 14:59:23 +08:00
|
|
|
func (s *GoesTestSuite) TestIndexStatus(c *C) {
|
|
|
|
indexName := "testindexstatus"
|
2016-09-28 03:19:07 +08:00
|
|
|
conn := NewClient(ESHost, ESPort)
|
2017-02-03 09:09:19 +08:00
|
|
|
|
|
|
|
// _status endpoint was removed in ES 2.0
|
|
|
|
if version, _ := conn.Version(); version > "2" {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2013-06-23 14:59:23 +08:00
|
|
|
conn.DeleteIndex(indexName)
|
|
|
|
|
|
|
|
mapping := map[string]interface{}{
|
|
|
|
"settings": map[string]interface{}{
|
|
|
|
"index.number_of_shards": 1,
|
|
|
|
"index.number_of_replicas": 1,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
_, err := conn.CreateIndex(indexName, mapping)
|
|
|
|
c.Assert(err, IsNil)
|
|
|
|
defer conn.DeleteIndex(indexName)
|
|
|
|
|
|
|
|
// gives ES some time to do its job
|
|
|
|
time.Sleep(1 * time.Second)
|
2015-06-24 03:21:03 +08:00
|
|
|
_, err = conn.RefreshIndex(indexName)
|
|
|
|
c.Assert(err, IsNil)
|
2013-06-23 14:59:23 +08:00
|
|
|
|
2014-09-27 02:55:47 +08:00
|
|
|
response, err := conn.IndexStatus([]string{"testindexstatus"})
|
2013-06-23 14:59:23 +08:00
|
|
|
c.Assert(err, IsNil)
|
|
|
|
|
|
|
|
expectedShards := Shard{Total: 2, Successful: 1, Failed: 0}
|
|
|
|
c.Assert(response.Shards, Equals, expectedShards)
|
|
|
|
|
2014-07-02 02:48:10 +08:00
|
|
|
primarySizeInBytes := response.Indices[indexName].Index["primary_size_in_bytes"].(float64)
|
|
|
|
sizeInBytes := response.Indices[indexName].Index["size_in_bytes"].(float64)
|
2015-06-24 23:32:01 +08:00
|
|
|
refreshTotal := response.Indices[indexName].Refresh["total"].(float64)
|
2014-07-02 02:48:10 +08:00
|
|
|
|
|
|
|
c.Assert(primarySizeInBytes > 0, Equals, true)
|
|
|
|
c.Assert(sizeInBytes > 0, Equals, true)
|
2015-06-24 23:32:01 +08:00
|
|
|
c.Assert(refreshTotal > 0, Equals, true)
|
2014-07-02 02:48:10 +08:00
|
|
|
|
2013-06-23 14:59:23 +08:00
|
|
|
expectedIndices := map[string]IndexStatus{
|
2014-08-18 04:26:17 +08:00
|
|
|
indexName: {
|
2013-06-23 14:59:23 +08:00
|
|
|
Index: map[string]interface{}{
|
2014-07-02 02:48:10 +08:00
|
|
|
"primary_size_in_bytes": primarySizeInBytes,
|
|
|
|
"size_in_bytes": sizeInBytes,
|
2013-06-23 14:59:23 +08:00
|
|
|
},
|
|
|
|
Translog: map[string]uint64{
|
|
|
|
"operations": 0,
|
|
|
|
},
|
|
|
|
Docs: map[string]uint64{
|
|
|
|
"num_docs": 0,
|
|
|
|
"max_doc": 0,
|
|
|
|
"deleted_docs": 0,
|
|
|
|
},
|
|
|
|
Merges: map[string]interface{}{
|
|
|
|
"current": float64(0),
|
|
|
|
"current_docs": float64(0),
|
|
|
|
"current_size_in_bytes": float64(0),
|
|
|
|
"total": float64(0),
|
|
|
|
"total_time_in_millis": float64(0),
|
|
|
|
"total_docs": float64(0),
|
|
|
|
"total_size_in_bytes": float64(0),
|
|
|
|
},
|
|
|
|
Refresh: map[string]interface{}{
|
2015-06-24 23:32:01 +08:00
|
|
|
"total": refreshTotal,
|
2013-06-23 14:59:23 +08:00
|
|
|
"total_time_in_millis": float64(0),
|
|
|
|
},
|
|
|
|
Flush: map[string]interface{}{
|
|
|
|
"total": float64(0),
|
|
|
|
"total_time_in_millis": float64(0),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
c.Assert(response.Indices, DeepEquals, expectedIndices)
|
|
|
|
}
|
2014-01-10 21:12:33 +08:00
|
|
|
|
|
|
|
func (s *GoesTestSuite) TestScroll(c *C) {
|
|
|
|
indexName := "testscroll"
|
|
|
|
docType := "tweet"
|
|
|
|
|
|
|
|
tweets := []Document{
|
2014-08-18 04:26:17 +08:00
|
|
|
{
|
2016-09-28 03:19:07 +08:00
|
|
|
ID: nil,
|
2014-01-10 21:12:33 +08:00
|
|
|
Index: indexName,
|
|
|
|
Type: docType,
|
2016-09-28 03:19:07 +08:00
|
|
|
BulkCommand: BulkCommandIndex,
|
2014-01-10 21:12:33 +08:00
|
|
|
Fields: map[string]interface{}{
|
|
|
|
"user": "foo",
|
|
|
|
"message": "some foo message",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
|
2014-08-18 04:26:17 +08:00
|
|
|
{
|
2016-09-28 03:19:07 +08:00
|
|
|
ID: nil,
|
2014-01-10 21:12:33 +08:00
|
|
|
Index: indexName,
|
|
|
|
Type: docType,
|
2016-09-28 03:19:07 +08:00
|
|
|
BulkCommand: BulkCommandIndex,
|
2014-01-10 21:12:33 +08:00
|
|
|
Fields: map[string]interface{}{
|
|
|
|
"user": "bar",
|
|
|
|
"message": "some bar message",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
|
2014-08-18 04:26:17 +08:00
|
|
|
{
|
2016-09-28 03:19:07 +08:00
|
|
|
ID: nil,
|
2014-01-10 21:12:33 +08:00
|
|
|
Index: indexName,
|
|
|
|
Type: docType,
|
2016-09-28 03:19:07 +08:00
|
|
|
BulkCommand: BulkCommandIndex,
|
2014-01-10 21:12:33 +08:00
|
|
|
Fields: map[string]interface{}{
|
|
|
|
"user": "foo",
|
|
|
|
"message": "another foo message",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2016-09-28 03:19:07 +08:00
|
|
|
conn := NewClient(ESHost, ESPort)
|
2014-01-10 21:12:33 +08:00
|
|
|
|
|
|
|
mapping := map[string]interface{}{
|
|
|
|
"settings": map[string]interface{}{
|
|
|
|
"index.number_of_shards": 1,
|
|
|
|
"index.number_of_replicas": 0,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
defer conn.DeleteIndex(indexName)
|
|
|
|
|
|
|
|
_, err := conn.CreateIndex(indexName, mapping)
|
|
|
|
c.Assert(err, IsNil)
|
|
|
|
|
2014-07-03 20:28:37 +08:00
|
|
|
_, err = conn.BulkSend(tweets)
|
2014-01-10 21:12:33 +08:00
|
|
|
c.Assert(err, IsNil)
|
|
|
|
|
|
|
|
_, err = conn.RefreshIndex(indexName)
|
|
|
|
c.Assert(err, IsNil)
|
|
|
|
|
2017-02-03 01:01:27 +08:00
|
|
|
var query map[string]interface{}
|
2017-02-03 03:04:45 +08:00
|
|
|
version, _ := conn.Version()
|
|
|
|
if version > "5" {
|
2017-02-03 01:01:27 +08:00
|
|
|
query = map[string]interface{}{
|
|
|
|
"query": map[string]interface{}{
|
|
|
|
"bool": map[string]interface{}{
|
|
|
|
"filter": map[string]interface{}{
|
|
|
|
"term": map[string]interface{}{
|
|
|
|
"user": "foo",
|
|
|
|
},
|
2014-01-10 21:12:33 +08:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2017-02-03 01:01:27 +08:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
query = map[string]interface{}{
|
|
|
|
"query": map[string]interface{}{
|
|
|
|
"filtered": map[string]interface{}{
|
|
|
|
"filter": map[string]interface{}{
|
|
|
|
"term": map[string]interface{}{
|
|
|
|
"user": "foo",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
2014-01-10 21:12:33 +08:00
|
|
|
}
|
|
|
|
|
2017-02-03 03:04:45 +08:00
|
|
|
searchResults, err := conn.Scan(query, []string{indexName}, []string{docType}, "1m", 1)
|
2014-01-10 21:12:33 +08:00
|
|
|
c.Assert(err, IsNil)
|
2017-02-03 03:04:45 +08:00
|
|
|
c.Assert(len(searchResults.ScrollID) > 0, Equals, true)
|
2014-01-10 21:12:33 +08:00
|
|
|
|
2017-02-03 03:04:45 +08:00
|
|
|
// Versions < 5.x don't include results in the initial response
|
|
|
|
if version < "5" {
|
|
|
|
searchResults, err = conn.Scroll(searchResults.ScrollID, "1m")
|
|
|
|
c.Assert(err, IsNil)
|
|
|
|
}
|
2014-01-10 21:12:33 +08:00
|
|
|
|
|
|
|
// some data in first chunk
|
|
|
|
c.Assert(searchResults.Hits.Total, Equals, uint64(2))
|
2016-09-28 03:19:07 +08:00
|
|
|
c.Assert(len(searchResults.ScrollID) > 0, Equals, true)
|
2014-01-10 21:12:33 +08:00
|
|
|
c.Assert(len(searchResults.Hits.Hits), Equals, 1)
|
|
|
|
|
2016-09-28 03:19:07 +08:00
|
|
|
searchResults, err = conn.Scroll(searchResults.ScrollID, "1m")
|
2014-01-10 21:12:33 +08:00
|
|
|
c.Assert(err, IsNil)
|
|
|
|
|
|
|
|
// more data in second chunk
|
|
|
|
c.Assert(searchResults.Hits.Total, Equals, uint64(2))
|
2016-09-28 03:19:07 +08:00
|
|
|
c.Assert(len(searchResults.ScrollID) > 0, Equals, true)
|
2014-01-10 21:12:33 +08:00
|
|
|
c.Assert(len(searchResults.Hits.Hits), Equals, 1)
|
|
|
|
|
2016-09-28 03:19:07 +08:00
|
|
|
searchResults, err = conn.Scroll(searchResults.ScrollID, "1m")
|
2014-01-10 21:12:33 +08:00
|
|
|
c.Assert(err, IsNil)
|
|
|
|
|
|
|
|
// nothing in third chunk
|
|
|
|
c.Assert(searchResults.Hits.Total, Equals, uint64(2))
|
2016-09-28 03:19:07 +08:00
|
|
|
c.Assert(len(searchResults.ScrollID) > 0, Equals, true)
|
2014-01-10 21:12:33 +08:00
|
|
|
c.Assert(len(searchResults.Hits.Hits), Equals, 0)
|
|
|
|
}
|
2014-03-16 02:02:11 +08:00
|
|
|
|
|
|
|
func (s *GoesTestSuite) TestAggregations(c *C) {
|
|
|
|
indexName := "testaggs"
|
|
|
|
docType := "tweet"
|
|
|
|
|
|
|
|
tweets := []Document{
|
2014-08-18 04:26:17 +08:00
|
|
|
{
|
2016-09-28 03:19:07 +08:00
|
|
|
ID: nil,
|
2014-03-16 02:02:11 +08:00
|
|
|
Index: indexName,
|
|
|
|
Type: docType,
|
2016-09-28 03:19:07 +08:00
|
|
|
BulkCommand: BulkCommandIndex,
|
2014-03-16 02:02:11 +08:00
|
|
|
Fields: map[string]interface{}{
|
2014-07-03 20:28:55 +08:00
|
|
|
"user": "foo",
|
|
|
|
"message": "some foo message",
|
|
|
|
"age": 25,
|
2014-03-16 02:02:11 +08:00
|
|
|
},
|
|
|
|
},
|
|
|
|
|
2014-08-18 04:26:17 +08:00
|
|
|
{
|
2016-09-28 03:19:07 +08:00
|
|
|
ID: nil,
|
2014-03-16 02:02:11 +08:00
|
|
|
Index: indexName,
|
|
|
|
Type: docType,
|
2016-09-28 03:19:07 +08:00
|
|
|
BulkCommand: BulkCommandIndex,
|
2014-03-16 02:02:11 +08:00
|
|
|
Fields: map[string]interface{}{
|
2014-07-03 20:28:55 +08:00
|
|
|
"user": "bar",
|
|
|
|
"message": "some bar message",
|
|
|
|
"age": 30,
|
2014-03-16 02:02:11 +08:00
|
|
|
},
|
|
|
|
},
|
|
|
|
|
2014-08-18 04:26:17 +08:00
|
|
|
{
|
2016-09-28 03:19:07 +08:00
|
|
|
ID: nil,
|
2014-03-16 02:02:11 +08:00
|
|
|
Index: indexName,
|
|
|
|
Type: docType,
|
2016-09-28 03:19:07 +08:00
|
|
|
BulkCommand: BulkCommandIndex,
|
2014-03-16 02:02:11 +08:00
|
|
|
Fields: map[string]interface{}{
|
2014-07-03 20:28:55 +08:00
|
|
|
"user": "foo",
|
|
|
|
"message": "another foo message",
|
2014-03-16 02:02:11 +08:00
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2016-09-28 03:19:07 +08:00
|
|
|
conn := NewClient(ESHost, ESPort)
|
2014-03-16 02:02:11 +08:00
|
|
|
|
|
|
|
mapping := map[string]interface{}{
|
|
|
|
"settings": map[string]interface{}{
|
|
|
|
"index.number_of_shards": 1,
|
|
|
|
"index.number_of_replicas": 0,
|
|
|
|
},
|
2017-02-03 04:50:25 +08:00
|
|
|
"mappings": map[string]interface{}{
|
|
|
|
docType: map[string]interface{}{
|
|
|
|
"properties": map[string]interface{}{
|
|
|
|
"user": map[string]interface{}{
|
|
|
|
"type": "string",
|
|
|
|
"index": "not_analyzed",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2014-03-16 02:02:11 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
defer conn.DeleteIndex(indexName)
|
|
|
|
|
|
|
|
_, err := conn.CreateIndex(indexName, mapping)
|
|
|
|
c.Assert(err, IsNil)
|
|
|
|
|
2014-07-03 20:28:37 +08:00
|
|
|
_, err = conn.BulkSend(tweets)
|
2014-03-16 02:02:11 +08:00
|
|
|
c.Assert(err, IsNil)
|
|
|
|
|
|
|
|
_, err = conn.RefreshIndex(indexName)
|
|
|
|
c.Assert(err, IsNil)
|
|
|
|
|
|
|
|
query := map[string]interface{}{
|
|
|
|
"aggs": map[string]interface{}{
|
|
|
|
"user": map[string]interface{}{
|
|
|
|
"terms": map[string]interface{}{
|
|
|
|
"field": "user",
|
|
|
|
"order": map[string]interface{}{
|
|
|
|
"_term": "asc",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
"aggs": map[string]interface{}{
|
|
|
|
"age": map[string]interface{}{
|
|
|
|
"stats": map[string]interface{}{
|
|
|
|
"field": "age",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
"age": map[string]interface{}{
|
|
|
|
"stats": map[string]interface{}{
|
|
|
|
"field": "age",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2016-09-28 03:19:07 +08:00
|
|
|
resp, _ := conn.Search(query, []string{indexName}, []string{docType}, url.Values{})
|
2014-03-16 02:02:11 +08:00
|
|
|
|
|
|
|
user, ok := resp.Aggregations["user"]
|
|
|
|
c.Assert(ok, Equals, true)
|
|
|
|
|
|
|
|
c.Assert(len(user.Buckets()), Equals, 2)
|
|
|
|
c.Assert(user.Buckets()[0].Key(), Equals, "bar")
|
|
|
|
c.Assert(user.Buckets()[1].Key(), Equals, "foo")
|
|
|
|
|
|
|
|
barAge := user.Buckets()[0].Aggregation("age")
|
|
|
|
c.Assert(barAge["count"], Equals, 1.0)
|
|
|
|
c.Assert(barAge["sum"], Equals, 30.0)
|
|
|
|
|
|
|
|
fooAge := user.Buckets()[1].Aggregation("age")
|
|
|
|
c.Assert(fooAge["count"], Equals, 1.0)
|
|
|
|
c.Assert(fooAge["sum"], Equals, 25.0)
|
|
|
|
|
|
|
|
age, ok := resp.Aggregations["age"]
|
|
|
|
c.Assert(ok, Equals, true)
|
|
|
|
|
|
|
|
c.Assert(age["count"], Equals, 2.0)
|
2014-07-03 20:28:55 +08:00
|
|
|
c.Assert(age["sum"], Equals, 25.0+30.0)
|
2014-03-16 02:02:11 +08:00
|
|
|
}
|
2014-11-11 21:37:34 +08:00
|
|
|
|
|
|
|
func (s *GoesTestSuite) TestPutMapping(c *C) {
|
|
|
|
indexName := "testputmapping"
|
|
|
|
docType := "tweet"
|
|
|
|
|
2016-09-28 03:19:07 +08:00
|
|
|
conn := NewClient(ESHost, ESPort)
|
2014-11-11 21:37:34 +08:00
|
|
|
// just in case
|
|
|
|
conn.DeleteIndex(indexName)
|
|
|
|
|
|
|
|
_, err := conn.CreateIndex(indexName, map[string]interface{}{})
|
|
|
|
c.Assert(err, IsNil)
|
|
|
|
defer conn.DeleteIndex(indexName)
|
|
|
|
|
|
|
|
d := Document{
|
|
|
|
Index: indexName,
|
|
|
|
Type: docType,
|
|
|
|
Fields: map[string]interface{}{
|
|
|
|
"user": "foo",
|
|
|
|
"message": "bar",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
response, err := conn.Index(d, url.Values{})
|
|
|
|
c.Assert(err, IsNil)
|
|
|
|
|
|
|
|
mapping := map[string]interface{}{
|
|
|
|
"tweet": map[string]interface{}{
|
|
|
|
"properties": map[string]interface{}{
|
|
|
|
"count": map[string]interface{}{
|
|
|
|
"type": "integer",
|
|
|
|
"index": "not_analyzed",
|
|
|
|
"store": true,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
2014-11-17 14:25:48 +08:00
|
|
|
response, err = conn.PutMapping("tweet", mapping, []string{indexName})
|
2014-11-11 21:37:34 +08:00
|
|
|
c.Assert(err, IsNil)
|
|
|
|
|
|
|
|
c.Assert(response.Acknowledged, Equals, true)
|
|
|
|
c.Assert(response.TimedOut, Equals, false)
|
|
|
|
}
|
2014-11-11 22:37:50 +08:00
|
|
|
|
|
|
|
func (s *GoesTestSuite) TestIndicesExist(c *C) {
|
2014-11-17 14:25:48 +08:00
|
|
|
indices := []string{"testindicesexist"}
|
2014-11-11 22:37:50 +08:00
|
|
|
|
2016-09-28 03:19:07 +08:00
|
|
|
conn := NewClient(ESHost, ESPort)
|
2014-11-11 22:37:50 +08:00
|
|
|
// just in case
|
2014-11-17 14:25:48 +08:00
|
|
|
conn.DeleteIndex(indices[0])
|
2014-11-11 22:37:50 +08:00
|
|
|
|
2014-11-17 14:25:48 +08:00
|
|
|
exists, err := conn.IndicesExist(indices)
|
2014-11-11 22:37:50 +08:00
|
|
|
c.Assert(exists, Equals, false)
|
|
|
|
|
2014-11-17 14:25:48 +08:00
|
|
|
_, err = conn.CreateIndex(indices[0], map[string]interface{}{})
|
2014-11-11 22:37:50 +08:00
|
|
|
c.Assert(err, IsNil)
|
2014-11-17 14:25:48 +08:00
|
|
|
defer conn.DeleteIndex(indices[0])
|
2014-11-16 06:08:40 +08:00
|
|
|
time.Sleep(200 * time.Millisecond)
|
2014-11-11 22:37:50 +08:00
|
|
|
|
2016-09-28 03:19:07 +08:00
|
|
|
exists, _ = conn.IndicesExist(indices)
|
2014-11-11 22:37:50 +08:00
|
|
|
c.Assert(exists, Equals, true)
|
|
|
|
|
2014-11-17 14:25:48 +08:00
|
|
|
indices = append(indices, "nonexistent")
|
2016-09-28 03:19:07 +08:00
|
|
|
exists, _ = conn.IndicesExist(indices)
|
2014-11-11 22:37:50 +08:00
|
|
|
c.Assert(exists, Equals, false)
|
|
|
|
}
|
2014-11-13 00:35:37 +08:00
|
|
|
|
|
|
|
func (s *GoesTestSuite) TestUpdate(c *C) {
|
|
|
|
indexName := "testupdate"
|
|
|
|
docType := "tweet"
|
2016-09-28 03:19:07 +08:00
|
|
|
docID := "1234"
|
2014-11-13 00:35:37 +08:00
|
|
|
|
2016-09-28 03:19:07 +08:00
|
|
|
conn := NewClient(ESHost, ESPort)
|
2014-11-13 00:35:37 +08:00
|
|
|
// just in case
|
|
|
|
conn.DeleteIndex(indexName)
|
|
|
|
|
|
|
|
_, err := conn.CreateIndex(indexName, map[string]interface{}{})
|
|
|
|
c.Assert(err, IsNil)
|
|
|
|
defer conn.DeleteIndex(indexName)
|
|
|
|
|
|
|
|
d := Document{
|
|
|
|
Index: indexName,
|
|
|
|
Type: docType,
|
2016-09-28 03:19:07 +08:00
|
|
|
ID: docID,
|
2014-11-13 00:35:37 +08:00
|
|
|
Fields: map[string]interface{}{
|
|
|
|
"user": "foo",
|
|
|
|
"message": "bar",
|
|
|
|
"counter": 1,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
extraArgs := make(url.Values, 1)
|
|
|
|
response, err := conn.Index(d, extraArgs)
|
|
|
|
c.Assert(err, IsNil)
|
2014-11-16 06:08:40 +08:00
|
|
|
time.Sleep(200 * time.Millisecond)
|
2014-11-13 00:35:37 +08:00
|
|
|
|
2015-06-24 04:05:28 +08:00
|
|
|
expectedResponse := &Response{
|
2015-07-15 00:29:04 +08:00
|
|
|
Status: 201,
|
2014-11-13 00:35:37 +08:00
|
|
|
Index: indexName,
|
2016-09-28 03:19:07 +08:00
|
|
|
ID: docID,
|
2014-11-13 00:35:37 +08:00
|
|
|
Type: docType,
|
|
|
|
Version: 1,
|
|
|
|
}
|
|
|
|
|
2014-11-13 16:21:27 +08:00
|
|
|
response.Raw = nil
|
2017-02-02 06:59:43 +08:00
|
|
|
response.Shards.Successful = 0
|
|
|
|
response.Shards.Total = 0
|
2014-11-13 00:35:37 +08:00
|
|
|
c.Assert(response, DeepEquals, expectedResponse)
|
|
|
|
|
|
|
|
// Now that we have an ordinary document indexed, try updating it
|
2017-02-03 01:01:27 +08:00
|
|
|
var query map[string]interface{}
|
2017-02-03 01:54:24 +08:00
|
|
|
if version, _ := conn.Version(); version > "5" {
|
2017-02-03 01:01:27 +08:00
|
|
|
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",
|
|
|
|
"lang": "groovy",
|
|
|
|
"params": map[string]interface{}{
|
|
|
|
"count": 5,
|
|
|
|
},
|
|
|
|
"upsert": map[string]interface{}{
|
|
|
|
"message": "candybar",
|
|
|
|
"user": "admin",
|
|
|
|
"counter": 1,
|
|
|
|
},
|
|
|
|
}
|
2014-11-13 00:35:37 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
response, err = conn.Update(d, query, extraArgs)
|
2017-02-03 09:22:12 +08:00
|
|
|
|
2015-06-25 02:56:01 +08:00
|
|
|
if err != nil && strings.Contains(err.(*SearchError).Msg, "dynamic scripting") {
|
2014-11-13 00:35:37 +08:00
|
|
|
c.Skip("Scripting is disabled on server, skipping this test")
|
|
|
|
return
|
|
|
|
}
|
2014-11-16 06:08:40 +08:00
|
|
|
time.Sleep(200 * time.Millisecond)
|
2014-11-13 00:35:37 +08:00
|
|
|
|
|
|
|
c.Assert(err, Equals, nil)
|
|
|
|
|
2016-09-28 03:19:07 +08:00
|
|
|
response, err = conn.Get(indexName, docType, docID, url.Values{})
|
2014-11-13 00:35:37 +08:00
|
|
|
c.Assert(err, Equals, nil)
|
|
|
|
c.Assert(response.Source["counter"], Equals, float64(6))
|
|
|
|
c.Assert(response.Source["user"], Equals, "foo")
|
|
|
|
c.Assert(response.Source["message"], Equals, "bar")
|
|
|
|
|
|
|
|
// Test another document, non-existent
|
2016-09-28 03:19:07 +08:00
|
|
|
docID = "555"
|
|
|
|
d.ID = docID
|
2014-11-13 00:35:37 +08:00
|
|
|
response, err = conn.Update(d, query, extraArgs)
|
|
|
|
c.Assert(err, Equals, nil)
|
2014-11-16 06:08:40 +08:00
|
|
|
time.Sleep(200 * time.Millisecond)
|
2014-11-13 00:35:37 +08:00
|
|
|
|
2016-09-28 03:19:07 +08:00
|
|
|
response, err = conn.Get(indexName, docType, docID, url.Values{})
|
2014-11-13 00:35:37 +08:00
|
|
|
c.Assert(err, Equals, nil)
|
|
|
|
c.Assert(response.Source["user"], Equals, "admin")
|
|
|
|
c.Assert(response.Source["message"], Equals, "candybar")
|
|
|
|
}
|
2014-11-13 16:22:11 +08:00
|
|
|
|
|
|
|
func (s *GoesTestSuite) TestGetMapping(c *C) {
|
|
|
|
indexName := "testmapping"
|
|
|
|
docType := "tweet"
|
|
|
|
|
2016-09-28 03:19:07 +08:00
|
|
|
conn := NewClient(ESHost, ESPort)
|
2014-11-13 16:22:11 +08:00
|
|
|
// just in case
|
|
|
|
conn.DeleteIndex(indexName)
|
|
|
|
|
|
|
|
_, err := conn.CreateIndex(indexName, map[string]interface{}{})
|
|
|
|
c.Assert(err, IsNil)
|
|
|
|
defer conn.DeleteIndex(indexName)
|
|
|
|
|
2014-11-16 06:08:40 +08:00
|
|
|
time.Sleep(300 * time.Millisecond)
|
|
|
|
|
2014-11-17 14:25:48 +08:00
|
|
|
response, err := conn.GetMapping([]string{docType}, []string{indexName})
|
2014-11-13 16:22:11 +08:00
|
|
|
c.Assert(err, Equals, nil)
|
|
|
|
c.Assert(len(response.Raw), Equals, 0)
|
|
|
|
|
|
|
|
d := Document{
|
|
|
|
Index: indexName,
|
|
|
|
Type: docType,
|
|
|
|
Fields: map[string]interface{}{
|
|
|
|
"user": "foo",
|
|
|
|
"message": "bar",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
response, err = conn.Index(d, url.Values{})
|
|
|
|
c.Assert(err, IsNil)
|
2014-11-16 06:08:40 +08:00
|
|
|
time.Sleep(200 * time.Millisecond)
|
2014-11-13 16:22:11 +08:00
|
|
|
|
2014-11-17 14:25:48 +08:00
|
|
|
response, err = conn.GetMapping([]string{docType}, []string{indexName})
|
2014-11-13 16:22:11 +08:00
|
|
|
c.Assert(err, Equals, nil)
|
|
|
|
c.Assert(len(response.Raw), Not(Equals), 0)
|
|
|
|
}
|
2014-11-25 08:41:35 +08:00
|
|
|
|
|
|
|
func (s *GoesTestSuite) TestDeleteMapping(c *C) {
|
|
|
|
indexName := "testdeletemapping"
|
|
|
|
docType := "tweet"
|
|
|
|
|
2016-09-28 03:19:07 +08:00
|
|
|
conn := NewClient(ESHost, ESPort)
|
2014-11-25 08:41:35 +08:00
|
|
|
// just in case
|
|
|
|
conn.DeleteIndex(indexName)
|
|
|
|
|
|
|
|
_, err := conn.CreateIndex(indexName, map[string]interface{}{})
|
|
|
|
c.Assert(err, IsNil)
|
|
|
|
defer conn.DeleteIndex(indexName)
|
|
|
|
|
|
|
|
d := Document{
|
|
|
|
Index: indexName,
|
|
|
|
Type: docType,
|
|
|
|
Fields: map[string]interface{}{
|
|
|
|
"user": "foo",
|
|
|
|
"message": "bar",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
response, err := conn.Index(d, url.Values{})
|
|
|
|
c.Assert(err, IsNil)
|
|
|
|
|
|
|
|
mapping := map[string]interface{}{
|
|
|
|
"tweet": map[string]interface{}{
|
|
|
|
"properties": map[string]interface{}{
|
|
|
|
"count": map[string]interface{}{
|
|
|
|
"type": "integer",
|
|
|
|
"index": "not_analyzed",
|
|
|
|
"store": true,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
response, err = conn.PutMapping("tweet", mapping, []string{indexName})
|
|
|
|
c.Assert(err, IsNil)
|
|
|
|
time.Sleep(200 * time.Millisecond)
|
|
|
|
|
|
|
|
response, err = conn.DeleteMapping("tweet", []string{indexName})
|
2017-02-03 08:28:09 +08:00
|
|
|
if version, _ := conn.Version(); version > "2" {
|
|
|
|
c.Assert(err, ErrorMatches, ".*not supported.*")
|
|
|
|
return
|
|
|
|
}
|
2014-11-25 08:41:35 +08:00
|
|
|
c.Assert(err, IsNil)
|
|
|
|
|
|
|
|
c.Assert(response.Acknowledged, Equals, true)
|
|
|
|
c.Assert(response.TimedOut, Equals, false)
|
|
|
|
}
|
2015-02-27 22:32:56 +08:00
|
|
|
|
|
|
|
func (s *GoesTestSuite) TestAddAlias(c *C) {
|
|
|
|
aliasName := "testAlias"
|
|
|
|
indexName := "testalias_1"
|
|
|
|
docType := "testDoc"
|
2016-09-28 03:19:07 +08:00
|
|
|
docID := "1234"
|
2015-02-27 22:32:56 +08:00
|
|
|
source := map[string]interface{}{
|
|
|
|
"user": "foo",
|
|
|
|
"message": "bar",
|
|
|
|
}
|
|
|
|
|
2016-09-28 03:19:07 +08:00
|
|
|
conn := NewClient(ESHost, ESPort)
|
2015-02-27 22:32:56 +08:00
|
|
|
defer conn.DeleteIndex(indexName)
|
|
|
|
|
|
|
|
_, err := conn.CreateIndex(indexName, map[string]interface{}{})
|
|
|
|
c.Assert(err, IsNil)
|
|
|
|
defer conn.DeleteIndex(indexName)
|
|
|
|
|
|
|
|
d := Document{
|
|
|
|
Index: indexName,
|
|
|
|
Type: docType,
|
2016-09-28 03:19:07 +08:00
|
|
|
ID: docID,
|
2015-02-27 22:32:56 +08:00
|
|
|
Fields: source,
|
|
|
|
}
|
|
|
|
|
|
|
|
// Index data
|
|
|
|
_, err = conn.Index(d, url.Values{})
|
|
|
|
c.Assert(err, IsNil)
|
|
|
|
|
|
|
|
// Add alias
|
|
|
|
_, err = conn.AddAlias(aliasName, []string{indexName})
|
|
|
|
c.Assert(err, IsNil)
|
|
|
|
|
|
|
|
// Get document via alias
|
2016-09-28 03:19:07 +08:00
|
|
|
response, err := conn.Get(aliasName, docType, docID, url.Values{})
|
2015-02-27 22:32:56 +08:00
|
|
|
c.Assert(err, IsNil)
|
|
|
|
|
2015-06-24 04:05:28 +08:00
|
|
|
expectedResponse := &Response{
|
2015-07-15 00:29:04 +08:00
|
|
|
Status: 200,
|
2015-02-27 22:32:56 +08:00
|
|
|
Index: indexName,
|
|
|
|
Type: docType,
|
2016-09-28 03:19:07 +08:00
|
|
|
ID: docID,
|
2015-02-27 22:32:56 +08:00
|
|
|
Version: 1,
|
|
|
|
Found: true,
|
|
|
|
Source: source,
|
|
|
|
}
|
|
|
|
|
|
|
|
response.Raw = nil
|
|
|
|
c.Assert(response, DeepEquals, expectedResponse)
|
|
|
|
}
|
2015-02-27 22:48:00 +08:00
|
|
|
|
|
|
|
func (s *GoesTestSuite) TestRemoveAlias(c *C) {
|
|
|
|
aliasName := "testAlias"
|
|
|
|
indexName := "testalias_1"
|
|
|
|
docType := "testDoc"
|
2016-09-28 03:19:07 +08:00
|
|
|
docID := "1234"
|
2015-02-27 22:48:00 +08:00
|
|
|
source := map[string]interface{}{
|
|
|
|
"user": "foo",
|
|
|
|
"message": "bar",
|
|
|
|
}
|
|
|
|
|
2016-09-28 03:19:07 +08:00
|
|
|
conn := NewClient(ESHost, ESPort)
|
2015-02-27 22:48:00 +08:00
|
|
|
defer conn.DeleteIndex(indexName)
|
|
|
|
|
|
|
|
_, err := conn.CreateIndex(indexName, map[string]interface{}{})
|
|
|
|
c.Assert(err, IsNil)
|
|
|
|
defer conn.DeleteIndex(indexName)
|
|
|
|
|
|
|
|
d := Document{
|
|
|
|
Index: indexName,
|
|
|
|
Type: docType,
|
2016-09-28 03:19:07 +08:00
|
|
|
ID: docID,
|
2015-02-27 22:48:00 +08:00
|
|
|
Fields: source,
|
|
|
|
}
|
|
|
|
|
|
|
|
// Index data
|
|
|
|
_, err = conn.Index(d, url.Values{})
|
|
|
|
c.Assert(err, IsNil)
|
|
|
|
|
|
|
|
// Add alias
|
|
|
|
_, err = conn.AddAlias(aliasName, []string{indexName})
|
|
|
|
c.Assert(err, IsNil)
|
|
|
|
|
|
|
|
// Remove alias
|
|
|
|
_, err = conn.RemoveAlias(aliasName, []string{indexName})
|
|
|
|
c.Assert(err, IsNil)
|
|
|
|
|
|
|
|
// Get document via alias
|
2016-09-28 03:19:07 +08:00
|
|
|
_, err = conn.Get(aliasName, docType, docID, url.Values{})
|
2017-02-03 09:19:51 +08:00
|
|
|
c.Assert(err.Error(), Matches, "\\[404\\] .*"+aliasName+".*")
|
2015-02-27 22:48:00 +08:00
|
|
|
}
|
2015-02-27 23:46:08 +08:00
|
|
|
|
|
|
|
func (s *GoesTestSuite) TestAliasExists(c *C) {
|
|
|
|
index := "testaliasexist_1"
|
|
|
|
alias := "testaliasexists"
|
|
|
|
|
2016-09-28 03:19:07 +08:00
|
|
|
conn := NewClient(ESHost, ESPort)
|
2015-02-27 23:46:08 +08:00
|
|
|
// just in case
|
|
|
|
conn.DeleteIndex(index)
|
|
|
|
|
|
|
|
exists, err := conn.AliasExists(alias)
|
|
|
|
c.Assert(exists, Equals, false)
|
|
|
|
|
|
|
|
_, err = conn.CreateIndex(index, map[string]interface{}{})
|
|
|
|
c.Assert(err, IsNil)
|
|
|
|
defer conn.DeleteIndex(index)
|
|
|
|
time.Sleep(200 * time.Millisecond)
|
|
|
|
|
|
|
|
_, err = conn.AddAlias(alias, []string{index})
|
|
|
|
c.Assert(err, IsNil)
|
|
|
|
time.Sleep(200 * time.Millisecond)
|
|
|
|
defer conn.RemoveAlias(alias, []string{index})
|
|
|
|
|
2016-09-28 03:19:07 +08:00
|
|
|
exists, _ = conn.AliasExists(alias)
|
2015-02-27 23:46:08 +08:00
|
|
|
c.Assert(exists, Equals, true)
|
|
|
|
}
|