forked from mattbaird/elastigo
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
implemented count, update, flush and status (mostly)
- Loading branch information
Showing
6 changed files
with
146 additions
and
23 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,44 @@ | ||
package core | ||
|
||
import ( | ||
"encoding/json" | ||
"fmt" | ||
"github.com/mattbaird/elastigo/api" | ||
) | ||
|
||
type CountResponse struct { | ||
Count int `json:"count"` | ||
Shard Shards `json:"_shards"` | ||
} | ||
|
||
type Shards struct { | ||
Total int `json:"total"` | ||
Successful int `json:"successful"` | ||
Failed int `json:"failed"` | ||
} | ||
|
||
// The count API allows to easily execute a query and get the number of matches for that query. | ||
// It can be executed across one or more indices and across one or more types. | ||
// The query can either be provided using a simple query string as a parameter, | ||
//or using the Query DSL defined within the request body. | ||
// http://www.elasticsearch.org/guide/reference/api/count.html | ||
// TODO: take parameters. | ||
// currently not working against 0.19.10 | ||
func Count(pretty bool, index string, _type string) (CountResponse, error) { | ||
var url string | ||
var retval CountResponse | ||
url = fmt.Sprintf("/%s/%s/_count?%s", index, _type, api.Pretty(pretty)) | ||
body, err := api.DoCommand("GET", url, nil) | ||
if err != nil { | ||
return retval, err | ||
} | ||
if err == nil { | ||
// marshall into json | ||
jsonErr := json.Unmarshal([]byte(body), &retval) | ||
if jsonErr != nil { | ||
return retval, jsonErr | ||
} | ||
} | ||
fmt.Println(body) | ||
return retval, err | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,36 @@ | ||
package core | ||
package core | ||
|
||
import ( | ||
"encoding/json" | ||
"fmt" | ||
"github.com/mattbaird/elastigo/api" | ||
) | ||
|
||
// The update API allows to update a document based on a script provided. The operation gets the document | ||
// (collocated with the shard) from the index, runs the script (with optional script language and parameters), | ||
// and index back the result (also allows to delete, or ignore the operation). It uses versioning to make sure | ||
// no updates have happened during the “get” and “reindex”. (available from 0.19 onwards). | ||
// Note, this operation still means full reindex of the document, it just removes some network roundtrips | ||
// and reduces chances of version conflicts between the get and the index. The _source field need to be enabled | ||
// for this feature to work. | ||
// | ||
// http://www.elasticsearch.org/guide/reference/api/update.html | ||
// TODO: finish this, it's fairly complex | ||
func Update(pretty bool, index string, _type string, id string) (api.BaseResponse, error) { | ||
var url string | ||
var retval api.BaseResponse | ||
url = fmt.Sprintf("/%s/%s/%s/_update?%s", index, _type, id, api.Pretty(pretty)) | ||
body, err := api.DoCommand("POST", url, nil) | ||
if err != nil { | ||
return retval, err | ||
} | ||
if err == nil { | ||
// marshall into json | ||
jsonErr := json.Unmarshal([]byte(body), &retval) | ||
if jsonErr != nil { | ||
return retval, jsonErr | ||
} | ||
} | ||
fmt.Println(body) | ||
return retval, err | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,36 @@ | ||
package indices | ||
package indices | ||
|
||
import ( | ||
"encoding/json" | ||
"fmt" | ||
"github.com/mattbaird/elastigo/api" | ||
) | ||
|
||
// The flush API allows to flush one or more indices through an API. The flush process of an index basically | ||
// frees memory from the index by flushing data to the index storage and clearing the internal transaction | ||
// log. By default, ElasticSearch uses memory heuristics in order to automatically trigger flush operations | ||
// as required in order to clear memory. | ||
// http://www.elasticsearch.org/guide/reference/api/admin-indices-flush.html | ||
// TODO: add Shards to response | ||
func Flush(index ...string) (api.BaseResponse, error) { | ||
var url string | ||
var retval api.BaseResponse | ||
if len(index) > 0 { | ||
url = fmt.Sprintf("/%s/_flush", index) | ||
} else { | ||
url = "/_flush" | ||
} | ||
body, err := api.DoCommand("POST", url, nil) | ||
if err != nil { | ||
return retval, err | ||
} | ||
if err == nil { | ||
// marshall into json | ||
jsonErr := json.Unmarshal([]byte(body), &retval) | ||
if jsonErr != nil { | ||
return retval, jsonErr | ||
} | ||
} | ||
fmt.Println(body) | ||
return retval, err | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,31 +1,35 @@ | ||
package indices | ||
|
||
import ( | ||
"encoding/json" | ||
"fmt" | ||
"github.com/mattbaird/elastigo/api" | ||
"log" | ||
) | ||
|
||
// Lists status details of all indices or the specified index. | ||
// http://www.elasticsearch.org/guide/reference/api/admin-indices-status.html | ||
func RunStatus(pretty bool, indices ...string) { | ||
index := "" | ||
if len(indices) >= 1 { | ||
index = indices[0] | ||
} | ||
|
||
var response map[string]interface{} | ||
|
||
func Status(pretty bool, indices ...string) (api.BaseResponse, error) { | ||
var retval api.BaseResponse | ||
var body string | ||
if len(index) > 0 { | ||
body = api.ElasticSearchRequest("GET", "/"+index+"/_status?pretty=1").Do(&response) | ||
var url string | ||
if len(indices) > 0 { | ||
//TODO, emit the indices csv style | ||
|
||
url = "/" + indices[0] + "/_status?pretty=1" | ||
} else { | ||
body = api.ElasticSearchRequest("GET", "/_status?pretty=1").Do(&response) | ||
url = "/_status?pretty=1" | ||
} | ||
|
||
if error, ok := response["error"]; ok { | ||
status, _ := response["status"] | ||
log.Fatalf("Error: %v (%v)\n", error, status) | ||
body, err := api.DoCommand("GET", url, nil) | ||
if err != nil { | ||
return retval, err | ||
} | ||
if err == nil { | ||
// marshall into json | ||
jsonErr := json.Unmarshal([]byte(body), &retval) | ||
if jsonErr != nil { | ||
return retval, jsonErr | ||
} | ||
} | ||
fmt.Print(body) | ||
fmt.Println(body) | ||
return retval, err | ||
} |