11# Elastic
22
3- Elastic is an [ Elasticsearch] ( http://www.elasticsearch.org/ ) client for [ Go] ( http://www.golang.org/ ) .
3+ Elastic is an [ Elasticsearch] ( http://www.elasticsearch.org/ ) client for the
4+ [ Go] ( http://www.golang.org/ ) programming language.
45
56[ ![ Build Status] ( https://travis-ci.org/olivere/elastic.svg?branch=master )] ( https://travis-ci.org/olivere/elastic )
67[ ![ Godoc] ( http://img.shields.io/badge/godoc-reference-blue.svg?style=flat )] ( https://godoc.org/github.com/olivere/elastic )
78[ ![ license] ( http://img.shields.io/badge/license-MIT-red.svg?style=flat )] ( https://raw.githubusercontent.com/olivere/elastic/master/LICENSE )
89
9- ## Releases
10+ See the [ wiki ] ( https://github.com/olivere/elastic/wiki ) for additional information about Elastic.
1011
11- I'm about to release a new version of Elastic soon. To continue using
12- the 1.0 version, all you need to do is to go-get a new URL and switch
13- your import path. We're using [ gopkg.in] ( http://gokpg.in/ ) for that.
14- Here's how to use Elastic version 1:
1512
16- ``` sh
17- $ go get -u gopkg.in/olivere/elastic.v1
18- ```
13+ ## Releases
1914
20- In your Go code:
15+ ### Current version
2116
22- ``` go
23- import " gopkg.in/olivere/elastic.v1 "
24- ```
17+ This is the source code of the current version of Elastic (version 2).
18+
19+ ### Earlier versions
2520
26- Once version 2 is ready, you can get it like this:
21+ If you came from an earlier version and found that you cannot update, don't
22+ worry. Earlier versions are still available. All you need to do is go-get
23+ them and change your import path. See below for details. Here's what you
24+ need to do to use Elastic version 1:
2725
2826``` sh
29- $ go get -u gopkg.in/olivere/elastic.v2
27+ $ go get gopkg.in/olivere/elastic.v1
3028```
3129
32- In your Go code :
30+ Then change your import path :
3331
3432``` go
35- import " gopkg.in/olivere/elastic.v2 "
33+ import " gopkg.in/olivere/elastic.v1 "
3634```
3735
38- If you continue to use ` github.com/olivere/elastic ` in your code base,
39- you are following master. I try to keep master stable, but things might
40- break now and then.
4136
4237## Status
4338
44- We use Elastic in production for more than two years now.
45- Although Elastic is quite stable from our experience, we don't have
46- a stable API yet. The reason for this is that Elasticsearch changes quite
47- often and at a fast pace. At this moment we focus on features, not on a
48- stable API. Having said that, there have been no huge changes for the last
49- 12 months that required you to rewrite your application big time.
39+ We use Elastic in production since 2012. Although Elastic is quite stable
40+ from our experience, we don't have a stable API yet. The reason for this
41+ is that Elasticsearch changes quite often and at a fast pace.
42+ At this moment we focus on features, not on a stable API.
43+
44+ Having said that, there have been no big API changes that required you
45+ to rewrite your application big time.
5046More often than not it's renaming APIs and adding/removing features
5147so that we are in sync with the Elasticsearch API.
5248
@@ -58,155 +54,42 @@ not yet implemented in Elastic (see below for details).
5854I add features and APIs as required. It's straightforward
5955to implement missing pieces. I'm accepting pull requests :-)
6056
61- Having said that, I hope you find the project useful. Fork it
62- as you like.
63-
64- ## Usage
65-
66- The first thing you do is to create a Client. The client takes a http.Client
67- and (optionally) a list of URLs to the Elasticsearch servers as arguments.
68- If the list of URLs is empty, http://localhost:9200 is used by default.
69- You typically create one client for your app.
70-
71- ``` go
72- client , err := elastic.NewClient (http.DefaultClient )
73- if err != nil {
74- // Handle error
75- }
76- ```
77-
78- Notice that you can pass your own http.Client implementation here. You can
79- also pass more than one URL to a client. Elastic pings the URLs periodically
80- and takes the first to succeed. By doing this periodically, Elastic provides
81- automatic failover, e.g. when an Elasticsearch server goes down during
82- updates.
57+ Having said that, I hope you find the project useful.
8358
84- If no Elasticsearch server is available, services will fail when creating
85- a new request and will return ` ErrNoClient ` . While this method is not very
86- sophisticated and might result in timeouts, it is robust enough for our
87- use cases. Pull requests are welcome.
8859
89- ``` go
90- client , err := elastic.NewClient (http.DefaultClient , " http://1.2.3.4:9200" , " http://1.2.3.5:9200" )
91- if err != nil {
92- // Handle error
93- }
94- ```
95-
96- A Client provides services. The services usually come with a variety of
97- methods to prepare the query and a ` Do ` function to execute it against the
98- Elasticsearch REST interface and return a response. Here is an example
99- of the IndexExists service that checks if a given index already exists.
100-
101- ``` go
102- exists , err := client.IndexExists (" twitter" ).Do ()
103- if err != nil {
104- // Handle error
105- }
106- if !exists {
107- // Index does not exist yet.
108- }
109- ```
60+ ## Usage
11061
111- Look up the documentation for Client to get an idea of the services provided
112- and what kinds of responses you get when executing the ` Do ` function of a service .
62+ The first thing you do is to create a Client. The client connects to
63+ Elasticsearch on http://127.0.0.1:9200 by default .
11364
114- Here's a longer example:
65+ You typically create one client for your app. Here's a complete example.
11566
11667``` go
117- // Import Elastic
118- import (
119- " github.com/olivere/elastic"
120- )
121-
122- // Obtain a client. You can provide your own HTTP client here.
123- client , err := elastic.NewClient (http.DefaultClient )
124- if err != nil {
125- // Handle error
126- panic (err)
127- }
128-
129- // Ping the Elasticsearch server to get e.g. the version number
130- info , code , err := client.Ping ().Do ()
68+ // Create a client
69+ client , err := elastic.NewClient ()
13170if err != nil {
13271 // Handle error
133- panic (err)
134- }
135- fmt.Printf (" Elasticsearch returned with code %d and version %s " , code, info.Version .Number )
136-
137- // Getting the ES version number is quite common, so there's a shortcut
138- esversion , err := client.ElasticsearchVersion (" http://127.0.0.1:9200" )
139- if err != nil {
140- // Handle error
141- panic (err)
142- }
143- fmt.Printf (" Elasticsearch version %s " , esversion)
144-
145- // Use the IndexExists service to check if a specified index exists.
146- exists , err := client.IndexExists (" twitter" ).Do ()
147- if err != nil {
148- // Handle error
149- panic (err)
150- }
151- if !exists {
152- // Create a new index.
153- createIndex , err := client.CreateIndex (" twitter" ).Do ()
154- if err != nil {
155- // Handle error
156- panic (err)
157- }
158- if !createIndex.Acknowledged {
159- // Not acknowledged
160- }
161- }
162-
163- // Index a tweet (using JSON serialization)
164- tweet1 := Tweet {User: " olivere" , Message : " Take Five" , Retweets : 0 }
165- put1 , err := client.Index ().
166- Index (" twitter" ).
167- Type (" tweet" ).
168- Id (" 1" ).
169- BodyJson (tweet1).
170- Do ()
171- if err != nil {
172- // Handle error
173- panic (err)
17472}
175- fmt.Printf (" Indexed tweet %s to index %s , type %s \n " , put1.Id , put1.Index , put1.Type )
17673
177- // Index a second tweet (by string)
178- tweet2 := ` {"user" : "olivere", "message" : "It's a Raggy Waltz"}`
179- put2 , err := client.Index ().
180- Index (" twitter" ).
181- Type (" tweet" ).
182- Id (" 2" ).
183- BodyString (tweet2).
184- Do ()
74+ // Create an index
75+ _, err = client.CreateIndex (" twitter" ).Do ()
18576if err != nil {
18677 // Handle error
18778 panic (err)
18879}
189- fmt.Printf (" Indexed tweet %s to index %s , type %s \n " , put2.Id , put2.Index , put2.Type )
19080
191- // Get tweet with specified ID
192- get1 , err := client.Get ().
81+ // Add a document to the index
82+ tweet := Tweet {User: " olivere" , Message : " Take Five" }
83+ _, err = client.Index ().
19384 Index (" twitter" ).
19485 Type (" tweet" ).
19586 Id (" 1" ).
87+ BodyJson (tweet).
19688 Do ()
19789if err != nil {
19890 // Handle error
19991 panic (err)
20092}
201- if get1.Found {
202- fmt.Printf (" Got document %s in version %d from index %s , type %s \n " , get1.Id , get1.Version , get1.Index , get1.Type )
203- }
204-
205- // Flush to make sure the documents got written.
206- _, err = client.Flush ().Index (" twitter" ).Do ()
207- if err != nil {
208- panic (err)
209- }
21093
21194// Search with a term query
21295termQuery := elastic.NewTermQuery (" user" , " olivere" )
@@ -250,36 +133,16 @@ if searchResult.Hits != nil {
250133 fmt.Print (" Found no tweets\n " )
251134}
252135
253-
254- // Update a tweet by the update API of Elasticsearch.
255- // We just increment the number of retweets.
256- update , err := client.Update ().Index (" twitter" ).Type (" tweet" ).Id (" 1" ).
257- Script (" ctx._source.retweets += num" ).
258- ScriptParams (map [string ]interface {}{" num" : 1 }).
259- Upsert (map [string ]interface {}{" retweets" : 0 }).
260- Do ()
261- if err != nil {
262- // Handle error
263- panic (err)
264- }
265- fmt.Printf (" New version of tweet %q is now %d " , update.Id , update.Version )
266-
267- // ...
268-
269- // Delete an index.
270- deleteIndex , err := client.DeleteIndex (" twitter" ).Do ()
136+ // Delete the index again
137+ _, err = client.DeleteIndex (" twitter" ).Do ()
271138if err != nil {
272139 // Handle error
273140 panic (err)
274141}
275- if !deleteIndex.Acknowledged {
276- // Not acknowledged
277- }
278142```
279143
280- ## Installation
144+ See the [ wiki ] ( /olivere/elastic/wiki ) for more details.
281145
282- Grab the code with ` go get github.com/olivere/elastic ` .
283146
284147## API Status
285148
@@ -300,7 +163,7 @@ Here's the current API status.
300163- [ ] Multi term vectors
301164- [x] Count
302165- [ ] Validate
303- - [ ] Explain
166+ - [x ] Explain
304167- [x] Search
305168- [ ] Search shards
306169- [x] Search template
@@ -317,16 +180,16 @@ Here's the current API status.
317180- [x] Delete index
318181- [x] Indices exists
319182- [x] Open/close index
320- - [ ] Put mapping
321- - [ ] Get mapping
183+ - [x ] Put mapping
184+ - [x ] Get mapping
322185- [ ] Get field mapping
323186- [ ] Types exist
324- - [ ] Delete mapping
187+ - [x ] Delete mapping
325188- [x] Index aliases
326189- [ ] Update indices settings
327190- [ ] Get settings
328191- [ ] Analyze
329- - [ ] Index templates
192+ - [x ] Index templates
330193- [ ] Warmers
331194- [ ] Status
332195- [ ] Indices stats
@@ -359,7 +222,7 @@ on the command line.
359222- [ ] Cluster reroute
360223- [ ] Cluster update settings
361224- [ ] Nodes stats
362- - [ ] Nodes info
225+ - [x ] Nodes info
363226- [ ] Nodes hot_threads
364227- [ ] Nodes shutdown
365228
@@ -491,7 +354,7 @@ on the command line.
491354### Scan
492355
493356Scrolling through documents (e.g. ` search_type=scan ` ) are implemented via
494- the ` Scroll ` and ` Scan ` services.
357+ the ` Scroll ` and ` Scan ` services. The ` ClearScroll ` API is implemented as well.
495358
496359## How to contribute
497360
0 commit comments