make all request fields exported so custom requests can be done

This commit is contained in:
Paul Bonser 2016-09-27 14:30:17 -05:00
parent c79b7c2b36
commit 3f0875ed66
3 changed files with 67 additions and 67 deletions

110
goes.go
View File

@ -49,7 +49,7 @@ func (c *Client) CreateIndex(name string, mapping interface{}) (*Response, error
Conn: c, Conn: c,
Query: mapping, Query: mapping,
IndexList: []string{name}, IndexList: []string{name},
method: "PUT", Method: "PUT",
} }
return r.Run() return r.Run()
@ -60,7 +60,7 @@ func (c *Client) 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()
@ -71,8 +71,8 @@ func (c *Client) 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()
@ -85,8 +85,8 @@ func (c *Client) UpdateIndexSettings(name string, settings interface{}) (*Respon
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()
@ -99,8 +99,8 @@ func (c *Client) Optimize(indexList []string, extraArgs url.Values) (*Response,
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()
@ -112,8 +112,8 @@ func (c *Client) Stats(indexList []string, extraArgs url.Values) (*Response, err
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()
@ -125,8 +125,8 @@ func (c *Client) 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()
@ -204,9 +204,9 @@ func (c *Client) 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()
@ -219,8 +219,8 @@ func (c *Client) Search(query interface{}, indexList []string, typeList []string
Query: query, Query: query,
IndexList: indexList, IndexList: indexList,
TypeList: typeList, TypeList: typeList,
method: "POST", Method: "POST",
api: "_search", API: "_search",
ExtraArgs: extraArgs, ExtraArgs: extraArgs,
} }
@ -234,8 +234,8 @@ func (c *Client) Count(query interface{}, indexList []string, typeList []string,
Query: query, Query: query,
IndexList: indexList, IndexList: indexList,
TypeList: typeList, TypeList: typeList,
method: "POST", Method: "POST",
api: "_count", API: "_count",
ExtraArgs: extraArgs, ExtraArgs: extraArgs,
} }
@ -251,8 +251,8 @@ func (c *Client) Query(query interface{}, indexList []string, typeList []string,
Query: query, Query: query,
IndexList: indexList, IndexList: indexList,
TypeList: typeList, TypeList: typeList,
method: httpMethod, Method: httpMethod,
api: "_query", API: "_query",
ExtraArgs: extraArgs, ExtraArgs: extraArgs,
} }
@ -271,8 +271,8 @@ func (c *Client) Scan(query interface{}, indexList []string, typeList []string,
Query: query, Query: query,
IndexList: indexList, IndexList: indexList,
TypeList: typeList, TypeList: typeList,
method: "POST", Method: "POST",
api: "_search", API: "_search",
ExtraArgs: v, ExtraArgs: v,
} }
@ -286,8 +286,8 @@ func (c *Client) 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),
} }
@ -300,8 +300,8 @@ func (c *Client) Get(index string, documentType string, id string, extraArgs url
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,
} }
@ -318,12 +318,12 @@ func (c *Client) 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()
@ -338,8 +338,8 @@ func (c *Client) 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()
@ -355,7 +355,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
@ -366,7 +366,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 != "" {
@ -390,8 +390,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 {
@ -402,12 +402,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")
} }
@ -439,11 +439,11 @@ func (req *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(req.id) > 0 { if len(req.ID) > 0 {
path += "/" + req.id path += "/" + req.ID
} }
path += "/" + req.api path += "/" + req.API
u := url.URL{ u := url.URL{
Scheme: "http", Scheme: "http",
@ -492,8 +492,8 @@ func (c *Client) PutMapping(typeName string, mapping interface{}, indexes []stri
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()
@ -505,8 +505,8 @@ func (c *Client) GetMapping(types []string, indexes []string) (*Response, error)
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()
@ -518,7 +518,7 @@ func (c *Client) 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()
@ -534,12 +534,12 @@ func (c *Client) Update(d Document, query interface{}, extraArgs url.Values) (*R
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()
@ -551,8 +551,8 @@ func (c *Client) DeleteMapping(typeName string, indexes []string) (*Response, er
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()
@ -575,8 +575,8 @@ func (c *Client) modifyAlias(action string, alias string, indexes []string) (*Re
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()
@ -597,8 +597,8 @@ func (c *Client) 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://"+ESHost+":"+ESPort+"/i/_search") c.Assert(r.URL(), Equals, "http://"+ESHost+":"+ESPort+"/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://"+ESHost+":"+ESPort+"/a,b/c,d/_search?version=1") c.Assert(r.URL(), Equals, "http://"+ESHost+":"+ESPort+"/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(), Equals, "http://"+ESHost+":"+ESPort+"/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
} }
// Response holds an elasticsearch response // Response holds an elasticsearch response