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

Commit 3e858d6

Browse files
committed
Put mappings, API help etc.
1 parent 933ad14 commit 3e858d6

13 files changed

+156
-17
lines changed

README.md

+35-4
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,10 @@
33
`es` is a small command line tool to interact with the
44
ElasticSearch search engine.
55

6+
Notice that you could do all of this with `curl` commands, as
7+
seen on the [ElasticSearch API](http://www.elasticsearch.org/guide/reference/api/).
8+
However, you probably save a few keystrokes with `es`.
9+
610
## Setup
711

812
You need to compile yourself currently:
@@ -14,7 +18,17 @@ You need to compile yourself currently:
1418

1519
## Commands
1620

17-
Lets list indices.
21+
Before we start, you can always lookup the ElasticSearch API via
22+
the `api` command, like so:
23+
24+
$ es api indices
25+
26+
The `api` command will open up a browser window with the API page
27+
that matches the specified command. You can find the complete
28+
[ElasticSearch API here](http://www.elasticsearch.org/guide/reference/api/).
29+
30+
Let's get started. First we list existing indices, either all of them
31+
or via a regular expression.
1832

1933
$ es indices
2034
master
@@ -24,7 +38,9 @@ Lets list indices.
2438
master
2539
marvel
2640

27-
And create a new index. This can be useful if you e.g. added templates.
41+
Let's create a new index. Use the -f flag to force creation, i.e. it will
42+
not print an error if the index already exists (and won't touch the
43+
existing index).
2844

2945
$ es create twitter
3046
$ es indices
@@ -36,7 +52,7 @@ And create a new index. This can be useful if you e.g. added templates.
3652
Error: IndexAlreadyExistsException[[twitter] Already exists] (400)
3753
$ es create -f twitter
3854

39-
Now, lets delete indices.
55+
Delete indices again.
4056

4157
$ es delete twitter
4258
$ es indices
@@ -51,7 +67,7 @@ Now, lets delete indices.
5167
marvel
5268
dummy
5369

54-
Let's review the mapping of an index.
70+
Let's review mappings, and even create mappings from the command line.
5571

5672
$ es mapping dummy
5773
{
@@ -60,6 +76,21 @@ Let's review the mapping of an index.
6076
}
6177
$ es mapping nonexistent
6278
Error: IndexMissingException[[nonexistent] missing] (404)
79+
$ es create twitter
80+
$ es put-mapping twitter tweet < tweet-mapping.json
81+
$ es mapping twitter
82+
{
83+
"twitter" : {
84+
"tweet" : {
85+
"properties" : {
86+
"message" : {
87+
"type" : "string",
88+
"store" : "yes"
89+
}
90+
}
91+
}
92+
}
93+
}
6394

6495
Templates, oh how I love thee... here's a sample session.
6596

api.go

+42
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,56 @@ package main
33
import (
44
"bytes"
55
"encoding/json"
6+
"fmt"
67
"log"
78
"io"
89
"io/ioutil"
910
"net/http"
11+
"os"
12+
"os/exec"
1013
"runtime"
1114
"strings"
1215
)
1316

17+
var cmdApi = &Command{
18+
Usage: "api <command>",
19+
Short: "show API for command",
20+
Long: `Opens browser with API details for the specified command.`,
21+
ApiUrl: "",
22+
}
23+
24+
func init() {
25+
cmdApi.Run = runApi
26+
}
27+
28+
func runApi(cmd *Command, args []string) {
29+
if len(args) == 0 {
30+
printUsage()
31+
return
32+
}
33+
34+
if len(args) != 1 {
35+
log.Fatal("too many arguments")
36+
}
37+
38+
for _, cmd := range commands {
39+
if cmd.Name() == args[0] && len(cmd.ApiUrl) > 0 {
40+
// open browser
41+
openBrowser := "open"
42+
if _, err := exec.LookPath("xdg-open"); err == nil {
43+
openBrowser = "xdg-open"
44+
}
45+
exec.Command(openBrowser, cmd.ApiUrl).Start()
46+
return
47+
}
48+
}
49+
50+
fmt.Fprintf(os.Stderr, "No API information for command: %s\n", args[0])
51+
os.Exit(2)
52+
}
53+
54+
// Generic API for accessing ElasticSearch server starts here.
55+
1456
type Request http.Request
1557

1658
func ESReq(method, path string) *Request {

create_index.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,15 @@ import (
88
var cmdCreateIndex = &Command{
99
Run: runCreateIndex,
1010
Usage: "create [-f] <index>",
11-
Short: "creates index",
11+
Short: "create index",
1212
Long: `
1313
Creates an empty index.
1414
1515
Example:
1616
1717
$ es create marvel
1818
`,
19+
ApiUrl: "http://www.elasticsearch.org/guide/reference/api/admin-indices-create-index.html",
1920
}
2021

2122
func init() {

create_template.go

+1
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ Example:
1818
1919
$ es create-template marvel < marvel-template.json
2020
`,
21+
ApiUrl: "http://www.elasticsearch.org/guide/reference/api/admin-indices-templates.html",
2122
}
2223

2324
func runCreateTemplate(cmd *Command, args []string) {

delete_index.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,15 @@ import (
88
var cmdDeleteIndex = &Command{
99
Run: runDeleteIndex,
1010
Usage: "delete [-f] <index>",
11-
Short: "deletes index",
11+
Short: "delete index",
1212
Long: `
1313
Deletes an index.
1414
1515
Example:
1616
1717
$ es delete marvel
1818
`,
19+
ApiUrl: "http://www.elasticsearch.org/guide/reference/api/admin-indices-delete-index.html",
1920
}
2021

2122
func init() {

delete_template.go

+5-4
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,11 @@ import (
66
)
77

88
var cmdDeleteTemplate = &Command{
9-
Run: runDeleteTemplate,
10-
Usage: "delete-template [-f] <template>",
11-
Short: "deletes a template",
12-
Long: `Deletes the specified template.`,
9+
Run: runDeleteTemplate,
10+
Usage: "delete-template [-f] <template>",
11+
Short: "delete template",
12+
Long: `Deletes the specified template.`,
13+
ApiUrl: "http://www.elasticsearch.org/guide/reference/api/admin-indices-templates.html",
1314
}
1415

1516
func init() {

help.go

+5-4
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,11 @@ import (
77
)
88

99
var cmdVersion = &Command{
10-
Run: runVersion,
11-
Usage: "version",
12-
Short: "show version",
13-
Long: `Shows the version string.`,
10+
Run: runVersion,
11+
Usage: "version",
12+
Short: "show version",
13+
Long: `Shows the version string.`,
14+
ApiUrl: "",
1415
}
1516

1617
func runVersion(cmd *Command, args []string) {

indices.go

+1
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ Example:
2121
$ es indices 'mas.*'
2222
master
2323
`,
24+
ApiUrl: "http://www.elasticsearch.org/guide/reference/api/index_.html",
2425
}
2526

2627
func init() {

main.go

+6-3
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@ type Command struct {
2222
Usage string
2323
Short string
2424
Long string
25+
26+
ApiUrl string
2527
}
2628

2729
func (c *Command) printUsage() {
@@ -41,18 +43,19 @@ func (c *Command) Name() string {
4143
// Running es on the command line will print these commands in order.
4244
var commands = []*Command{
4345
cmdIndices,
44-
cmdMapping,
45-
4646
cmdCreateIndex,
4747
cmdDeleteIndex,
4848

49+
cmdMapping,
50+
cmdPutMapping,
51+
4952
cmdTemplates,
5053
cmdTemplate,
5154
cmdCreateTemplate,
5255
cmdDeleteTemplate,
5356

5457
cmdVersion,
55-
58+
cmdApi,
5659
cmdHelp,
5760
}
5861

mapping.go

+1
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ Example:
1717
1818
$ es mapping twitter
1919
`,
20+
ApiUrl: "http://www.elasticsearch.org/guide/reference/api/admin-indices-get-mapping.html",
2021
}
2122

2223
func runMapping(cmd *Command, args []string) {

put_mapping.go

+54
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
package main
2+
3+
import (
4+
"bufio"
5+
"encoding/json"
6+
"log"
7+
"os"
8+
)
9+
10+
var cmdPutMapping = &Command{
11+
Run: runPutMapping,
12+
Usage: "put-mapping <index> <type>",
13+
Short: "create or update mapping",
14+
Long: `
15+
Creates or updates a mapping for a document type on an index.
16+
http://www.elasticsearch.org/guide/reference/api/admin-indices-put-mapping.html
17+
18+
Example:
19+
20+
$ es put-mapping twitter tweet < tweet-mapping.json
21+
`,
22+
ApiUrl: "http://www.elasticsearch.org/guide/reference/api/admin-indices-put-mapping.html",
23+
}
24+
25+
func runPutMapping(cmd *Command, args []string) {
26+
if len(args) != 2 {
27+
cmd.printUsage()
28+
os.Exit(1)
29+
}
30+
31+
index := args[0]
32+
doctype := args[1]
33+
34+
var response struct {
35+
Ok bool `json:"ok,omitempty"`
36+
Ack bool `json:"acknowledged,omitempty"`
37+
Error string `json:"error,omitempty"`
38+
Status int `json:"status,omitempty"`
39+
}
40+
41+
// parse Stdin into JSON
42+
var data interface{}
43+
reader := bufio.NewReader(os.Stdin)
44+
if err := json.NewDecoder(reader).Decode(&data); err != nil {
45+
log.Fatal("invalid json\n")
46+
}
47+
48+
req := ESReq("PUT", "/"+index+"/"+doctype+"/_mapping")
49+
req.SetBodyJson(data)
50+
req.Do(&response)
51+
if len(response.Error) > 0 {
52+
log.Fatalf("Error: %v (%v)\n", response.Error, response.Status)
53+
}
54+
}

template.go

+1
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ Example:
1919
{
2020
}
2121
`,
22+
ApiUrl: "http://www.elasticsearch.org/guide/reference/api/admin-indices-templates.html",
2223
}
2324

2425
func init() {

templates.go

+1
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ Example:
2121
$ es templates 'mas.*'
2222
master-template
2323
`,
24+
ApiUrl: "http://www.elasticsearch.org/guide/reference/api/admin-indices-templates.html",
2425
}
2526

2627
func init() {

0 commit comments

Comments
 (0)