Skip to content

Commit b2a8539

Browse files
committed
Fix main example for 5.0
Close #412
1 parent d4c6c8a commit b2a8539

File tree

1 file changed

+43
-4
lines changed

1 file changed

+43
-4
lines changed

example_test.go

+43-4
Original file line numberDiff line numberDiff line change
@@ -47,15 +47,15 @@ func Example() {
4747
// Handle error
4848
panic(err)
4949
}
50-
fmt.Printf("Elasticsearch returned with code %d and version %s", code, info.Version.Number)
50+
fmt.Printf("Elasticsearch returned with code %d and version %s\n", code, info.Version.Number)
5151

5252
// Getting the ES version number is quite common, so there's a shortcut
5353
esversion, err := client.ElasticsearchVersion("http://127.0.0.1:9200")
5454
if err != nil {
5555
// Handle error
5656
panic(err)
5757
}
58-
fmt.Printf("Elasticsearch version %s", esversion)
58+
fmt.Printf("Elasticsearch version %s\n", esversion)
5959

6060
// Use the IndexExists service to check if a specified index exists.
6161
exists, err := client.IndexExists("twitter").Do(context.Background())
@@ -65,7 +65,46 @@ func Example() {
6565
}
6666
if !exists {
6767
// Create a new index.
68-
createIndex, err := client.CreateIndex("twitter").Do(context.Background())
68+
mapping := `
69+
{
70+
"settings":{
71+
"number_of_shards":1,
72+
"number_of_replicas":0
73+
},
74+
"mappings":{
75+
"_default_": {
76+
"_all": {
77+
"enabled": true
78+
}
79+
},
80+
"tweet":{
81+
"properties":{
82+
"user":{
83+
"type":"keyword"
84+
},
85+
"message":{
86+
"type":"text",
87+
"store": true,
88+
"fielddata": true
89+
},
90+
"retweets":{
91+
"type":"long"
92+
},
93+
"tags":{
94+
"type":"keyword"
95+
},
96+
"location":{
97+
"type":"geo_point"
98+
},
99+
"suggest_field":{
100+
"type":"completion"
101+
}
102+
}
103+
}
104+
}
105+
}
106+
`
107+
createIndex, err := client.CreateIndex("twitter").Body(mapping).Do(context.Background())
69108
if err != nil {
70109
// Handle error
71110
panic(err)
@@ -178,7 +217,7 @@ func Example() {
178217

179218
// Update a tweet by the update API of Elasticsearch.
180219
// We just increment the number of retweets.
181-
script := elastic.NewScript("ctx._source.retweets += num").Param("num", 1)
220+
script := elastic.NewScript("ctx._source.retweets += params.num").Param("num", 1)
182221
update, err := client.Update().Index("twitter").Type("tweet").Id("1").
183222
Script(script).
184223
Upsert(map[string]interface{}{"retweets": 0}).

0 commit comments

Comments
 (0)