Skip to content
This repository has been archived by the owner on Sep 27, 2018. It is now read-only.

Commit

Permalink
New commands: settings, flush-enable, flush-disable
Browse files Browse the repository at this point in the history
* Flush enable/disable are rather helpful when performing
  backups via rsync (see https://gist.github.com/karussell/1074906
  for example)
  • Loading branch information
olivere committed Mar 14, 2013
1 parent 75b11d3 commit 1b7037b
Show file tree
Hide file tree
Showing 4 changed files with 171 additions and 0 deletions.
63 changes: 63 additions & 0 deletions flush-disable.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
package main

import (
"log"
)

var cmdFlushDisable = &Command{
Run: runFlushDisable,
Usage: "flush-disable [index]",
Short: "disable flush",
Long: `
Disables flush. Useful if you want to prevent accidential flushes
from happening, e.g. while performing a backup via rsync.
Use flush-enable to revert this action.
This is basically the same as:
curl -XPUT 'localhost:9200/_settings' -d '{
"index" : {
"translog.disable_flush" : "true"
}
}'
Example:
$ es flush-disable
$ es flush-disable twitter
`,
ApiUrl: "http://www.elasticsearch.org/guide/reference/api/admin-indices-update-settings.html",
}

func runFlushDisable(cmd *Command, args []string) {
index := ""
if len(args) >= 1 {
index = args[0]
}

translogReq := make(map[string]interface{})
translogReq["translog.disable_flush"] = "true"
data := make(map[string]interface{})
data["index"] = translogReq

var response struct {
Ok bool `json:"ok,omitempty"`
Error string `json:"error,omitempty"`
Status int `json:"status,omitempty"`
}

path := ""
if index != "" {
path += "/"+index
}
path += "/_settings"

req := ESReq("PUT", path)
req.SetBodyJson(data)
req.Do(&response)

if len(response.Error) > 0 {
log.Fatalf("Error: %v (%v)\n", response.Error, response.Status)
}
}
62 changes: 62 additions & 0 deletions flush-enable.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
package main

import (
"log"
)

var cmdFlushEnable = &Command{
Run: runFlushEnable,
Usage: "flush-enable [index]",
Short: "enable flush",
Long: `
Enables flush. Useful to return to normal usage after flush has
been temporarily disabled (see flush-disable) in order to
perform a backup (e.g. via rsync).
This is basically the same as:
curl -XPUT 'localhost:9200/_settings' -d '{
"index" : {
"translog.disable_flush" : "false"
}
}'
Example:
$ es flush-enable
$ es flush-enable twitter
`,
ApiUrl: "http://www.elasticsearch.org/guide/reference/api/admin-indices-update-settings.html",
}

func runFlushEnable(cmd *Command, args []string) {
index := ""
if len(args) >= 1 {
index = args[0]
}

translogReq := make(map[string]interface{})
translogReq["translog.disable_flush"] = "false"
data := make(map[string]interface{})
data["index"] = translogReq

var response struct {
Ok bool `json:"ok,omitempty"`
Error string `json:"error,omitempty"`
Status int `json:"status,omitempty"`
}

path := ""
if index != "" {
path += "/"+index
}
path += "/_settings"

req := ESReq("PUT", path)
req.SetBodyJson(data)
req.Do(&response)

if len(response.Error) > 0 {
log.Fatalf("Error: %v (%v)\n", response.Error, response.Status)
}
}
3 changes: 3 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,11 +50,14 @@ var commands = []*Command{
cmdIndices,
cmdCreateIndex,
cmdDeleteIndex,
cmdSettings,
cmdStatus,
cmdStats,
cmdRefresh,
cmdOptimize,
cmdFlush,
cmdFlushDisable,
cmdFlushEnable,

cmdAliases,
cmdIndexAliases,
Expand Down
43 changes: 43 additions & 0 deletions settings.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package main

import (
"fmt"
"log"
)

var cmdSettings = &Command{
Run: runSettings,
Usage: "settings [index]",
Short: "prints index settings",
Long: `
Prints index settings.
Example:
$ es settings
$ es settings twitter
`,
ApiUrl: "http://www.elasticsearch.org/guide/reference/api/admin-indices-get-settings.html",
}

func runSettings(cmd *Command, args []string) {
index := ""
if len(args) >= 1 {
index = args[0]
}

var response map[string]interface{}

var body string
if len(index) > 0 {
body = ESReq("GET", "/"+index+"/_settings?pretty=1").Do(&response)
} else {
body = ESReq("GET", "/_settings?pretty=1").Do(&response)
}

if error, ok := response["error"]; ok {
status, _ := response["status"]
log.Fatalf("Error: %v (%v)\n", error, status)
}
fmt.Print(body)
}

0 comments on commit 1b7037b

Please sign in to comment.