Elastic-Go Elasticsearch的Go语言客户端开发包 Elastic-Go

xuanbao • 9739 次点击    
这是一个分享于 的项目,其中的信息可能已经有所发展或是发生改变。
Elastic是 [Elasticsearch](http://www.oschina.net/p/elasticsearch) 的 [Go](http://www.oschina.net/p/go) 语言客户端开发包。 快速入门: <pre class="brush:cpp ;toolbar: true; auto-links: false;">// Create a client client, err := elastic.NewClient() if err != nil {     // Handle error } // Create an index _, err = client.CreateIndex(&#34;twitter&#34;).Do() if err != nil {     // Handle error     panic(err) } // Add a document to the index tweet := Tweet{User: &#34;olivere&#34;, Message: &#34;Take Five&#34;} _, err = client.Index().     Index(&#34;twitter&#34;).     Type(&#34;tweet&#34;).     Id(&#34;1&#34;).     BodyJson(tweet).     Refresh(true).     Do() if err != nil {     // Handle error     panic(err) } // Search with a term query termQuery := elastic.NewTermQuery(&#34;user&#34;, &#34;olivere&#34;) searchResult, err := client.Search().     Index(&#34;twitter&#34;).   // search in index &#34;twitter&#34;     Query(termQuery).   // specify the query     Sort(&#34;user&#34;, true). // sort by &#34;user&#34; field, ascending     From(0).Size(10).   // take documents 0-9     Pretty(true).       // pretty print request and response JSON     Do()                // execute if err != nil {     // Handle error     panic(err) } // searchResult is of type SearchResult and returns hits, suggestions, // and all kinds of other information from Elasticsearch. fmt.Printf(&#34;Query took %d milliseconds\n&#34;, searchResult.TookInMillis) // Each is a convenience function that iterates over hits in a search result. // It makes sure you don&#39;t need to check for nil values in the response. // However, it ignores errors in serialization. If you want full control // over iterating the hits, see below. var ttyp Tweet for _, item := range searchResult.Each(reflect.TypeOf(ttyp)) {     if t, ok := item.(Tweet); ok {         fmt.Printf(&#34;Tweet by %s: %s\n&#34;, t.User, t.Message)     } } // TotalHits is another convenience function that works even when something goes wrong. fmt.Printf(&#34;Found a total of %d tweets\n&#34;, searchResult.TotalHits()) // Here&#39;s how you iterate through results with full control over each step. if searchResult.Hits.TotalHits &gt; 0 {     fmt.Printf(&#34;Found a total of %d tweets\n&#34;, searchResult.Hits.TotalHits)     // Iterate through results     for _, hit := range searchResult.Hits.Hits {         // hit.Index contains the name of the index         // Deserialize hit.Source into a Tweet (could also be just a map[string]interface{}).         var t Tweet         err := json.Unmarshal(*hit.Source, &amp;t)         if err != nil {             // Deserialization failed         }         // Work with tweet         fmt.Printf(&#34;Tweet by %s: %s\n&#34;, t.User, t.Message)     } } else {     // No hits     fmt.Print(&#34;Found no tweets\n&#34;) } // Delete the index again _, err = client.DeleteIndex(&#34;twitter&#34;).Do() if err != nil {     // Handle error     panic(err) }</pre>
授权协议:
MIT
开发语言:
Google Go 查看源码»
操作系统:
跨平台
9739 次点击  
加入收藏 微博
暂无回复
添加一条新回复 (您需要 登录 后才能回复 没有账号 ?)
  • 请尽量让自己的回复能够对别人有帮助
  • 支持 Markdown 格式, **粗体**、~~删除线~~、`单行代码`
  • 支持 @ 本站用户;支持表情(输入 : 提示),见 Emoji cheat sheet
  • 图片支持拖拽、截图粘贴等方式上传