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

Commit 01df596

Browse files
committed
Add optimize, refresh, and flush
1 parent c7fbe9b commit 01df596

File tree

4 files changed

+160
-0
lines changed

4 files changed

+160
-0
lines changed

flush.go

+45
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
package main
2+
3+
import (
4+
"log"
5+
)
6+
7+
var cmdFlush = &Command{
8+
Run: runFlush,
9+
Usage: "flush [index]",
10+
Short: "flushes indices",
11+
Long: `
12+
Flushes data to the index storage, frees memory and clears the
13+
internal transaction log. You often need to flush before updating
14+
ElasticSearch to a new version.
15+
16+
Example:
17+
18+
$ es flush
19+
$ es flush twitter
20+
`,
21+
ApiUrl: "http://www.elasticsearch.org/guide/reference/api/admin-indices-flush.html",
22+
}
23+
24+
func runFlush(cmd *Command, args []string) {
25+
index := ""
26+
if len(args) >= 1 {
27+
index = args[0]
28+
}
29+
30+
var response struct {
31+
Ok bool `json:"ok,omitempty"`
32+
Error string `json:"error,omitempty"`
33+
Status int `json:"status,omitempty"`
34+
}
35+
36+
if len(index) > 0 {
37+
ESReq("POST", "/"+index+"/_flush").Do(&response)
38+
} else {
39+
ESReq("POST", "/_flush").Do(&response)
40+
}
41+
42+
if len(response.Error) > 0 {
43+
log.Fatalf("Error: %v (%v)\n", response.Error, response.Status)
44+
}
45+
}

main.go

+5
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ const (
1414
)
1515

1616
var force bool
17+
var flush bool
18+
var refresh bool
1719

1820
type Command struct {
1921
Run func(cmd *Command, args []string)
@@ -47,6 +49,9 @@ var commands = []*Command{
4749
cmdDeleteIndex,
4850
cmdStatus,
4951
cmdStats,
52+
cmdRefresh,
53+
cmdOptimize,
54+
cmdFlush,
5055

5156
cmdMapping,
5257
cmdPutMapping,

optimize.go

+65
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
package main
2+
3+
import (
4+
"log"
5+
"net/url"
6+
)
7+
8+
var cmdOptimize = &Command{
9+
Run: runOptimize,
10+
Usage: "optimize [-r -f] [index]",
11+
Short: "optimizes indices",
12+
Long: `
13+
Optimize basically optimizes the index for faster search operations.
14+
15+
Use -r, -r=true, -r=false to enable/disable refresh (enabled by default).
16+
Use -f, -f=true, -f=false to enable/disable flush (enabled by default).
17+
18+
Example:
19+
20+
$ es optimize
21+
$ es optimize -f=false twitter
22+
$ es optimize twitter
23+
`,
24+
ApiUrl: "http://www.elasticsearch.org/guide/reference/api/admin-indices-optimize.html",
25+
}
26+
27+
func init() {
28+
cmdOptimize.Flag.BoolVar(&flush, "f", true, "flush")
29+
cmdOptimize.Flag.BoolVar(&refresh, "r", true, "refresh")
30+
}
31+
32+
func runOptimize(cmd *Command, args []string) {
33+
index := ""
34+
if len(args) >= 1 {
35+
index = args[len(args)-1]
36+
}
37+
38+
values := url.Values{}
39+
if flush {
40+
values.Set("flush", "1")
41+
} else {
42+
values.Set("flush", "0")
43+
}
44+
if refresh {
45+
values.Set("refresh", "1")
46+
} else {
47+
values.Set("refresh", "0")
48+
}
49+
50+
var response struct {
51+
Ok bool `json:"ok,omitempty"`
52+
Error string `json:"error,omitempty"`
53+
Status int `json:"status,omitempty"`
54+
}
55+
56+
if len(index) > 0 {
57+
ESReq("POST", "/"+index+"/_optimize?"+values.Encode()).Do(&response)
58+
} else {
59+
ESReq("POST", "/_optimize?"+values.Encode()).Do(&response)
60+
}
61+
62+
if len(response.Error) > 0 {
63+
log.Fatalf("Error: %v (%v)\n", response.Error, response.Status)
64+
}
65+
}

refresh.go

+45
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
package main
2+
3+
import (
4+
"log"
5+
)
6+
7+
var cmdRefresh = &Command{
8+
Run: runRefresh,
9+
Usage: "refresh [index]",
10+
Short: "refreshes indices",
11+
Long: `
12+
Refresh allows to explicitly refresh one or more index,
13+
making all operations performed since the last refresh
14+
available for search.
15+
16+
Example:
17+
18+
$ es refresh
19+
$ es refresh twitter
20+
`,
21+
ApiUrl: "http://www.elasticsearch.org/guide/reference/api/admin-indices-refresh.html",
22+
}
23+
24+
func runRefresh(cmd *Command, args []string) {
25+
index := ""
26+
if len(args) >= 1 {
27+
index = args[0]
28+
}
29+
30+
var response struct {
31+
Ok bool `json:"ok,omitempty"`
32+
Error string `json:"error,omitempty"`
33+
Status int `json:"status,omitempty"`
34+
}
35+
36+
if len(index) > 0 {
37+
ESReq("POST", "/"+index+"/_refresh").Do(&response)
38+
} else {
39+
ESReq("POST", "/_refresh").Do(&response)
40+
}
41+
42+
if len(response.Error) > 0 {
43+
log.Fatalf("Error: %v (%v)\n", response.Error, response.Status)
44+
}
45+
}

0 commit comments

Comments
 (0)