// IndexWithParameters takes all the potential parameters available func IndexWithParameters(pretty bool, index string, _type string, id string, parentId string, version int, op_type string, routing string, timestamp string, ttl int, percolate string, timeout string, refresh bool, data interface{}) (api.BaseResponse, error) { var url string var retval api.BaseResponse url, err := GetIndexUrl(index, _type, id, parentId, version, op_type, routing, timestamp, ttl, percolate, timeout, refresh) if err != nil { return retval, err } var method string if len(id) == 0 { method = "POST" } else { method = "PUT" } body, err := api.DoCommand(method, url, data) if err != nil { return retval, err } if err == nil { // marshall into json jsonErr := json.Unmarshal(body, &retval) if jsonErr != nil { return retval, jsonErr } } return retval, err }
// ClearCache allows to clear either all caches or specific cached associated with one ore more indices. // see http://www.elasticsearch.org/guide/reference/api/admin-indices-clearcache/ func ClearCache(clearId bool, clearBloom bool, indices ...string) (api.ExtendedStatus, error) { var retval api.ExtendedStatus var clearCacheUrl string if len(indices) > 0 { clearCacheUrl = fmt.Sprintf("/%s/_cache/clear", strings.Join(indices, ",")) } else { clearCacheUrl = fmt.Sprintf("/_cache/clear") } var values url.Values = url.Values{} if clearId { values.Add("id", "true") } if clearBloom { values.Add("bloom", "true") } clearCacheUrl += "?" + values.Encode() body, err := api.DoCommand("POST", clearCacheUrl, nil) if err != nil { return retval, err } if err == nil { // marshall into json jsonErr := json.Unmarshal(body, &retval) if jsonErr != nil { return retval, jsonErr } } return retval, err }
// MGet allows the caller to get multiple documents based on an index, type (optional) and id (and possibly routing). // The response includes a docs array with all the fetched documents, each element similar in structure to a document // provided by the get API. // see http://www.elasticsearch.org/guide/reference/api/multi-get.html func MGet(pretty bool, index string, _type string, mgetRequest MGetRequestContainer) (MGetResponseContainer, error) { var url string var retval MGetResponseContainer if len(index) <= 0 { url = fmt.Sprintf("/_mget?%s", api.Pretty(pretty)) } if len(_type) > 0 && len(index) > 0 { url = fmt.Sprintf("/%s/%s/_mget?%s", index, _type, api.Pretty(pretty)) } else if len(index) > 0 { url = fmt.Sprintf("/%s/_mget?%s", index, 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(body, &retval) if jsonErr != nil { return retval, jsonErr } } fmt.Println(body) return retval, err }
// GetSource retrieves the document by id and converts it to provided interface func GetSource(index string, _type string, id string, source interface{}) error { url := fmt.Sprintf("/%s/%s/%s/_source", index, _type, id) body, err := api.DoCommand("GET", url, nil) if err == nil { err = json.Unmarshal(body, &source) } //fmt.Println(body) return err }
// This does the actual send of a buffer, which has already been formatted // into bytes of ES formatted bulk data func BulkSend(buf *bytes.Buffer) error { _, err := api.DoCommand("POST", "/_bulk", buf) if err != nil { log.Println(err) BulkErrorCt += 1 return err } return nil }
// NodesShutdown allows the caller to shutdown between one and all nodes in the cluster // delay is a integer representing number of seconds // passing "" or "_all" for the nodes parameter will shut down all nodes // see http://www.elasticsearch.org/guide/reference/api/admin-cluster-nodes-shutdown/ func NodesShutdown(delay int, nodes ...string) error { shutdownUrl := fmt.Sprintf("/_cluster/nodes/%s/_shutdown", strings.Join(nodes, ",")) if delay > 0 { var values url.Values = url.Values{} values.Add("delay", strconv.Itoa(delay)) shutdownUrl += "?" + values.Encode() } _, err := api.DoCommand("POST", shutdownUrl, nil) if err != nil { return err } return nil }
// IndicesExists checks for the existance of indices. uses http 404 if it does not exist, and 200 if it does // see http://www.elasticsearch.org/guide/reference/api/admin-indices-indices-exists/ func IndicesExists(indices ...string) (bool, error) { var url string if len(indices) > 0 { url = fmt.Sprintf("/%s", strings.Join(indices, ",")) } _, err := api.DoCommand("HEAD", url, nil) if err != nil { eserror := err.(api.ESError) if eserror.Code == 404 { return false, err } else { return eserror.Code == 200, err } } return true, nil }
// State gets the comprehensive state information for the whole cluster // see http://www.elasticsearch.org/guide/reference/api/admin-cluster-state/ func State(filter_nodes bool, filter_routing_table bool, filter_metadata bool, filter_blocks bool, filter_indices ...string) (ClusterStateResponse, error) { url := getStateUrl(false, false, false, false, filter_indices...) var retval ClusterStateResponse body, err := api.DoCommand("GET", url, nil) if err != nil { return retval, err } if err == nil { // marshall into json jsonErr := json.Unmarshal(body, &retval) if jsonErr != nil { return retval, jsonErr } } return retval, err }
// Update updates 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, data interface{}) (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, data) if err != nil { return retval, err } if err == nil { // marshall into json jsonErr := json.Unmarshal(body, &retval) if jsonErr != nil { return retval, jsonErr } } return retval, err }
// Count allows the caller 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(body, &retval) if jsonErr != nil { return retval, jsonErr } } //fmt.Println(body) return retval, err }
// RegisterPercolate allows the caller to register queries against an index, and then send percolate requests which include a doc, and // getting back the queries that match on that doc out of the set of registered queries. // Think of it as the reverse operation of indexing and then searching. Instead of sending docs, indexing them, // and then running queries. One sends queries, registers them, and then sends docs and finds out which queries // match that doc. // see http://www.elasticsearch.org/guide/reference/api/percolate.html func RegisterPercolate(pretty bool, index string, name string, query api.Query) (api.BaseResponse, error) { var url string var retval api.BaseResponse url = fmt.Sprintf("/_percolator/%s/%s?%s", index, name, api.Pretty(pretty)) body, err := api.DoCommand("PUT", url, query) if err != nil { return retval, err } if err == nil { // marshall into json jsonErr := json.Unmarshal(body, &retval) if jsonErr != nil { return retval, jsonErr } } fmt.Println(body) return retval, err }
// Delete API allows to delete a typed JSON document from a specific index based on its id. // http://www.elasticsearch.org/guide/reference/api/delete.html // todo: add routing and versioning support func Delete(pretty bool, index string, _type string, id string, version int, routing string) (api.BaseResponse, error) { var url string var retval api.BaseResponse url = fmt.Sprintf("/%s/%s/%s?%s", index, _type, id, api.Pretty(pretty)) body, err := api.DoCommand("DELETE", url, nil) if err != nil { return retval, err } if err == nil { // marshall into json jsonErr := json.Unmarshal(body, &retval) if jsonErr != nil { return retval, jsonErr } } //fmt.Println(body) return retval, err }
// MoreLikeThis allows the caller to get documents that are “like” a specified document. // http://www.elasticsearch.org/guide/reference/api/more-like-this.html func MoreLikeThis(pretty bool, index string, _type string, id string, query MoreLikeThisQuery) (api.BaseResponse, error) { var url string var retval api.BaseResponse url = fmt.Sprintf("/%s/%s/%s/_mlt?%s", index, _type, id, api.Pretty(pretty)) body, err := api.DoCommand("GET", url, query) if err != nil { return retval, err } if err == nil { // marshall into json jsonErr := json.Unmarshal(body, &retval) if jsonErr != nil { return retval, jsonErr } } fmt.Println(body) return retval, err }
func Percolate(pretty bool, index string, _type string, name string, doc string) (api.Match, error) { var url string var retval api.Match url = fmt.Sprintf("/%s/%s/_percolate?%s", index, _type, api.Pretty(pretty)) body, err := api.DoCommand("GET", url, doc) if err != nil { return retval, err } if err == nil { // marshall into json jsonErr := json.Unmarshal(body, &retval) if jsonErr != nil { return retval, jsonErr } } fmt.Println(body) return retval, err }
func Scroll(pretty bool, scroll_id string, scroll string) (SearchResult, error) { var url string var retval SearchResult url = fmt.Sprintf("/_search/scroll?%s%s", api.Pretty(pretty), api.Scroll(scroll)) body, err := api.DoCommand("POST", url, scroll_id) if err != nil { return retval, err } if err == nil { // marshall into json jsonErr := json.Unmarshal([]byte(body), &retval) if jsonErr != nil { return retval, jsonErr } } return retval, err }
// UpdateSettings allows to update cluster wide specific settings. Defaults to Transient setting // Settings updated can either be persistent (applied cross restarts) or transient (will not survive a full cluster restart). // http://www.elasticsearch.org/guide/reference/api/admin-cluster-update-settings.html func UpdateSettings(settingType string, key string, value int) (ClusterSettingsResponse, error) { var retval ClusterSettingsResponse if settingType != "transient" && settingType != "persistent" { return retval, errors.New(fmt.Sprintf("settingType must be one of transient or persistent, you passed %s", settingType)) } var url string = "/_cluster/state" m := map[string]map[string]int{settingType: map[string]int{key: value}} body, err := api.DoCommand("PUT", url, m) if err != nil { return retval, err } if err == nil { // marshall into json jsonErr := json.Unmarshal(body, &retval) if jsonErr != nil { return retval, jsonErr } } return retval, err }
// The inspecting the response func ExampleBulkIndexer_responses() { indexer := core.NewBulkIndexer(10) // Create a custom Sendor Func, to allow inspection of response/error indexer.BulkSendor = func(buf *bytes.Buffer) error { // @buf is the buffer of docs about to be written respJson, err := api.DoCommand("POST", "/_bulk", buf) if err != nil { // handle it better than this fmt.Println(string(respJson)) } return err } done := make(chan bool) indexer.Run(done) for i := 0; i < 20; i++ { indexer.Index("twitter", "user", strconv.Itoa(i), "", nil, `{"name":"bob"}`) } done <- true // send shutdown signal }
// HealthWithParameters gets cluster health data and exposes all parameters to the caller // level - one of cluster, indices, or shards // wait_for_status - green, yellow, or red. Will wait until the status changes to passed status // wait_for_relocating_shards - How many relocating shards to wait for. Default no wait // wait_for_nodes - The request waits until N nodes are available // timeout - How long to wait if any wait_* params are passed. Defaults to 30s func HealthWithParameters(pretty bool, level string, wait_for_status string, timeout string, wait_for_relocating_shards int, wait_for_nodes int, indices ...string) (ClusterHealthResponse, error) { var url string var retval ClusterHealthResponse url, err := getHealthUrl(pretty, level, wait_for_status, timeout, wait_for_relocating_shards, wait_for_nodes, indices...) if err != nil { return retval, err } body, err := api.DoCommand("GET", url, nil) if err != nil { return retval, err } if err == nil { // marshall into json jsonErr := json.Unmarshal(body, &retval) if jsonErr != nil { return retval, jsonErr } } return retval, err }
// SearchRequest performs a very basic search on an index via the request URI API. // // params: // @pretty: bool for pretty reply or not, a parameter to elasticsearch // @index: the elasticsearch index // @_type: optional ("" if not used) search specific type in this index // @query: this can be one of 3 types: // 1) string value that is valid elasticsearch // 2) io.Reader that can be set in body (also valid elasticsearch string syntax..) // 3) other type marshalable to json (also valid elasticsearch json) // // out, err := SearchRequest(true, "github","",qryType ,"", 0) // // http://www.elasticsearch.org/guide/reference/api/search/uri-request.html func SearchRequest(pretty bool, index string, _type string, query interface{}, scroll string, scan int) (SearchResult, error) { var uriVal string var retval SearchResult if len(_type) > 0 && _type != "*" { uriVal = fmt.Sprintf("/%s/%s/_search?%s%s%s", index, _type, api.Pretty(pretty), api.Scroll(scroll), api.Scan(scan)) } else { uriVal = fmt.Sprintf("/%s/_search?%s%s%s", index, api.Pretty(pretty), api.Scroll(scroll), api.Scan(scan)) } body, err := api.DoCommand("POST", uriVal, query) if err != nil { return retval, err } if err == nil { // marshall into json jsonErr := json.Unmarshal([]byte(body), &retval) if jsonErr != nil { return retval, jsonErr } } return retval, err }
// Refresh explicitly refreshes one or more index, making all operations performed since // the last refresh available for search. The (near) real-time capabilities depend on the index engine used. // For example, the robin one requires refresh to be called, but by default a refresh is scheduled periodically. // http://www.elasticsearch.org/guide/reference/api/admin-indices-refresh.html // TODO: add Shards to response func Refresh(indices ...string) (api.BaseResponse, error) { var url string var retval api.BaseResponse if len(indices) > 0 { url = fmt.Sprintf("/%s/_refresh", strings.Join(indices, ",")) } else { url = "/_refresh" } body, err := api.DoCommand("POST", url, nil) if err != nil { return retval, err } if err == nil { // marshall into json jsonErr := json.Unmarshal(body, &retval) if jsonErr != nil { return retval, jsonErr } } return retval, err }
//Modified from elastigo to use custom response type func elasticGet(pretty bool, index string, _type string, id string) (elasticResponse, error) { var url string var retval elasticResponse if len(_type) > 0 { url = fmt.Sprintf("/%s/%s/%s?%s", index, _type, id, api.Pretty(pretty)) } else { url = fmt.Sprintf("/%s/%s?%s", index, id, 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(body, &retval) if jsonErr != nil { return retval, jsonErr } } return retval, err }
// The cluster health API allows to get a very simple status on the health of the cluster. // see http://www.elasticsearch.org/guide/reference/api/admin-cluster-health.html // TODO: implement wait_for_status, timeout, wait_for_relocating_shards, wait_for_nodes // TODO: implement level (Can be one of cluster, indices or shards. Controls the details level of the health // information returned. Defaults to cluster.) func Reroute(pretty bool, dryRun bool, commands Commands) (ClusterHealthResponse, error) { var url string var retval ClusterHealthResponse if len(commands.Commands) > 0 { url = fmt.Sprintf("/_cluster/reroute%s&%s", api.Pretty(pretty), dryRunOption(dryRun)) } else { return retval, errors.New("Must pass at least one command") } body, err := api.DoCommand("POST", url, commands) if err != nil { return retval, err } if err == nil { // marshall into json jsonErr := json.Unmarshal(body, &retval) if jsonErr != nil { return retval, jsonErr } } fmt.Println(body) return retval, err }
// Explain computes a score explanation for a query and a specific document. // This can give useful feedback whether a document matches or didn’t match a specific query. // This feature is available from version 0.19.9 and up. // see http://www.elasticsearch.org/guide/reference/api/explain.html func Explain(pretty bool, index string, _type string, id string, query string) (api.Match, error) { var url string var retval api.Match if len(_type) > 0 { url = fmt.Sprintf("/%s/%s/_explain?%s", index, _type, api.Pretty(pretty)) } else { url = fmt.Sprintf("/%s/_explain?%s", index, api.Pretty(pretty)) } body, err := api.DoCommand("GET", url, query) if err != nil { return retval, err } if err == nil { // marshall into json jsonErr := json.Unmarshal(body, &retval) if jsonErr != nil { return retval, jsonErr } } fmt.Println(body) return retval, err }
// AnalyzeIndices performs the analysis process on a text and return the tokens breakdown of the text. // http://www.elasticsearch.org/guide/reference/api/admin-indices-analyze/ func OptimizeIndices(max_num_segments int, only_expunge_deletes bool, refresh bool, flush bool, wait_for_merge bool, indices ...string) (OptimizeResponse, error) { var retval OptimizeResponse var optimizeUrl string = "/_optimize" if len(indices) > 0 { optimizeUrl = fmt.Sprintf("/%s/%s", strings.Join(indices, ","), optimizeUrl) } var values url.Values = url.Values{} if max_num_segments > 0 { values.Add("max_num_segments", strconv.Itoa(max_num_segments)) } if only_expunge_deletes { values.Add("only_expunge_deletes", strconv.FormatBool(only_expunge_deletes)) } if !refresh { values.Add("refresh", strconv.FormatBool(refresh)) } if !flush { values.Add("flush", strconv.FormatBool(flush)) } if wait_for_merge { values.Add("wait_for_merge", strconv.FormatBool(wait_for_merge)) } optimizeUrl += "?" + values.Encode() body, err := api.DoCommand("POST", optimizeUrl, nil) if err != nil { return retval, err } if err == nil { // marshall into json jsonErr := json.Unmarshal(body, &retval) if jsonErr != nil { return retval, jsonErr } } return retval, err }
// DeleteByQuery allows the caller to delete documents from one or more indices and one or more types based on a query. // The query can either be provided using a simple query string as a parameter, or using the Query DSL defined within // the request body. // see: http://www.elasticsearch.org/guide/reference/api/delete-by-query.html func DeleteByQuery(pretty bool, indices []string, types []string, query interface{}) (api.BaseResponse, error) { var url string var retval api.BaseResponse if len(indices) > 0 && len(types) > 0 { url = fmt.Sprintf("http://localhost:9200/%s/%s/_query?%s&%s", strings.Join(indices, ","), strings.Join(types, ","), buildQuery(), api.Pretty(pretty)) } else if len(indices) > 0 { url = fmt.Sprintf("http://localhost:9200/%s/_query?%s&%s", strings.Join(indices, ","), buildQuery(), api.Pretty(pretty)) } body, err := api.DoCommand("DELETE", url, query) if err != nil { return retval, err } if err == nil { // marshall into json jsonErr := json.Unmarshal(body, &retval) if jsonErr != nil { return retval, jsonErr } } fmt.Println(body) return retval, err }
// AnalyzeIndices performs the analysis process on a text and return the tokens breakdown of the text. // http://www.elasticsearch.org/guide/reference/api/admin-indices-analyze/ func AnalyzeIndices(index string, analyzer string, tokenizer string, field string, text string, filters ...string) (AnalyzeResponse, error) { var retval AnalyzeResponse if len(text) <= 0 { return retval, errors.New("text to analyze must not be blank") } var analyzeUrl string = "/_analyze" if len(index) > 0 { analyzeUrl = fmt.Sprintf("/%s/%s", index, analyzeUrl) } var values url.Values = url.Values{} if len(analyzer) > 0 { values.Add("analyzer", analyzer) } if len(tokenizer) > 0 { values.Add("tokenizer", tokenizer) } if len(field) > 0 { values.Add("field", field) } if len(filters) > 0 { values.Add("filters", strings.Join(filters, ",")) } // text will not be blank values.Add("text", text) analyzeUrl += "?" + values.Encode() body, err := api.DoCommand("GET", analyzeUrl, nil) if err != nil { return retval, err } if err == nil { // marshall into json jsonErr := json.Unmarshal(body, &retval) if jsonErr != nil { return retval, jsonErr } } return retval, err }
// Snapshot allows to explicitly perform a snapshot through the gateway of one or more indices (backup them). // By default, each index gateway periodically snapshot changes, though it can be disabled and be controlled completely through this API. // see http://www.elasticsearch.org/guide/reference/api/admin-indices-gateway-snapshot/ func Snapshot(indices ...string) (api.ExtendedStatus, error) { var retval api.ExtendedStatus var url string if len(indices) > 0 { url = fmt.Sprintf("/%s/_gateway/snapshot", strings.Join(indices, ",")) } else { url = fmt.Sprintf("/_gateway/snapshot") } body, err := api.DoCommand("POST", url, nil) if err != nil { return retval, err } if err == nil { // marshall into json jsonErr := json.Unmarshal(body, &retval) if jsonErr != nil { return retval, jsonErr } } return retval, err }
// Validate allows a user to validate a potentially expensive query without executing it. // see http://www.elasticsearch.org/guide/reference/api/validate.html func Validate(pretty bool, index string, _type string, query string, explain bool) (api.BaseResponse, error) { var url string var retval api.BaseResponse if len(_type) > 0 { url = fmt.Sprintf("/%s/%s/_validate/query?q=%s&%s&explain=%t", index, _type, query, api.Pretty(pretty), explain) } else { url = fmt.Sprintf("/%s/_validate/query?q=%s&%s&explain=%t", index, query, api.Pretty(pretty), explain) } body, err := api.DoCommand("GET", url, nil) if err != nil { return retval, err } if err == nil { // marshall into json jsonErr := json.Unmarshal(body, &retval) if jsonErr != nil { return retval, jsonErr } } fmt.Println(body) return retval, err }
// Status lists status details of all indices or the specified index. // http://www.elasticsearch.org/guide/reference/api/admin-indices-status.html func Status(pretty bool, indices ...string) (api.BaseResponse, error) { var retval api.BaseResponse var url string if len(indices) > 0 { url = fmt.Sprintf("/%s/_status?%s", strings.Join(indices, ","), api.Pretty(pretty)) } else { url = fmt.Sprintf("/_status?%s", 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(body, &retval) if jsonErr != nil { return retval, jsonErr } } return retval, err }
// SearchUri performs the simplest possible query in url string // params: // @index: the elasticsearch index // @_type: optional ("" if not used) search specific type in this index // @query: valid string lucene search syntax // // out, err := SearchUri("github","",`user:kimchy` ,"", 0) // // produces a request like this: host:9200/github/_search?q=user:kimchy" // // http://www.elasticsearch.org/guide/reference/api/search/uri-request.html func SearchUri(index, _type string, query, scroll string, scan int) (SearchResult, error) { var uriVal string var retval SearchResult query = url.QueryEscape(query) if len(_type) > 0 && _type != "*" { uriVal = fmt.Sprintf("/%s/%s/_search?q=%s%s%s", index, _type, query, api.Scroll(scroll), api.Scan(scan)) } else { uriVal = fmt.Sprintf("/%s/_search?q=%s%s%s", index, query, api.Scroll(scroll), api.Scan(scan)) } //log.Println(uriVal) body, err := api.DoCommand("GET", uriVal, 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 } } return retval, err }