Compare commits
32 Commits
travis_ble
...
master
Author | SHA1 | Date | |
---|---|---|---|
89c18a567f | |||
|
fbfb1d80a8 | ||
|
d25b7ff831 | ||
|
4b222647b4 | ||
|
3a66a8f6d3 | ||
|
42e5dfaa73 | ||
|
167e20d669 | ||
|
782ceb74a5 | ||
|
49e38a74a2 | ||
|
6c0739bec7 | ||
|
a6663ce61d | ||
|
4b35e6f0cf | ||
|
19c83bfeff | ||
|
7ee055cb7d | ||
|
28784db09c | ||
|
a9196feea3 | ||
|
22f0f3b3be | ||
|
2bb228813f | ||
|
f82254f6d3 | ||
|
63b210957a | ||
|
b04496cc3e | ||
|
6492f3a5e3 | ||
|
f9192a7ca8 | ||
|
f5716dce83 | ||
|
a1af556756 | ||
|
b684e69451 | ||
|
c2f6a74d74 | ||
|
5d13647d3c | ||
|
2715203d96 | ||
|
e89f41828e | ||
|
db565276fc | ||
|
10e4302b6c |
30
.travis.yml
30
.travis.yml
@ -1,27 +1,31 @@
|
|||||||
language: go
|
language: go
|
||||||
|
|
||||||
|
addons:
|
||||||
|
apt:
|
||||||
|
packages:
|
||||||
|
- oracle-java8-set-default
|
||||||
|
|
||||||
go:
|
go:
|
||||||
- 1.5.4
|
- 1.6.4
|
||||||
- 1.6.3
|
- 1.7.5
|
||||||
- 1.7.1
|
|
||||||
|
|
||||||
env:
|
env:
|
||||||
global:
|
global:
|
||||||
- GO15VENDOREXPERIMENT=1
|
- JAVA_HOME=/usr/lib/jvm/java-8-oracle
|
||||||
matrix:
|
matrix:
|
||||||
- ES_VERSION=1.3.4
|
- ES_VERSION=1.7.5 ES_URL=https://download.elastic.co/elasticsearch/elasticsearch/elasticsearch-1.7.5.tar.gz
|
||||||
- ES_VERSION=1.4.4
|
- ES_VERSION=2.4.4 ES_URL=https://download.elastic.co/elasticsearch/release/org/elasticsearch/distribution/tar/elasticsearch/2.4.4/elasticsearch-2.4.4.tar.gz
|
||||||
- ES_VERSION=1.5.2
|
- ES_VERSION=5.2.0 ES_URL=https://artifacts.elastic.co/downloads/elasticsearch/elasticsearch-5.2.0.tar.gz
|
||||||
- ES_VERSION=1.6.0
|
|
||||||
- ES_VERSION=1.7.0
|
|
||||||
|
|
||||||
before_script:
|
before_script:
|
||||||
|
- java -version
|
||||||
|
- echo $JAVA_HOME
|
||||||
- mkdir ${HOME}/elasticsearch
|
- mkdir ${HOME}/elasticsearch
|
||||||
- wget https://download.elastic.co/elasticsearch/elasticsearch/elasticsearch-${ES_VERSION}.tar.gz
|
- wget $ES_URL
|
||||||
- tar -xzf elasticsearch-${ES_VERSION}.tar.gz -C ${HOME}/elasticsearch
|
- tar -xzf elasticsearch-${ES_VERSION}.tar.gz -C ${HOME}/elasticsearch
|
||||||
- "echo 'script.groovy.sandbox.enabled: true' >> ${HOME}/elasticsearch/elasticsearch-${ES_VERSION}/config/elasticsearch.yml"
|
- "echo 'script.inline: true' >> ${HOME}/elasticsearch/elasticsearch-${ES_VERSION}/config/elasticsearch.yml"
|
||||||
- ${HOME}/elasticsearch/elasticsearch-${ES_VERSION}/bin/elasticsearch >/dev/null &
|
- ${HOME}/elasticsearch/elasticsearch-${ES_VERSION}/bin/elasticsearch &
|
||||||
- sleep 10 # Wait for ES to start up
|
- wget --retry-connrefused http://127.0.0.1:9200/ # Wait for ES to start up
|
||||||
|
|
||||||
install:
|
install:
|
||||||
- go get github.com/Masterminds/glide
|
- go get github.com/Masterminds/glide
|
||||||
|
2
Makefile
2
Makefile
@ -5,7 +5,7 @@ help:
|
|||||||
@echo "- watch: watch for changes and re-run tests"
|
@echo "- watch: watch for changes and re-run tests"
|
||||||
|
|
||||||
deps:
|
deps:
|
||||||
glide install && mkdir -p vendor/bin && go build -o vendor/bin/ginkgo github.com/onsi/ginkgo/ginkgo
|
glide install && mkdir -p vendor/bin && go build -o vendor/bin/ginkgo ./vendor/github.com/onsi/ginkgo/ginkgo
|
||||||
|
|
||||||
|
|
||||||
test: deps
|
test: deps
|
||||||
|
392
goes.go
392
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,93 +43,116 @@ 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{
|
||||||
Conn: c,
|
|
||||||
Query: mapping,
|
Query: mapping,
|
||||||
IndexList: []string{name},
|
IndexList: []string{name},
|
||||||
Method: "PUT",
|
Method: "PUT",
|
||||||
}
|
}
|
||||||
|
|
||||||
return r.Run()
|
return c.Do(&r)
|
||||||
}
|
}
|
||||||
|
|
||||||
// DeleteIndex deletes an index represented by a name
|
// DeleteIndex deletes an index represented by a name
|
||||||
func (c *Client) DeleteIndex(name string) (*Response, error) {
|
func (c *Client) DeleteIndex(name string) (*Response, error) {
|
||||||
r := Request{
|
r := Request{
|
||||||
Conn: c,
|
|
||||||
IndexList: []string{name},
|
IndexList: []string{name},
|
||||||
Method: "DELETE",
|
Method: "DELETE",
|
||||||
}
|
}
|
||||||
|
|
||||||
return r.Run()
|
return c.Do(&r)
|
||||||
}
|
}
|
||||||
|
|
||||||
// RefreshIndex refreshes an index represented by a name
|
// RefreshIndex refreshes an index represented by a name
|
||||||
func (c *Client) RefreshIndex(name string) (*Response, error) {
|
func (c *Client) RefreshIndex(name string) (*Response, error) {
|
||||||
r := Request{
|
r := Request{
|
||||||
Conn: c,
|
|
||||||
IndexList: []string{name},
|
IndexList: []string{name},
|
||||||
Method: "POST",
|
Method: "POST",
|
||||||
API: "_refresh",
|
API: "_refresh",
|
||||||
}
|
}
|
||||||
|
|
||||||
return r.Run()
|
return c.Do(&r)
|
||||||
}
|
}
|
||||||
|
|
||||||
// UpdateIndexSettings updates settings for existing index represented by a name and a settings
|
// UpdateIndexSettings updates settings for existing index represented by a name and a settings
|
||||||
// as described here: https://www.elastic.co/guide/en/elasticsearch/reference/current/indices-update-settings.html
|
// as described here: https://www.elastic.co/guide/en/elasticsearch/reference/current/indices-update-settings.html
|
||||||
func (c *Client) UpdateIndexSettings(name string, settings interface{}) (*Response, error) {
|
func (c *Client) UpdateIndexSettings(name string, settings interface{}) (*Response, error) {
|
||||||
r := Request{
|
r := Request{
|
||||||
Conn: c,
|
|
||||||
Query: settings,
|
Query: settings,
|
||||||
IndexList: []string{name},
|
IndexList: []string{name},
|
||||||
Method: "PUT",
|
Method: "PUT",
|
||||||
API: "_settings",
|
API: "_settings",
|
||||||
}
|
}
|
||||||
|
|
||||||
return r.Run()
|
return c.Do(&r)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Optimize an index represented by a name, extra args are also allowed please check:
|
// Optimize an index represented by a name, extra args are also allowed please check:
|
||||||
// http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/indices-optimize.html#indices-optimize
|
// http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/indices-optimize.html#indices-optimize
|
||||||
func (c *Client) Optimize(indexList []string, extraArgs url.Values) (*Response, error) {
|
func (c *Client) Optimize(indexList []string, extraArgs url.Values) (*Response, error) {
|
||||||
r := Request{
|
r := Request{
|
||||||
Conn: c,
|
|
||||||
IndexList: indexList,
|
IndexList: indexList,
|
||||||
ExtraArgs: extraArgs,
|
ExtraArgs: extraArgs,
|
||||||
Method: "POST",
|
Method: "POST",
|
||||||
API: "_optimize",
|
API: "_optimize",
|
||||||
}
|
}
|
||||||
|
if version, _ := c.Version(); version > "2.1" {
|
||||||
|
r.API = "_forcemerge"
|
||||||
|
}
|
||||||
|
|
||||||
return r.Run()
|
return c.Do(&r)
|
||||||
|
}
|
||||||
|
|
||||||
|
// ForceMerge is the same as Optimize, but matches the naming of the endpoint as of ES 2.1.0
|
||||||
|
func (c *Client) ForceMerge(indexList []string, extraArgs url.Values) (*Response, error) {
|
||||||
|
return c.Optimize(indexList, extraArgs)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Stats fetches statistics (_stats) for the current elasticsearch server
|
// Stats fetches statistics (_stats) for the current elasticsearch server
|
||||||
func (c *Client) Stats(indexList []string, extraArgs url.Values) (*Response, error) {
|
func (c *Client) Stats(indexList []string, extraArgs url.Values) (*Response, error) {
|
||||||
r := Request{
|
r := Request{
|
||||||
Conn: c,
|
|
||||||
IndexList: indexList,
|
IndexList: indexList,
|
||||||
ExtraArgs: extraArgs,
|
ExtraArgs: extraArgs,
|
||||||
Method: "GET",
|
Method: "GET",
|
||||||
API: "_stats",
|
API: "_stats",
|
||||||
}
|
}
|
||||||
|
|
||||||
return r.Run()
|
return c.Do(&r)
|
||||||
}
|
}
|
||||||
|
|
||||||
// IndexStatus fetches the status (_status) for the indices defined in
|
// IndexStatus fetches the status (_status) for the indices defined in
|
||||||
// indexList. Use _all in indexList to get stats for all indices
|
// indexList. Use _all in indexList to get stats for all indices
|
||||||
func (c *Client) IndexStatus(indexList []string) (*Response, error) {
|
func (c *Client) IndexStatus(indexList []string) (*Response, error) {
|
||||||
r := Request{
|
r := Request{
|
||||||
Conn: c,
|
|
||||||
IndexList: indexList,
|
IndexList: indexList,
|
||||||
Method: "GET",
|
Method: "GET",
|
||||||
API: "_status",
|
API: "_status",
|
||||||
}
|
}
|
||||||
|
|
||||||
return r.Run()
|
return c.Do(&r)
|
||||||
}
|
}
|
||||||
|
|
||||||
// BulkSend bulk adds multiple documents in bulk mode
|
// BulkSend bulk adds multiple documents in bulk mode
|
||||||
@ -152,7 +175,7 @@ func (c *Client) BulkSend(documents []Document) (*Response, error) {
|
|||||||
|
|
||||||
// len(documents) * 2 : action + optional_sources
|
// len(documents) * 2 : action + optional_sources
|
||||||
// + 1 : room for the trailing \n
|
// + 1 : room for the trailing \n
|
||||||
bulkData := make([][]byte, len(documents)*2+1)
|
bulkData := make([][]byte, 0, len(documents)*2+1)
|
||||||
i := 0
|
i := 0
|
||||||
|
|
||||||
for _, doc := range documents {
|
for _, doc := range documents {
|
||||||
@ -168,7 +191,7 @@ func (c *Client) BulkSend(documents []Document) (*Response, error) {
|
|||||||
return &Response{}, err
|
return &Response{}, err
|
||||||
}
|
}
|
||||||
|
|
||||||
bulkData[i] = action
|
bulkData = append(bulkData, action)
|
||||||
i++
|
i++
|
||||||
|
|
||||||
if doc.Fields != nil {
|
if doc.Fields != nil {
|
||||||
@ -194,28 +217,42 @@ func (c *Client) BulkSend(documents []Document) (*Response, error) {
|
|||||||
return &Response{}, err
|
return &Response{}, err
|
||||||
}
|
}
|
||||||
|
|
||||||
bulkData[i] = sources
|
bulkData = append(bulkData, sources)
|
||||||
i++
|
i++
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// forces an extra trailing \n absolutely necessary for elasticsearch
|
// forces an extra trailing \n absolutely necessary for elasticsearch
|
||||||
bulkData[len(bulkData)-1] = []byte(nil)
|
bulkData = append(bulkData, []byte(nil))
|
||||||
|
|
||||||
r := Request{
|
r := Request{
|
||||||
Conn: c,
|
|
||||||
Method: "POST",
|
Method: "POST",
|
||||||
API: "_bulk",
|
API: "_bulk",
|
||||||
BulkData: bytes.Join(bulkData, []byte("\n")),
|
BulkData: bytes.Join(bulkData, []byte("\n")),
|
||||||
}
|
}
|
||||||
|
|
||||||
return r.Run()
|
resp, err := c.Do(&r)
|
||||||
|
if err != nil {
|
||||||
|
return resp, err
|
||||||
|
}
|
||||||
|
|
||||||
|
if resp.Errors {
|
||||||
|
for _, item := range resp.Items {
|
||||||
|
for _, i := range item {
|
||||||
|
if i.Error != "" {
|
||||||
|
return resp, &SearchError{i.Error, i.Status}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return resp, &SearchError{Msg: "Unknown error while bulk indexing"}
|
||||||
|
}
|
||||||
|
|
||||||
|
return resp, err
|
||||||
}
|
}
|
||||||
|
|
||||||
// Search executes a search query against an index
|
// Search executes a search query against an index
|
||||||
func (c *Client) Search(query interface{}, indexList []string, typeList []string, extraArgs url.Values) (*Response, error) {
|
func (c *Client) Search(query interface{}, indexList []string, typeList []string, extraArgs url.Values) (*Response, error) {
|
||||||
r := Request{
|
r := Request{
|
||||||
Conn: c,
|
|
||||||
Query: query,
|
Query: query,
|
||||||
IndexList: indexList,
|
IndexList: indexList,
|
||||||
TypeList: typeList,
|
TypeList: typeList,
|
||||||
@ -224,13 +261,12 @@ func (c *Client) Search(query interface{}, indexList []string, typeList []string
|
|||||||
ExtraArgs: extraArgs,
|
ExtraArgs: extraArgs,
|
||||||
}
|
}
|
||||||
|
|
||||||
return r.Run()
|
return c.Do(&r)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Count executes a count query against an index, use the Count field in the response for the result
|
// Count executes a count query against an index, use the Count field in the response for the result
|
||||||
func (c *Client) Count(query interface{}, indexList []string, typeList []string, extraArgs url.Values) (*Response, error) {
|
func (c *Client) Count(query interface{}, indexList []string, typeList []string, extraArgs url.Values) (*Response, error) {
|
||||||
r := Request{
|
r := Request{
|
||||||
Conn: c,
|
|
||||||
Query: query,
|
Query: query,
|
||||||
IndexList: indexList,
|
IndexList: indexList,
|
||||||
TypeList: typeList,
|
TypeList: typeList,
|
||||||
@ -239,7 +275,7 @@ func (c *Client) Count(query interface{}, indexList []string, typeList []string,
|
|||||||
ExtraArgs: extraArgs,
|
ExtraArgs: extraArgs,
|
||||||
}
|
}
|
||||||
|
|
||||||
return r.Run()
|
return c.Do(&r)
|
||||||
}
|
}
|
||||||
|
|
||||||
//Query runs a query against an index using the provided http method.
|
//Query runs a query against an index using the provided http method.
|
||||||
@ -247,7 +283,6 @@ func (c *Client) Count(query interface{}, indexList []string, typeList []string,
|
|||||||
//for the HTTP method.
|
//for the HTTP method.
|
||||||
func (c *Client) Query(query interface{}, indexList []string, typeList []string, httpMethod string, extraArgs url.Values) (*Response, error) {
|
func (c *Client) Query(query interface{}, indexList []string, typeList []string, httpMethod string, extraArgs url.Values) (*Response, error) {
|
||||||
r := Request{
|
r := Request{
|
||||||
Conn: c,
|
|
||||||
Query: query,
|
Query: query,
|
||||||
IndexList: indexList,
|
IndexList: indexList,
|
||||||
TypeList: typeList,
|
TypeList: typeList,
|
||||||
@ -256,18 +291,56 @@ func (c *Client) Query(query interface{}, indexList []string, typeList []string,
|
|||||||
ExtraArgs: extraArgs,
|
ExtraArgs: extraArgs,
|
||||||
}
|
}
|
||||||
|
|
||||||
return r.Run()
|
return c.Do(&r)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Scan starts scroll over an index
|
// DeleteByQuery deletes documents matching the specified query. It will return an error for ES 2.x,
|
||||||
|
// because delete by query support was removed in those versions.
|
||||||
|
func (c *Client) DeleteByQuery(query interface{}, indexList []string, typeList []string, extraArgs url.Values) (*Response, error) {
|
||||||
|
version, err := c.Version()
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
if version > "2" && version < "5" {
|
||||||
|
return nil, errors.New("ElasticSearch 2.x does not support delete by query")
|
||||||
|
}
|
||||||
|
|
||||||
|
r := Request{
|
||||||
|
Query: query,
|
||||||
|
IndexList: indexList,
|
||||||
|
TypeList: typeList,
|
||||||
|
Method: "DELETE",
|
||||||
|
API: "_query",
|
||||||
|
ExtraArgs: extraArgs,
|
||||||
|
}
|
||||||
|
|
||||||
|
if version > "5" {
|
||||||
|
r.API = "_delete_by_query"
|
||||||
|
r.Method = "POST"
|
||||||
|
}
|
||||||
|
|
||||||
|
return c.Do(&r)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Scan starts scroll over an index.
|
||||||
|
// For ES versions < 5.x, it uses search_type=scan; for 5.x it uses sort=_doc. This means that data
|
||||||
|
// will be returned in the initial response for 5.x versions, but not for older versions. Code
|
||||||
|
// wishing to be compatible with both should be written to handle either case.
|
||||||
func (c *Client) Scan(query interface{}, indexList []string, typeList []string, timeout string, size int) (*Response, error) {
|
func (c *Client) Scan(query interface{}, indexList []string, typeList []string, timeout string, size int) (*Response, error) {
|
||||||
v := url.Values{}
|
v := url.Values{}
|
||||||
v.Add("search_type", "scan")
|
version, err := c.Version()
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
if version > "5" {
|
||||||
|
v.Add("sort", "_doc")
|
||||||
|
} else {
|
||||||
|
v.Add("search_type", "scan")
|
||||||
|
}
|
||||||
v.Add("scroll", timeout)
|
v.Add("scroll", timeout)
|
||||||
v.Add("size", strconv.Itoa(size))
|
v.Add("size", strconv.Itoa(size))
|
||||||
|
|
||||||
r := Request{
|
r := Request{
|
||||||
Conn: c,
|
|
||||||
Query: query,
|
Query: query,
|
||||||
IndexList: indexList,
|
IndexList: indexList,
|
||||||
TypeList: typeList,
|
TypeList: typeList,
|
||||||
@ -276,36 +349,44 @@ func (c *Client) Scan(query interface{}, indexList []string, typeList []string,
|
|||||||
ExtraArgs: v,
|
ExtraArgs: v,
|
||||||
}
|
}
|
||||||
|
|
||||||
return r.Run()
|
return c.Do(&r)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Scroll fetches data by scroll id
|
// Scroll fetches data by scroll id
|
||||||
func (c *Client) Scroll(scrollID string, timeout string) (*Response, error) {
|
func (c *Client) Scroll(scrollID string, timeout string) (*Response, error) {
|
||||||
v := url.Values{}
|
|
||||||
v.Add("scroll", timeout)
|
|
||||||
|
|
||||||
r := Request{
|
r := Request{
|
||||||
Conn: c,
|
Method: "POST",
|
||||||
Method: "POST",
|
API: "_search/scroll",
|
||||||
API: "_search/scroll",
|
|
||||||
ExtraArgs: v,
|
|
||||||
Body: []byte(scrollID),
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return r.Run()
|
if version, err := c.Version(); err != nil {
|
||||||
|
return nil, err
|
||||||
|
} else if version > "2" {
|
||||||
|
r.Body, err = json.Marshal(map[string]string{"scroll": timeout, "scroll_id": scrollID})
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
v := url.Values{}
|
||||||
|
v.Add("scroll", timeout)
|
||||||
|
v.Add("scroll_id", scrollID)
|
||||||
|
|
||||||
|
r.ExtraArgs = v
|
||||||
|
}
|
||||||
|
|
||||||
|
return c.Do(&r)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Get a typed document by its id
|
// Get a typed document by its id
|
||||||
func (c *Client) Get(index string, documentType string, id string, extraArgs url.Values) (*Response, error) {
|
func (c *Client) Get(index string, documentType string, id string, extraArgs url.Values) (*Response, error) {
|
||||||
r := Request{
|
r := Request{
|
||||||
Conn: c,
|
|
||||||
IndexList: []string{index},
|
IndexList: []string{index},
|
||||||
Method: "GET",
|
Method: "GET",
|
||||||
API: documentType + "/" + id,
|
API: documentType + "/" + id,
|
||||||
ExtraArgs: extraArgs,
|
ExtraArgs: extraArgs,
|
||||||
}
|
}
|
||||||
|
|
||||||
return r.Run()
|
return c.Do(&r)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Index indexes a Document
|
// Index indexes a Document
|
||||||
@ -313,7 +394,6 @@ func (c *Client) Get(index string, documentType string, id string, extraArgs url
|
|||||||
// URL arguments, for example, to control routing, ttl, version, op_type, etc.
|
// URL arguments, for example, to control routing, ttl, version, op_type, etc.
|
||||||
func (c *Client) Index(d Document, extraArgs url.Values) (*Response, error) {
|
func (c *Client) Index(d Document, extraArgs url.Values) (*Response, error) {
|
||||||
r := Request{
|
r := Request{
|
||||||
Conn: c,
|
|
||||||
Query: d.Fields,
|
Query: d.Fields,
|
||||||
IndexList: []string{d.Index.(string)},
|
IndexList: []string{d.Index.(string)},
|
||||||
TypeList: []string{d.Type},
|
TypeList: []string{d.Type},
|
||||||
@ -326,7 +406,7 @@ func (c *Client) Index(d Document, extraArgs url.Values) (*Response, error) {
|
|||||||
r.ID = d.ID.(string)
|
r.ID = d.ID.(string)
|
||||||
}
|
}
|
||||||
|
|
||||||
return r.Run()
|
return c.Do(&r)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Delete deletes a Document d
|
// Delete deletes a Document d
|
||||||
@ -334,7 +414,6 @@ func (c *Client) Index(d Document, extraArgs url.Values) (*Response, error) {
|
|||||||
// URL arguments, for example, to control routing.
|
// URL arguments, for example, to control routing.
|
||||||
func (c *Client) Delete(d Document, extraArgs url.Values) (*Response, error) {
|
func (c *Client) Delete(d Document, extraArgs url.Values) (*Response, error) {
|
||||||
r := Request{
|
r := Request{
|
||||||
Conn: c,
|
|
||||||
IndexList: []string{d.Index.(string)},
|
IndexList: []string{d.Index.(string)},
|
||||||
TypeList: []string{d.Type},
|
TypeList: []string{d.Type},
|
||||||
ExtraArgs: extraArgs,
|
ExtraArgs: extraArgs,
|
||||||
@ -342,117 +421,7 @@ func (c *Client) Delete(d Document, extraArgs url.Values) (*Response, error) {
|
|||||||
ID: d.ID.(string),
|
ID: d.ID.(string),
|
||||||
}
|
}
|
||||||
|
|
||||||
return r.Run()
|
return c.Do(&r)
|
||||||
}
|
|
||||||
|
|
||||||
// Run executes an elasticsearch Request. It converts data to Json, sends the
|
|
||||||
// request and returns the Response obtained
|
|
||||||
func (req *Request) Run() (*Response, error) {
|
|
||||||
body, statusCode, err := req.run()
|
|
||||||
esResp := &Response{Status: statusCode}
|
|
||||||
|
|
||||||
if err != nil {
|
|
||||||
return esResp, err
|
|
||||||
}
|
|
||||||
|
|
||||||
if req.Method != "HEAD" {
|
|
||||||
err = json.Unmarshal(body, &esResp)
|
|
||||||
if err != nil {
|
|
||||||
return esResp, err
|
|
||||||
}
|
|
||||||
err = json.Unmarshal(body, &esResp.Raw)
|
|
||||||
if err != nil {
|
|
||||||
return esResp, err
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if req.API == "_bulk" && esResp.Errors {
|
|
||||||
for _, item := range esResp.Items {
|
|
||||||
for _, i := range item {
|
|
||||||
if i.Error != "" {
|
|
||||||
return esResp, &SearchError{i.Error, i.Status}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return esResp, &SearchError{Msg: "Unknown error while bulk indexing"}
|
|
||||||
}
|
|
||||||
|
|
||||||
if esResp.Error != "" {
|
|
||||||
return esResp, &SearchError{esResp.Error, esResp.Status}
|
|
||||||
}
|
|
||||||
|
|
||||||
return esResp, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func (req *Request) run() ([]byte, uint64, error) {
|
|
||||||
postData := []byte{}
|
|
||||||
|
|
||||||
// XXX : refactor this
|
|
||||||
if len(req.Body) > 0 {
|
|
||||||
postData = req.Body
|
|
||||||
} else if req.API == "_bulk" {
|
|
||||||
postData = req.BulkData
|
|
||||||
} else {
|
|
||||||
b, err := json.Marshal(req.Query)
|
|
||||||
if err != nil {
|
|
||||||
return nil, 0, err
|
|
||||||
}
|
|
||||||
postData = b
|
|
||||||
}
|
|
||||||
|
|
||||||
reader := bytes.NewReader(postData)
|
|
||||||
|
|
||||||
newReq, err := http.NewRequest(req.Method, req.URL(), reader)
|
|
||||||
if err != nil {
|
|
||||||
return nil, 0, err
|
|
||||||
}
|
|
||||||
|
|
||||||
if req.Method == "POST" || req.Method == "PUT" {
|
|
||||||
newReq.Header.Set("Content-Type", "application/json")
|
|
||||||
}
|
|
||||||
|
|
||||||
resp, err := req.Conn.Client.Do(newReq)
|
|
||||||
if err != nil {
|
|
||||||
return nil, 0, err
|
|
||||||
}
|
|
||||||
|
|
||||||
defer resp.Body.Close()
|
|
||||||
|
|
||||||
body, err := ioutil.ReadAll(resp.Body)
|
|
||||||
if err != nil {
|
|
||||||
return nil, uint64(resp.StatusCode), err
|
|
||||||
}
|
|
||||||
|
|
||||||
if resp.StatusCode > 201 && resp.StatusCode < 400 {
|
|
||||||
return nil, uint64(resp.StatusCode), errors.New(string(body))
|
|
||||||
}
|
|
||||||
|
|
||||||
return body, uint64(resp.StatusCode), nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// URL builds a Request for a URL
|
|
||||||
func (req *Request) URL() string {
|
|
||||||
path := "/" + strings.Join(req.IndexList, ",")
|
|
||||||
|
|
||||||
if len(req.TypeList) > 0 {
|
|
||||||
path += "/" + strings.Join(req.TypeList, ",")
|
|
||||||
}
|
|
||||||
|
|
||||||
// XXX : for indexing documents using the normal (non bulk) API
|
|
||||||
if len(req.ID) > 0 {
|
|
||||||
path += "/" + req.ID
|
|
||||||
}
|
|
||||||
|
|
||||||
path += "/" + req.API
|
|
||||||
|
|
||||||
u := url.URL{
|
|
||||||
Scheme: "http",
|
|
||||||
Host: fmt.Sprintf("%s:%s", req.Conn.Host, req.Conn.Port),
|
|
||||||
Path: path,
|
|
||||||
RawQuery: req.ExtraArgs.Encode(),
|
|
||||||
}
|
|
||||||
|
|
||||||
return u.String()
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Buckets returns list of buckets in aggregation
|
// Buckets returns list of buckets in aggregation
|
||||||
@ -489,39 +458,36 @@ func (b Bucket) Aggregation(name string) Aggregation {
|
|||||||
func (c *Client) PutMapping(typeName string, mapping interface{}, indexes []string) (*Response, error) {
|
func (c *Client) PutMapping(typeName string, mapping interface{}, indexes []string) (*Response, error) {
|
||||||
|
|
||||||
r := Request{
|
r := Request{
|
||||||
Conn: c,
|
|
||||||
Query: mapping,
|
Query: mapping,
|
||||||
IndexList: indexes,
|
IndexList: indexes,
|
||||||
Method: "PUT",
|
Method: "PUT",
|
||||||
API: "_mappings/" + typeName,
|
API: "_mappings/" + typeName,
|
||||||
}
|
}
|
||||||
|
|
||||||
return r.Run()
|
return c.Do(&r)
|
||||||
}
|
}
|
||||||
|
|
||||||
// GetMapping returns the mappings for the specified types
|
// GetMapping returns the mappings for the specified types
|
||||||
func (c *Client) GetMapping(types []string, indexes []string) (*Response, error) {
|
func (c *Client) GetMapping(types []string, indexes []string) (*Response, error) {
|
||||||
|
|
||||||
r := Request{
|
r := Request{
|
||||||
Conn: c,
|
|
||||||
IndexList: indexes,
|
IndexList: indexes,
|
||||||
Method: "GET",
|
Method: "GET",
|
||||||
API: "_mapping/" + strings.Join(types, ","),
|
API: "_mapping/" + strings.Join(types, ","),
|
||||||
}
|
}
|
||||||
|
|
||||||
return r.Run()
|
return c.Do(&r)
|
||||||
}
|
}
|
||||||
|
|
||||||
// IndicesExist checks whether index (or indices) exist on the server
|
// IndicesExist checks whether index (or indices) exist on the server
|
||||||
func (c *Client) IndicesExist(indexes []string) (bool, error) {
|
func (c *Client) IndicesExist(indexes []string) (bool, error) {
|
||||||
|
|
||||||
r := Request{
|
r := Request{
|
||||||
Conn: c,
|
|
||||||
IndexList: indexes,
|
IndexList: indexes,
|
||||||
Method: "HEAD",
|
Method: "HEAD",
|
||||||
}
|
}
|
||||||
|
|
||||||
resp, err := r.Run()
|
resp, err := c.Do(&r)
|
||||||
|
|
||||||
return resp.Status == 200, err
|
return resp.Status == 200, err
|
||||||
}
|
}
|
||||||
@ -529,7 +495,6 @@ func (c *Client) IndicesExist(indexes []string) (bool, error) {
|
|||||||
// Update updates the specified document using the _update endpoint
|
// Update updates the specified document using the _update endpoint
|
||||||
func (c *Client) Update(d Document, query interface{}, extraArgs url.Values) (*Response, error) {
|
func (c *Client) Update(d Document, query interface{}, extraArgs url.Values) (*Response, error) {
|
||||||
r := Request{
|
r := Request{
|
||||||
Conn: c,
|
|
||||||
Query: query,
|
Query: query,
|
||||||
IndexList: []string{d.Index.(string)},
|
IndexList: []string{d.Index.(string)},
|
||||||
TypeList: []string{d.Type},
|
TypeList: []string{d.Type},
|
||||||
@ -542,25 +507,29 @@ func (c *Client) Update(d Document, query interface{}, extraArgs url.Values) (*R
|
|||||||
r.ID = d.ID.(string)
|
r.ID = d.ID.(string)
|
||||||
}
|
}
|
||||||
|
|
||||||
return r.Run()
|
return c.Do(&r)
|
||||||
}
|
}
|
||||||
|
|
||||||
// DeleteMapping deletes a mapping along with all data in the type
|
// DeleteMapping deletes a mapping along with all data in the type
|
||||||
func (c *Client) DeleteMapping(typeName string, indexes []string) (*Response, error) {
|
func (c *Client) DeleteMapping(typeName string, indexes []string) (*Response, error) {
|
||||||
|
if version, err := c.Version(); err != nil {
|
||||||
|
return nil, err
|
||||||
|
} else if version > "2" {
|
||||||
|
return nil, errors.New("Deletion of mappings is not supported in ES 2.x and above.")
|
||||||
|
}
|
||||||
|
|
||||||
r := Request{
|
r := Request{
|
||||||
Conn: c,
|
|
||||||
IndexList: indexes,
|
IndexList: indexes,
|
||||||
Method: "DELETE",
|
Method: "DELETE",
|
||||||
API: "_mappings/" + typeName,
|
API: "_mappings/" + typeName,
|
||||||
}
|
}
|
||||||
|
|
||||||
return r.Run()
|
return c.Do(&r)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *Client) modifyAlias(action string, alias string, indexes []string) (*Response, error) {
|
func (c *Client) modifyAlias(action string, alias string, indexes []string) (*Response, error) {
|
||||||
command := map[string]interface{}{
|
command := map[string]interface{}{
|
||||||
"actions": make([]map[string]interface{}, 1),
|
"actions": make([]map[string]interface{}, 0, 1),
|
||||||
}
|
}
|
||||||
|
|
||||||
for _, index := range indexes {
|
for _, index := range indexes {
|
||||||
@ -573,13 +542,12 @@ func (c *Client) modifyAlias(action string, alias string, indexes []string) (*Re
|
|||||||
}
|
}
|
||||||
|
|
||||||
r := Request{
|
r := Request{
|
||||||
Conn: c,
|
|
||||||
Query: command,
|
Query: command,
|
||||||
Method: "POST",
|
Method: "POST",
|
||||||
API: "_aliases",
|
API: "_aliases",
|
||||||
}
|
}
|
||||||
|
|
||||||
return r.Run()
|
return c.Do(&r)
|
||||||
}
|
}
|
||||||
|
|
||||||
// AddAlias creates an alias to one or more indexes
|
// AddAlias creates an alias to one or more indexes
|
||||||
@ -596,12 +564,94 @@ func (c *Client) RemoveAlias(alias string, indexes []string) (*Response, error)
|
|||||||
func (c *Client) AliasExists(alias string) (bool, error) {
|
func (c *Client) AliasExists(alias string) (bool, error) {
|
||||||
|
|
||||||
r := Request{
|
r := Request{
|
||||||
Conn: c,
|
|
||||||
Method: "HEAD",
|
Method: "HEAD",
|
||||||
API: "_alias/" + alias,
|
API: "_alias/" + alias,
|
||||||
}
|
}
|
||||||
|
|
||||||
resp, err := r.Run()
|
resp, err := c.Do(&r)
|
||||||
|
|
||||||
return resp.Status == 200, err
|
return resp.Status == 200, err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (c *Client) replaceHost(req *http.Request) {
|
||||||
|
req.URL.Scheme = "http"
|
||||||
|
req.URL.Host = fmt.Sprintf("%s:%s", c.Host, c.Port)
|
||||||
|
}
|
||||||
|
|
||||||
|
// DoRaw Does the provided requeset and returns the raw bytes and the status code of the response
|
||||||
|
func (c *Client) DoRaw(r Requester) ([]byte, uint64, error) {
|
||||||
|
req, err := r.Request()
|
||||||
|
if err != nil {
|
||||||
|
return nil, 0, err
|
||||||
|
}
|
||||||
|
c.replaceHost(req)
|
||||||
|
|
||||||
|
if c.AuthUsername != "" {
|
||||||
|
req.SetBasicAuth(c.AuthUsername, c.AuthPassword)
|
||||||
|
}
|
||||||
|
|
||||||
|
return c.doRequest(req)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Do runs the request returned by the requestor and returns the parsed response
|
||||||
|
func (c *Client) Do(r Requester) (*Response, error) {
|
||||||
|
req, err := r.Request()
|
||||||
|
if err != nil {
|
||||||
|
return &Response{}, err
|
||||||
|
}
|
||||||
|
c.replaceHost(req)
|
||||||
|
|
||||||
|
if c.AuthUsername != "" {
|
||||||
|
req.SetBasicAuth(c.AuthUsername, c.AuthPassword)
|
||||||
|
}
|
||||||
|
|
||||||
|
body, statusCode, err := c.doRequest(req)
|
||||||
|
esResp := &Response{Status: statusCode}
|
||||||
|
|
||||||
|
if err != nil {
|
||||||
|
return esResp, err
|
||||||
|
}
|
||||||
|
|
||||||
|
if req.Method != "HEAD" {
|
||||||
|
err = json.Unmarshal(body, &esResp)
|
||||||
|
if err != nil {
|
||||||
|
return esResp, err
|
||||||
|
}
|
||||||
|
err = json.Unmarshal(body, &esResp.Raw)
|
||||||
|
if err != nil {
|
||||||
|
return esResp, err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if len(esResp.RawError) > 0 && esResp.RawError[0] == '"' {
|
||||||
|
json.Unmarshal(esResp.RawError, &esResp.Error)
|
||||||
|
} else {
|
||||||
|
esResp.Error = string(esResp.RawError)
|
||||||
|
}
|
||||||
|
esResp.RawError = nil
|
||||||
|
|
||||||
|
if esResp.Error != "" {
|
||||||
|
return esResp, &SearchError{esResp.Error, esResp.Status}
|
||||||
|
}
|
||||||
|
|
||||||
|
return esResp, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c *Client) doRequest(req *http.Request) ([]byte, uint64, error) {
|
||||||
|
resp, err := c.Client.Do(req)
|
||||||
|
if err != nil {
|
||||||
|
return nil, 0, err
|
||||||
|
}
|
||||||
|
defer resp.Body.Close()
|
||||||
|
|
||||||
|
body, err := ioutil.ReadAll(resp.Body)
|
||||||
|
if err != nil {
|
||||||
|
return nil, uint64(resp.StatusCode), err
|
||||||
|
}
|
||||||
|
|
||||||
|
if resp.StatusCode > 201 && resp.StatusCode < 400 {
|
||||||
|
return nil, uint64(resp.StatusCode), errors.New(string(body))
|
||||||
|
}
|
||||||
|
|
||||||
|
return body, uint64(resp.StatusCode), nil
|
||||||
|
}
|
||||||
|
197
goes_test.go
197
goes_test.go
@ -5,14 +5,13 @@
|
|||||||
package goes
|
package goes
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
. "gopkg.in/check.v1"
|
||||||
"net/http"
|
"net/http"
|
||||||
"net/url"
|
"net/url"
|
||||||
"os"
|
"os"
|
||||||
"strings"
|
"strings"
|
||||||
"testing"
|
"testing"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
. "github.com/go-check/check"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
var (
|
var (
|
||||||
@ -41,7 +40,7 @@ func (s *GoesTestSuite) SetUpTest(c *C) {
|
|||||||
|
|
||||||
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) {
|
||||||
@ -54,16 +53,13 @@ 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)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *GoesTestSuite) TestUrl(c *C) {
|
func (s *GoesTestSuite) TestUrl(c *C) {
|
||||||
conn := NewClient(ESHost, ESPort)
|
|
||||||
|
|
||||||
r := Request{
|
r := Request{
|
||||||
Conn: conn,
|
|
||||||
Query: "q",
|
Query: "q",
|
||||||
IndexList: []string{"i"},
|
IndexList: []string{"i"},
|
||||||
TypeList: []string{},
|
TypeList: []string{},
|
||||||
@ -71,21 +67,21 @@ func (s *GoesTestSuite) TestUrl(c *C) {
|
|||||||
API: "_search",
|
API: "_search",
|
||||||
}
|
}
|
||||||
|
|
||||||
c.Assert(r.URL(), Equals, "http://"+ESHost+":"+ESPort+"/i/_search")
|
c.Assert(r.URL().String(), Equals, "/i/_search")
|
||||||
|
|
||||||
r.IndexList = []string{"a", "b"}
|
r.IndexList = []string{"a", "b"}
|
||||||
c.Assert(r.URL(), Equals, "http://"+ESHost+":"+ESPort+"/a,b/_search")
|
c.Assert(r.URL().String(), Equals, "/a,b/_search")
|
||||||
|
|
||||||
r.TypeList = []string{"c", "d"}
|
r.TypeList = []string{"c", "d"}
|
||||||
c.Assert(r.URL(), Equals, "http://"+ESHost+":"+ESPort+"/a,b/c,d/_search")
|
c.Assert(r.URL().String(), Equals, "/a,b/c,d/_search")
|
||||||
|
|
||||||
r.ExtraArgs = make(url.Values, 1)
|
r.ExtraArgs = make(url.Values, 1)
|
||||||
r.ExtraArgs.Set("version", "1")
|
r.ExtraArgs.Set("version", "1")
|
||||||
c.Assert(r.URL(), Equals, "http://"+ESHost+":"+ESPort+"/a,b/c,d/_search?version=1")
|
c.Assert(r.URL().String(), Equals, "/a,b/c,d/_search?version=1")
|
||||||
|
|
||||||
r.ID = "1234"
|
r.ID = "1234"
|
||||||
r.API = ""
|
r.API = ""
|
||||||
c.Assert(r.URL(), Equals, "http://"+ESHost+":"+ESPort+"/a,b/c,d/1234/?version=1")
|
c.Assert(r.URL().String(), Equals, "/a,b/c,d/1234/?version=1")
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *GoesTestSuite) TestEsDown(c *C) {
|
func (s *GoesTestSuite) TestEsDown(c *C) {
|
||||||
@ -94,13 +90,12 @@ func (s *GoesTestSuite) TestEsDown(c *C) {
|
|||||||
var query = map[string]interface{}{"query": "foo"}
|
var query = map[string]interface{}{"query": "foo"}
|
||||||
|
|
||||||
r := Request{
|
r := Request{
|
||||||
Conn: conn,
|
|
||||||
Query: query,
|
Query: query,
|
||||||
IndexList: []string{"i"},
|
IndexList: []string{"i"},
|
||||||
Method: "GET",
|
Method: "GET",
|
||||||
API: "_search",
|
API: "_search",
|
||||||
}
|
}
|
||||||
_, err := r.Run()
|
_, err := conn.Do(&r)
|
||||||
|
|
||||||
c.Assert(err, ErrorMatches, ".* no such host")
|
c.Assert(err, ErrorMatches, ".* no such host")
|
||||||
}
|
}
|
||||||
@ -111,15 +106,14 @@ func (s *GoesTestSuite) TestRunMissingIndex(c *C) {
|
|||||||
var query = map[string]interface{}{"query": "foo"}
|
var query = map[string]interface{}{"query": "foo"}
|
||||||
|
|
||||||
r := Request{
|
r := Request{
|
||||||
Conn: conn,
|
|
||||||
Query: query,
|
Query: query,
|
||||||
IndexList: []string{"i"},
|
IndexList: []string{"i"},
|
||||||
Method: "GET",
|
Method: "GET",
|
||||||
API: "_search",
|
API: "_search",
|
||||||
}
|
}
|
||||||
_, err := r.Run()
|
_, err := conn.Do(&r)
|
||||||
|
|
||||||
c.Assert(err.Error(), Equals, "[404] IndexMissingException[[i] missing]")
|
c.Assert(err.Error(), Matches, "\\[40.\\] .*i.*")
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *GoesTestSuite) TestCreateIndex(c *C) {
|
func (s *GoesTestSuite) TestCreateIndex(c *C) {
|
||||||
@ -155,9 +149,10 @@ func (s *GoesTestSuite) TestDeleteIndexInexistantIndex(c *C) {
|
|||||||
conn := NewClient(ESHost, ESPort)
|
conn := NewClient(ESHost, ESPort)
|
||||||
resp, err := conn.DeleteIndex("foobar")
|
resp, err := conn.DeleteIndex("foobar")
|
||||||
|
|
||||||
c.Assert(err.Error(), Equals, "[404] IndexMissingException[[foobar] missing]")
|
c.Assert(err.Error(), Matches, "\\[404\\] .*foobar.*")
|
||||||
resp.Raw = nil // Don't make us have to duplicate this.
|
resp.Raw = nil // Don't make us have to duplicate this.
|
||||||
c.Assert(resp, DeepEquals, &Response{Status: 404, Error: "IndexMissingException[[foobar] missing]"})
|
c.Assert(resp.Status, Equals, uint64(404))
|
||||||
|
c.Assert(resp.Error, Matches, ".*foobar.*")
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *GoesTestSuite) TestDeleteIndexExistingIndex(c *C) {
|
func (s *GoesTestSuite) TestDeleteIndexExistingIndex(c *C) {
|
||||||
@ -166,6 +161,7 @@ func (s *GoesTestSuite) TestDeleteIndexExistingIndex(c *C) {
|
|||||||
indexName := "testdeleteindexexistingindex"
|
indexName := "testdeleteindexexistingindex"
|
||||||
|
|
||||||
_, err := conn.CreateIndex(indexName, map[string]interface{}{})
|
_, err := conn.CreateIndex(indexName, map[string]interface{}{})
|
||||||
|
defer conn.DeleteIndex(indexName)
|
||||||
|
|
||||||
c.Assert(err, IsNil)
|
c.Assert(err, IsNil)
|
||||||
|
|
||||||
@ -184,8 +180,12 @@ func (s *GoesTestSuite) TestUpdateIndexSettings(c *C) {
|
|||||||
conn := NewClient(ESHost, ESPort)
|
conn := NewClient(ESHost, ESPort)
|
||||||
indexName := "testupdateindex"
|
indexName := "testupdateindex"
|
||||||
|
|
||||||
|
// Just in case
|
||||||
|
conn.DeleteIndex(indexName)
|
||||||
|
|
||||||
_, err := conn.CreateIndex(indexName, map[string]interface{}{})
|
_, err := conn.CreateIndex(indexName, map[string]interface{}{})
|
||||||
c.Assert(err, IsNil)
|
c.Assert(err, IsNil)
|
||||||
|
defer conn.DeleteIndex(indexName)
|
||||||
|
|
||||||
_, err = conn.UpdateIndexSettings(indexName, map[string]interface{}{
|
_, err = conn.UpdateIndexSettings(indexName, map[string]interface{}{
|
||||||
"index": map[string]interface{}{
|
"index": map[string]interface{}{
|
||||||
@ -204,6 +204,7 @@ func (s *GoesTestSuite) TestRefreshIndex(c *C) {
|
|||||||
|
|
||||||
_, err := conn.CreateIndex(indexName, map[string]interface{}{})
|
_, err := conn.CreateIndex(indexName, map[string]interface{}{})
|
||||||
c.Assert(err, IsNil)
|
c.Assert(err, IsNil)
|
||||||
|
defer conn.DeleteIndex(indexName)
|
||||||
|
|
||||||
_, err = conn.RefreshIndex(indexName)
|
_, err = conn.RefreshIndex(indexName)
|
||||||
c.Assert(err, IsNil)
|
c.Assert(err, IsNil)
|
||||||
@ -219,6 +220,7 @@ func (s *GoesTestSuite) TestOptimize(c *C) {
|
|||||||
conn.DeleteIndex(indexName)
|
conn.DeleteIndex(indexName)
|
||||||
_, err := conn.CreateIndex(indexName, map[string]interface{}{})
|
_, err := conn.CreateIndex(indexName, map[string]interface{}{})
|
||||||
c.Assert(err, IsNil)
|
c.Assert(err, IsNil)
|
||||||
|
defer conn.DeleteIndex(indexName)
|
||||||
|
|
||||||
// we must wait for a bit otherwise ES crashes
|
// we must wait for a bit otherwise ES crashes
|
||||||
time.Sleep(1 * time.Second)
|
time.Sleep(1 * time.Second)
|
||||||
@ -265,6 +267,7 @@ func (s *GoesTestSuite) TestBulkSend(c *C) {
|
|||||||
conn.DeleteIndex(indexName)
|
conn.DeleteIndex(indexName)
|
||||||
_, err := conn.CreateIndex(indexName, nil)
|
_, err := conn.CreateIndex(indexName, nil)
|
||||||
c.Assert(err, IsNil)
|
c.Assert(err, IsNil)
|
||||||
|
defer conn.DeleteIndex(indexName)
|
||||||
|
|
||||||
response, err := conn.BulkSend(tweets)
|
response, err := conn.BulkSend(tweets)
|
||||||
i := Item{
|
i := Item{
|
||||||
@ -355,6 +358,7 @@ func (s *GoesTestSuite) TestStats(c *C) {
|
|||||||
conn.DeleteIndex(indexName)
|
conn.DeleteIndex(indexName)
|
||||||
_, err := conn.CreateIndex(indexName, map[string]interface{}{})
|
_, err := conn.CreateIndex(indexName, map[string]interface{}{})
|
||||||
c.Assert(err, IsNil)
|
c.Assert(err, IsNil)
|
||||||
|
defer conn.DeleteIndex(indexName)
|
||||||
|
|
||||||
// we must wait for a bit otherwise ES crashes
|
// we must wait for a bit otherwise ES crashes
|
||||||
time.Sleep(1 * time.Second)
|
time.Sleep(1 * time.Second)
|
||||||
@ -394,9 +398,7 @@ func (s *GoesTestSuite) TestIndexWithFieldsInStruct(c *C) {
|
|||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
extraArgs := make(url.Values, 1)
|
response, err := conn.Index(d, nil)
|
||||||
extraArgs.Set("ttl", "86400000")
|
|
||||||
response, err := conn.Index(d, extraArgs)
|
|
||||||
c.Assert(err, IsNil)
|
c.Assert(err, IsNil)
|
||||||
|
|
||||||
expectedResponse := &Response{
|
expectedResponse := &Response{
|
||||||
@ -408,6 +410,7 @@ func (s *GoesTestSuite) TestIndexWithFieldsInStruct(c *C) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
response.Raw = nil
|
response.Raw = nil
|
||||||
|
response.Shards = Shard{}
|
||||||
c.Assert(response, DeepEquals, expectedResponse)
|
c.Assert(response, DeepEquals, expectedResponse)
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -431,9 +434,7 @@ func (s *GoesTestSuite) TestIndexWithFieldsNotInMapOrStruct(c *C) {
|
|||||||
Fields: "test",
|
Fields: "test",
|
||||||
}
|
}
|
||||||
|
|
||||||
extraArgs := make(url.Values, 1)
|
_, err = conn.Index(d, nil)
|
||||||
extraArgs.Set("ttl", "86400000")
|
|
||||||
_, err = conn.Index(d, extraArgs)
|
|
||||||
c.Assert(err, Not(IsNil))
|
c.Assert(err, Not(IsNil))
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -460,9 +461,7 @@ func (s *GoesTestSuite) TestIndexIdDefined(c *C) {
|
|||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
extraArgs := make(url.Values, 1)
|
response, err := conn.Index(d, nil)
|
||||||
extraArgs.Set("ttl", "86400000")
|
|
||||||
response, err := conn.Index(d, extraArgs)
|
|
||||||
c.Assert(err, IsNil)
|
c.Assert(err, IsNil)
|
||||||
|
|
||||||
expectedResponse := &Response{
|
expectedResponse := &Response{
|
||||||
@ -474,6 +473,7 @@ func (s *GoesTestSuite) TestIndexIdDefined(c *C) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
response.Raw = nil
|
response.Raw = nil
|
||||||
|
response.Shards = Shard{}
|
||||||
c.Assert(response, DeepEquals, expectedResponse)
|
c.Assert(response, DeepEquals, expectedResponse)
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -545,6 +545,7 @@ func (s *GoesTestSuite) TestDelete(c *C) {
|
|||||||
Version: 2,
|
Version: 2,
|
||||||
}
|
}
|
||||||
response.Raw = nil
|
response.Raw = nil
|
||||||
|
response.Shards = Shard{}
|
||||||
c.Assert(response, DeepEquals, expectedResponse)
|
c.Assert(response, DeepEquals, expectedResponse)
|
||||||
|
|
||||||
response, err = conn.Delete(d, url.Values{})
|
response, err = conn.Delete(d, url.Values{})
|
||||||
@ -560,6 +561,7 @@ func (s *GoesTestSuite) TestDelete(c *C) {
|
|||||||
Version: 3,
|
Version: 3,
|
||||||
}
|
}
|
||||||
response.Raw = nil
|
response.Raw = nil
|
||||||
|
response.Shards = Shard{}
|
||||||
c.Assert(response, DeepEquals, expectedResponse)
|
c.Assert(response, DeepEquals, expectedResponse)
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -569,6 +571,8 @@ func (s *GoesTestSuite) TestDeleteByQuery(c *C) {
|
|||||||
docID := "1234"
|
docID := "1234"
|
||||||
|
|
||||||
conn := NewClient(ESHost, ESPort)
|
conn := NewClient(ESHost, ESPort)
|
||||||
|
version, _ := conn.Version()
|
||||||
|
|
||||||
// just in case
|
// just in case
|
||||||
conn.DeleteIndex(indexName)
|
conn.DeleteIndex(indexName)
|
||||||
|
|
||||||
@ -608,7 +612,13 @@ func (s *GoesTestSuite) TestDeleteByQuery(c *C) {
|
|||||||
c.Assert(err, IsNil)
|
c.Assert(err, IsNil)
|
||||||
c.Assert(response.Hits.Total, Equals, uint64(1))
|
c.Assert(response.Hits.Total, Equals, uint64(1))
|
||||||
|
|
||||||
response, err = conn.Query(query, []string{indexName}, []string{docType}, "DELETE", url.Values{})
|
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
|
||||||
|
}
|
||||||
|
|
||||||
c.Assert(err, IsNil)
|
c.Assert(err, IsNil)
|
||||||
|
|
||||||
@ -621,8 +631,13 @@ func (s *GoesTestSuite) TestDeleteByQuery(c *C) {
|
|||||||
Version: 0,
|
Version: 0,
|
||||||
}
|
}
|
||||||
response.Raw = nil
|
response.Raw = nil
|
||||||
|
response.Shards = Shard{}
|
||||||
|
response.Took = 0
|
||||||
c.Assert(response, DeepEquals, expectedResponse)
|
c.Assert(response, DeepEquals, expectedResponse)
|
||||||
|
|
||||||
|
_, err = conn.RefreshIndex(indexName)
|
||||||
|
c.Assert(err, IsNil)
|
||||||
|
|
||||||
//should be 0 docs after delete by query
|
//should be 0 docs after delete by query
|
||||||
response, err = conn.Search(query, []string{indexName}, []string{docType}, url.Values{})
|
response, err = conn.Search(query, []string{indexName}, []string{docType}, url.Values{})
|
||||||
c.Assert(err, IsNil)
|
c.Assert(err, IsNil)
|
||||||
@ -639,6 +654,7 @@ func (s *GoesTestSuite) TestGet(c *C) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
conn := NewClient(ESHost, ESPort)
|
conn := NewClient(ESHost, ESPort)
|
||||||
|
version, _ := conn.Version()
|
||||||
conn.DeleteIndex(indexName)
|
conn.DeleteIndex(indexName)
|
||||||
|
|
||||||
_, err := conn.CreateIndex(indexName, map[string]interface{}{})
|
_, err := conn.CreateIndex(indexName, map[string]interface{}{})
|
||||||
@ -671,11 +687,6 @@ func (s *GoesTestSuite) TestGet(c *C) {
|
|||||||
response.Raw = nil
|
response.Raw = nil
|
||||||
c.Assert(response, DeepEquals, expectedResponse)
|
c.Assert(response, DeepEquals, expectedResponse)
|
||||||
|
|
||||||
fields := make(url.Values, 1)
|
|
||||||
fields.Set("fields", "f1")
|
|
||||||
response, err = conn.Get(indexName, docType, docID, fields)
|
|
||||||
c.Assert(err, IsNil)
|
|
||||||
|
|
||||||
expectedResponse = &Response{
|
expectedResponse = &Response{
|
||||||
Status: 200,
|
Status: 200,
|
||||||
Index: indexName,
|
Index: indexName,
|
||||||
@ -688,6 +699,18 @@ func (s *GoesTestSuite) TestGet(c *C) {
|
|||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
|
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)
|
||||||
|
|
||||||
response.Raw = nil
|
response.Raw = nil
|
||||||
c.Assert(response, DeepEquals, expectedResponse)
|
c.Assert(response, DeepEquals, expectedResponse)
|
||||||
}
|
}
|
||||||
@ -801,6 +824,12 @@ func (s *GoesTestSuite) TestCount(c *C) {
|
|||||||
func (s *GoesTestSuite) TestIndexStatus(c *C) {
|
func (s *GoesTestSuite) TestIndexStatus(c *C) {
|
||||||
indexName := "testindexstatus"
|
indexName := "testindexstatus"
|
||||||
conn := NewClient(ESHost, ESPort)
|
conn := NewClient(ESHost, ESPort)
|
||||||
|
|
||||||
|
// _status endpoint was removed in ES 2.0
|
||||||
|
if version, _ := conn.Version(); version > "2" {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
conn.DeleteIndex(indexName)
|
conn.DeleteIndex(indexName)
|
||||||
|
|
||||||
mapping := map[string]interface{}{
|
mapping := map[string]interface{}{
|
||||||
@ -929,24 +958,43 @@ 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{}{
|
version, _ := conn.Version()
|
||||||
"filtered": map[string]interface{}{
|
if version > "5" {
|
||||||
"filter": map[string]interface{}{
|
query = map[string]interface{}{
|
||||||
"term": map[string]interface{}{
|
"query": map[string]interface{}{
|
||||||
"user": "foo",
|
"bool": map[string]interface{}{
|
||||||
|
"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)
|
searchResults, err := conn.Scan(query, []string{indexName}, []string{docType}, "1m", 1)
|
||||||
c.Assert(err, IsNil)
|
c.Assert(err, IsNil)
|
||||||
c.Assert(len(scan.ScrollID) > 0, Equals, true)
|
c.Assert(len(searchResults.ScrollID) > 0, Equals, true)
|
||||||
|
|
||||||
searchResults, err := conn.Scroll(scan.ScrollID, "1m")
|
// Versions < 5.x don't include results in the initial response
|
||||||
c.Assert(err, IsNil)
|
if version < "5" {
|
||||||
|
searchResults, err = conn.Scroll(searchResults.ScrollID, "1m")
|
||||||
|
c.Assert(err, IsNil)
|
||||||
|
}
|
||||||
|
|
||||||
// some data in first chunk
|
// some data in first chunk
|
||||||
c.Assert(searchResults.Hits.Total, Equals, uint64(2))
|
c.Assert(searchResults.Hits.Total, Equals, uint64(2))
|
||||||
@ -1018,6 +1066,16 @@ func (s *GoesTestSuite) TestAggregations(c *C) {
|
|||||||
"index.number_of_shards": 1,
|
"index.number_of_shards": 1,
|
||||||
"index.number_of_replicas": 0,
|
"index.number_of_replicas": 0,
|
||||||
},
|
},
|
||||||
|
"mappings": map[string]interface{}{
|
||||||
|
docType: map[string]interface{}{
|
||||||
|
"properties": map[string]interface{}{
|
||||||
|
"user": map[string]interface{}{
|
||||||
|
"type": "string",
|
||||||
|
"index": "not_analyzed",
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
defer conn.DeleteIndex(indexName)
|
defer conn.DeleteIndex(indexName)
|
||||||
@ -1183,23 +1241,44 @@ func (s *GoesTestSuite) TestUpdate(c *C) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
response.Raw = nil
|
response.Raw = nil
|
||||||
|
response.Shards.Successful = 0
|
||||||
|
response.Shards.Total = 0
|
||||||
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 version, _ := conn.Version(); version > "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)
|
||||||
|
|
||||||
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
|
||||||
@ -1303,6 +1382,10 @@ func (s *GoesTestSuite) TestDeleteMapping(c *C) {
|
|||||||
time.Sleep(200 * time.Millisecond)
|
time.Sleep(200 * time.Millisecond)
|
||||||
|
|
||||||
response, err = conn.DeleteMapping("tweet", []string{indexName})
|
response, err = conn.DeleteMapping("tweet", []string{indexName})
|
||||||
|
if version, _ := conn.Version(); version > "2" {
|
||||||
|
c.Assert(err, ErrorMatches, ".*not supported.*")
|
||||||
|
return
|
||||||
|
}
|
||||||
c.Assert(err, IsNil)
|
c.Assert(err, IsNil)
|
||||||
|
|
||||||
c.Assert(response.Acknowledged, Equals, true)
|
c.Assert(response.Acknowledged, Equals, true)
|
||||||
@ -1397,7 +1480,7 @@ func (s *GoesTestSuite) TestRemoveAlias(c *C) {
|
|||||||
|
|
||||||
// Get document via alias
|
// Get document via alias
|
||||||
_, err = conn.Get(aliasName, docType, docID, url.Values{})
|
_, err = conn.Get(aliasName, docType, docID, url.Values{})
|
||||||
c.Assert(err.Error(), Equals, "[404] IndexMissingException[["+aliasName+"] missing]")
|
c.Assert(err.Error(), Matches, "\\[404\\] .*"+aliasName+".*")
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *GoesTestSuite) TestAliasExists(c *C) {
|
func (s *GoesTestSuite) TestAliasExists(c *C) {
|
||||||
|
113
request.go
Normal file
113
request.go
Normal file
@ -0,0 +1,113 @@
|
|||||||
|
package goes
|
||||||
|
|
||||||
|
import (
|
||||||
|
"bytes"
|
||||||
|
"encoding/json"
|
||||||
|
"io/ioutil"
|
||||||
|
"net/http"
|
||||||
|
"net/url"
|
||||||
|
"strings"
|
||||||
|
)
|
||||||
|
|
||||||
|
// Requester implements Request which builds an HTTP request for Elasticsearch
|
||||||
|
type Requester interface {
|
||||||
|
// Request should set the URL and Body (if needed). The host of the URL will be overwritten by the client.
|
||||||
|
Request() (*http.Request, error)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Request holds a single request to elasticsearch
|
||||||
|
type Request struct {
|
||||||
|
// A search query
|
||||||
|
Query interface{}
|
||||||
|
|
||||||
|
// Which index to search into
|
||||||
|
IndexList []string
|
||||||
|
|
||||||
|
// Which type to search into
|
||||||
|
TypeList []string
|
||||||
|
|
||||||
|
// HTTP Method to user (GET, POST ...)
|
||||||
|
Method string
|
||||||
|
|
||||||
|
// Which api keyword (_search, _bulk, etc) to use
|
||||||
|
API string
|
||||||
|
|
||||||
|
// Bulk data
|
||||||
|
BulkData []byte
|
||||||
|
|
||||||
|
// Request body
|
||||||
|
Body []byte
|
||||||
|
|
||||||
|
// A list of extra URL arguments
|
||||||
|
ExtraArgs url.Values
|
||||||
|
|
||||||
|
// Used for the id field when indexing a document
|
||||||
|
ID string
|
||||||
|
|
||||||
|
// Auth username
|
||||||
|
AuthUsername string
|
||||||
|
|
||||||
|
// Auth password
|
||||||
|
AuthPassword string
|
||||||
|
}
|
||||||
|
|
||||||
|
// URL builds a URL for a Request
|
||||||
|
func (req *Request) URL() *url.URL {
|
||||||
|
var path string
|
||||||
|
if len(req.IndexList) > 0 {
|
||||||
|
path = "/" + strings.Join(req.IndexList, ",")
|
||||||
|
}
|
||||||
|
|
||||||
|
if len(req.TypeList) > 0 {
|
||||||
|
path += "/" + strings.Join(req.TypeList, ",")
|
||||||
|
}
|
||||||
|
|
||||||
|
// XXX : for indexing documents using the normal (non bulk) API
|
||||||
|
if len(req.ID) > 0 {
|
||||||
|
path += "/" + req.ID
|
||||||
|
}
|
||||||
|
|
||||||
|
path += "/" + req.API
|
||||||
|
|
||||||
|
u := url.URL{
|
||||||
|
//Scheme: "http",
|
||||||
|
//Host: fmt.Sprintf("%s:%s", req.Conn.Host, req.Conn.Port),
|
||||||
|
Path: path,
|
||||||
|
RawQuery: req.ExtraArgs.Encode(),
|
||||||
|
}
|
||||||
|
|
||||||
|
return &u
|
||||||
|
}
|
||||||
|
|
||||||
|
// Request generates an http.Request based on the contents of the Request struct
|
||||||
|
func (req *Request) Request() (*http.Request, error) {
|
||||||
|
postData := []byte{}
|
||||||
|
|
||||||
|
// XXX : refactor this
|
||||||
|
if len(req.Body) > 0 {
|
||||||
|
postData = req.Body
|
||||||
|
} else if req.API == "_bulk" {
|
||||||
|
postData = req.BulkData
|
||||||
|
} else if req.Query != nil {
|
||||||
|
b, err := json.Marshal(req.Query)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
postData = b
|
||||||
|
}
|
||||||
|
|
||||||
|
newReq, err := http.NewRequest(req.Method, "", nil)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
newReq.URL = req.URL()
|
||||||
|
newReq.Body = ioutil.NopCloser(bytes.NewReader(postData))
|
||||||
|
newReq.ContentLength = int64(len(postData))
|
||||||
|
|
||||||
|
if req.Method == "POST" || req.Method == "PUT" {
|
||||||
|
newReq.Header.Set("Content-Type", "application/json")
|
||||||
|
}
|
||||||
|
return newReq, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
var _ Requester = (*Request)(nil)
|
39
structs.go
39
structs.go
@ -5,8 +5,8 @@
|
|||||||
package goes
|
package goes
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"encoding/json"
|
||||||
"net/http"
|
"net/http"
|
||||||
"net/url"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
// Client represents a connection to elasticsearch
|
// Client represents a connection to elasticsearch
|
||||||
@ -20,45 +20,22 @@ 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
|
||||||
}
|
|
||||||
|
|
||||||
// Request holds a single request to elasticsearch
|
// Detected version of ES
|
||||||
type Request struct {
|
version string
|
||||||
// Which connection will be used
|
|
||||||
Conn *Client
|
|
||||||
|
|
||||||
// A search query
|
// user name for http basic auth
|
||||||
Query interface{}
|
AuthUsername string `json:"username"`
|
||||||
|
|
||||||
// Which index to search into
|
// pass word for http basic auth
|
||||||
IndexList []string
|
AuthPassword string `json:"password"`
|
||||||
|
|
||||||
// Which type to search into
|
|
||||||
TypeList []string
|
|
||||||
|
|
||||||
// HTTP Method to user (GET, POST ...)
|
|
||||||
Method string
|
|
||||||
|
|
||||||
// Which api keyword (_search, _bulk, etc) to use
|
|
||||||
API string
|
|
||||||
|
|
||||||
// Bulk data
|
|
||||||
BulkData []byte
|
|
||||||
|
|
||||||
// Request body
|
|
||||||
Body []byte
|
|
||||||
|
|
||||||
// A list of extra URL arguments
|
|
||||||
ExtraArgs url.Values
|
|
||||||
|
|
||||||
// Used for the id field when indexing a document
|
|
||||||
ID string
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Response holds an elasticsearch response
|
// Response holds an elasticsearch response
|
||||||
type Response struct {
|
type Response struct {
|
||||||
Acknowledged bool
|
Acknowledged bool
|
||||||
Error string
|
Error string
|
||||||
|
RawError json.RawMessage `json:"error"`
|
||||||
Errors bool
|
Errors bool
|
||||||
Status uint64
|
Status uint64
|
||||||
Took uint64
|
Took uint64
|
||||||
|
Loading…
Reference in New Issue
Block a user