Make all Request fields public so custom requests can be done

This commit is contained in:
Paul Bonser 2016-07-26 20:36:34 -05:00
parent 167d78773c
commit 391f1cf1bf
3 changed files with 67 additions and 67 deletions

110
goes.go
View File

@ -46,7 +46,7 @@ func (c *Connection) CreateIndex(name string, mapping interface{}) (*Response, e
Conn: c, Conn: c,
Query: mapping, Query: mapping,
IndexList: []string{name}, IndexList: []string{name},
method: "PUT", Method: "PUT",
} }
return r.Run() return r.Run()
@ -57,7 +57,7 @@ func (c *Connection) DeleteIndex(name string) (*Response, error) {
r := Request{ r := Request{
Conn: c, Conn: c,
IndexList: []string{name}, IndexList: []string{name},
method: "DELETE", Method: "DELETE",
} }
return r.Run() return r.Run()
@ -68,8 +68,8 @@ func (c *Connection) RefreshIndex(name string) (*Response, error) {
r := Request{ r := Request{
Conn: c, Conn: c,
IndexList: []string{name}, IndexList: []string{name},
method: "POST", Method: "POST",
api: "_refresh", API: "_refresh",
} }
return r.Run() return r.Run()
@ -82,8 +82,8 @@ func (c *Connection) UpdateIndexSettings(name string, settings interface{}) (*Re
Conn: c, Conn: c,
Query: settings, Query: settings,
IndexList: []string{name}, IndexList: []string{name},
method: "PUT", Method: "PUT",
api: "_settings", API: "_settings",
} }
return r.Run() return r.Run()
@ -96,8 +96,8 @@ func (c *Connection) Optimize(indexList []string, extraArgs url.Values) (*Respon
Conn: c, Conn: c,
IndexList: indexList, IndexList: indexList,
ExtraArgs: extraArgs, ExtraArgs: extraArgs,
method: "POST", Method: "POST",
api: "_optimize", API: "_optimize",
} }
return r.Run() return r.Run()
@ -109,8 +109,8 @@ func (c *Connection) Stats(indexList []string, extraArgs url.Values) (*Response,
Conn: c, Conn: c,
IndexList: indexList, IndexList: indexList,
ExtraArgs: extraArgs, ExtraArgs: extraArgs,
method: "GET", Method: "GET",
api: "_stats", API: "_stats",
} }
return r.Run() return r.Run()
@ -122,8 +122,8 @@ func (c *Connection) IndexStatus(indexList []string) (*Response, error) {
r := Request{ r := Request{
Conn: c, Conn: c,
IndexList: indexList, IndexList: indexList,
method: "GET", Method: "GET",
api: "_status", API: "_status",
} }
return r.Run() return r.Run()
@ -201,9 +201,9 @@ func (c *Connection) BulkSend(documents []Document) (*Response, error) {
r := Request{ r := Request{
Conn: c, 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() return r.Run()
@ -216,8 +216,8 @@ func (c *Connection) Search(query interface{}, indexList []string, typeList []st
Query: query, Query: query,
IndexList: indexList, IndexList: indexList,
TypeList: typeList, TypeList: typeList,
method: "POST", Method: "POST",
api: "_search", API: "_search",
ExtraArgs: extraArgs, ExtraArgs: extraArgs,
} }
@ -231,8 +231,8 @@ func (c *Connection) Count(query interface{}, indexList []string, typeList []str
Query: query, Query: query,
IndexList: indexList, IndexList: indexList,
TypeList: typeList, TypeList: typeList,
method: "POST", Method: "POST",
api: "_count", API: "_count",
ExtraArgs: extraArgs, ExtraArgs: extraArgs,
} }
@ -248,8 +248,8 @@ func (c *Connection) Query(query interface{}, indexList []string, typeList []str
Query: query, Query: query,
IndexList: indexList, IndexList: indexList,
TypeList: typeList, TypeList: typeList,
method: httpMethod, Method: httpMethod,
api: "_query", API: "_query",
ExtraArgs: extraArgs, ExtraArgs: extraArgs,
} }
@ -268,8 +268,8 @@ func (c *Connection) Scan(query interface{}, indexList []string, typeList []stri
Query: query, Query: query,
IndexList: indexList, IndexList: indexList,
TypeList: typeList, TypeList: typeList,
method: "POST", Method: "POST",
api: "_search", API: "_search",
ExtraArgs: v, ExtraArgs: v,
} }
@ -283,8 +283,8 @@ func (c *Connection) Scroll(scrollId string, timeout string) (*Response, error)
r := Request{ r := Request{
Conn: c, Conn: c,
method: "POST", Method: "POST",
api: "_search/scroll", API: "_search/scroll",
ExtraArgs: v, ExtraArgs: v,
Body: []byte(scrollId), Body: []byte(scrollId),
} }
@ -297,8 +297,8 @@ func (c *Connection) Get(index string, documentType string, id string, extraArgs
r := Request{ r := Request{
Conn: c, Conn: c,
IndexList: []string{index}, IndexList: []string{index},
method: "GET", Method: "GET",
api: documentType + "/" + id, API: documentType + "/" + id,
ExtraArgs: extraArgs, ExtraArgs: extraArgs,
} }
@ -315,12 +315,12 @@ func (c *Connection) Index(d Document, extraArgs url.Values) (*Response, error)
IndexList: []string{d.Index.(string)}, IndexList: []string{d.Index.(string)},
TypeList: []string{d.Type}, TypeList: []string{d.Type},
ExtraArgs: extraArgs, ExtraArgs: extraArgs,
method: "POST", Method: "POST",
} }
if d.Id != nil { if d.Id != nil {
r.method = "PUT" r.Method = "PUT"
r.id = d.Id.(string) r.ID = d.Id.(string)
} }
return r.Run() return r.Run()
@ -335,8 +335,8 @@ func (c *Connection) Delete(d Document, extraArgs url.Values) (*Response, error)
IndexList: []string{d.Index.(string)}, IndexList: []string{d.Index.(string)},
TypeList: []string{d.Type}, TypeList: []string{d.Type},
ExtraArgs: extraArgs, ExtraArgs: extraArgs,
method: "DELETE", Method: "DELETE",
id: d.Id.(string), ID: d.Id.(string),
} }
return r.Run() return r.Run()
@ -352,7 +352,7 @@ func (req *Request) Run() (*Response, error) {
return esResp, err return esResp, err
} }
if req.method != "HEAD" { if req.Method != "HEAD" {
err = json.Unmarshal(body, &esResp) err = json.Unmarshal(body, &esResp)
if err != nil { if err != nil {
return esResp, err return esResp, err
@ -363,7 +363,7 @@ func (req *Request) Run() (*Response, error) {
} }
} }
if req.api == "_bulk" && esResp.Errors { if req.API == "_bulk" && esResp.Errors {
for _, item := range esResp.Items { for _, item := range esResp.Items {
for _, i := range item { for _, i := range item {
if i.Error != "" { if i.Error != "" {
@ -387,8 +387,8 @@ func (req *Request) run() ([]byte, uint64, error) {
// XXX : refactor this // XXX : refactor this
if len(req.Body) > 0 { if len(req.Body) > 0 {
postData = req.Body postData = req.Body
} else if req.api == "_bulk" { } else if req.API == "_bulk" {
postData = req.bulkData postData = req.BulkData
} else { } else {
b, err := json.Marshal(req.Query) b, err := json.Marshal(req.Query)
if err != nil { if err != nil {
@ -399,12 +399,12 @@ func (req *Request) run() ([]byte, uint64, error) {
reader := bytes.NewReader(postData) reader := bytes.NewReader(postData)
newReq, err := http.NewRequest(req.method, req.Url(), reader) newReq, err := http.NewRequest(req.Method, req.Url(), reader)
if err != nil { if err != nil {
return nil, 0, err return nil, 0, err
} }
if req.method == "POST" || req.method == "PUT" { if req.Method == "POST" || req.Method == "PUT" {
newReq.Header.Set("Content-Type", "application/json") newReq.Header.Set("Content-Type", "application/json")
} }
@ -436,11 +436,11 @@ func (r *Request) Url() string {
} }
// XXX : for indexing documents using the normal (non bulk) API // XXX : for indexing documents using the normal (non bulk) API
if len(r.id) > 0 { if len(r.ID) > 0 {
path += "/" + r.id path += "/" + r.ID
} }
path += "/" + r.api path += "/" + r.API
u := url.URL{ u := url.URL{
Scheme: "http", Scheme: "http",
@ -490,8 +490,8 @@ func (c *Connection) PutMapping(typeName string, mapping interface{}, indexes []
Conn: c, Conn: c,
Query: mapping, Query: mapping,
IndexList: indexes, IndexList: indexes,
method: "PUT", Method: "PUT",
api: "_mappings/" + typeName, API: "_mappings/" + typeName,
} }
return r.Run() return r.Run()
@ -502,8 +502,8 @@ func (c *Connection) GetMapping(types []string, indexes []string) (*Response, er
r := Request{ r := Request{
Conn: c, Conn: c,
IndexList: indexes, IndexList: indexes,
method: "GET", Method: "GET",
api: "_mapping/" + strings.Join(types, ","), API: "_mapping/" + strings.Join(types, ","),
} }
return r.Run() return r.Run()
@ -515,7 +515,7 @@ func (c *Connection) IndicesExist(indexes []string) (bool, error) {
r := Request{ r := Request{
Conn: c, Conn: c,
IndexList: indexes, IndexList: indexes,
method: "HEAD", Method: "HEAD",
} }
resp, err := r.Run() resp, err := r.Run()
@ -530,12 +530,12 @@ func (c *Connection) Update(d Document, query interface{}, extraArgs url.Values)
IndexList: []string{d.Index.(string)}, IndexList: []string{d.Index.(string)},
TypeList: []string{d.Type}, TypeList: []string{d.Type},
ExtraArgs: extraArgs, ExtraArgs: extraArgs,
method: "POST", Method: "POST",
api: "_update", API: "_update",
} }
if d.Id != nil { if d.Id != nil {
r.id = d.Id.(string) r.ID = d.Id.(string)
} }
return r.Run() return r.Run()
@ -547,8 +547,8 @@ func (c *Connection) DeleteMapping(typeName string, indexes []string) (*Response
r := Request{ r := Request{
Conn: c, Conn: c,
IndexList: indexes, IndexList: indexes,
method: "DELETE", Method: "DELETE",
api: "_mappings/" + typeName, API: "_mappings/" + typeName,
} }
return r.Run() return r.Run()
@ -571,8 +571,8 @@ func (c *Connection) modifyAlias(action string, alias string, indexes []string)
r := Request{ r := Request{
Conn: c, Conn: c,
Query: command, Query: command,
method: "POST", Method: "POST",
api: "_aliases", API: "_aliases",
} }
return r.Run() return r.Run()
@ -593,8 +593,8 @@ func (c *Connection) AliasExists(alias string) (bool, error) {
r := Request{ r := Request{
Conn: c, Conn: c,
method: "HEAD", Method: "HEAD",
api: "_alias/" + alias, API: "_alias/" + alias,
} }
resp, err := r.Run() resp, err := r.Run()

View File

@ -67,8 +67,8 @@ func (s *GoesTestSuite) TestUrl(c *C) {
Query: "q", Query: "q",
IndexList: []string{"i"}, IndexList: []string{"i"},
TypeList: []string{}, TypeList: []string{},
method: "GET", Method: "GET",
api: "_search", API: "_search",
} }
c.Assert(r.Url(), Equals, "http://"+ES_HOST+":"+ES_PORT+"/i/_search") c.Assert(r.Url(), Equals, "http://"+ES_HOST+":"+ES_PORT+"/i/_search")
@ -83,8 +83,8 @@ func (s *GoesTestSuite) TestUrl(c *C) {
r.ExtraArgs.Set("version", "1") r.ExtraArgs.Set("version", "1")
c.Assert(r.Url(), Equals, "http://"+ES_HOST+":"+ES_PORT+"/a,b/c,d/_search?version=1") c.Assert(r.Url(), Equals, "http://"+ES_HOST+":"+ES_PORT+"/a,b/c,d/_search?version=1")
r.id = "1234" r.ID = "1234"
r.api = "" r.API = ""
c.Assert(r.Url(), Equals, "http://"+ES_HOST+":"+ES_PORT+"/a,b/c,d/1234/?version=1") c.Assert(r.Url(), Equals, "http://"+ES_HOST+":"+ES_PORT+"/a,b/c,d/1234/?version=1")
} }
@ -97,8 +97,8 @@ func (s *GoesTestSuite) TestEsDown(c *C) {
Conn: conn, Conn: conn,
Query: query, Query: query,
IndexList: []string{"i"}, IndexList: []string{"i"},
method: "GET", Method: "GET",
api: "_search", API: "_search",
} }
_, err := r.Run() _, err := r.Run()
@ -114,8 +114,8 @@ func (s *GoesTestSuite) TestRunMissingIndex(c *C) {
Conn: conn, Conn: conn,
Query: query, Query: query,
IndexList: []string{"i"}, IndexList: []string{"i"},
method: "GET", Method: "GET",
api: "_search", API: "_search",
} }
_, err := r.Run() _, err := r.Run()

View File

@ -37,13 +37,13 @@ type Request struct {
TypeList []string TypeList []string
// HTTP Method to user (GET, POST ...) // HTTP Method to user (GET, POST ...)
method string Method string
// Which api keyword (_search, _bulk, etc) to use // Which api keyword (_search, _bulk, etc) to use
api string API string
// Bulk data // Bulk data
bulkData []byte BulkData []byte
// Request body // Request body
Body []byte Body []byte
@ -52,7 +52,7 @@ type Request struct {
ExtraArgs url.Values ExtraArgs url.Values
// Used for the id field when indexing a document // Used for the id field when indexing a document
id string ID string
} }
// Represents a Response from elasticsearch // Represents a Response from elasticsearch