【GO】Elasticsearch的簡單寫入和讀取示例
下載pkg
go get gopkg.in/olivere/elastic.v5
修改import項
import "gopkg.in/olivere/elastic.v5"
原始碼
package main
import (
"context"
"encoding/json"
"fmt"
"reflect"
"time"
"gopkg.in/olivere/elastic.v5" // "github.com/olivere/elastic"
)
var (
index_log_order string = "logisitc_order"
type_log_order string = "order"
)
// LogOrder is a structure used for serializing/deserializing data in Elasticsearch.
type LogOrder struct {
User string `json:"user"`
Message string `json:"message"`
Reorders int `json:"reorders"`
Image string `json:"image,omitempty"`
Created time.Time `json:"created,omitempty"`
Tags []string `json:"tags,omitempty"`
Location string `json:"location,omitempty"`
Suggest *elastic.SuggestField `json:"suggest_field,omitempty"`
}
const mapping = `
{
"settings":{
"number_of_shards": 1,
"number_of_replicas": 0
},
"mappings":{
"order":{
"properties":{
"user":{
"type":"keyword"
},
"message":{
"type":"text",
"store": true,
"fielddata": true
},
"image":{
"type":"keyword"
},
"created":{
"type":"date"
},
"tags":{
"type":"keyword"
},
"location":{
"type":"geo_point"
},
"suggest_field":{
"type":"completion"
}
}
}
}
}`
func main() {
// Starting with elastic.v5, you must pass a context to execute each service
ctx := context.Background()
// Obtain a client and connect to the default Elasticsearch installation
// on 127.0.0.1:9200. Of course you can configure your client to connect
// to other hosts and configure it in various other ways.
client, err := elastic.NewClient()
if err != nil {
// Handle error
panic(err)
}
// Ping the Elasticsearch server to get e.g. the version number
info, code, err := client.Ping("http://127.0.0.1:9200").Do(ctx)
if err != nil {
// Handle error
panic(err)
}
fmt.Printf("Elasticsearch returned with code %d and version %s\n", code, info.Version.Number)
// Getting the ES version number is quite common, so there's a shortcut
esversion, err := client.ElasticsearchVersion("http://127.0.0.1:9200")
if err != nil {
// Handle error
panic(err)
}
fmt.Printf("Elasticsearch version %s\n", esversion)
// Use the IndexExists service to check if a specified index exists.
exists, err := client.IndexExists(index_log_order).Do(ctx)
if err != nil {
// Handle error
panic(err)
}
if !exists {
// Create a new index.
createIndex, err := client.CreateIndex(index_log_order).BodyString(mapping).Do(ctx)
if err != nil {
// Handle error
panic(err)
}
if !createIndex.Acknowledged {
// Not acknowledged
}
}
// Index a order (using JSON serialization)
order1 := LogOrder{User: "olivere", Message: "Take Five", Reorders: 0}
put1, err := client.Index().
Index(index_log_order).
Type(type_log_order).
Id("1").
BodyJson(order1).
Do(ctx)
if err != nil {
// Handle error
panic(err)
}
fmt.Printf("Indexed order %s to index %s, type %s\n", put1.Id, put1.Index, put1.Type)
// Index a second order (by string)
order2 := `{"user" : "olivere", "message" : "It's a Raggy Waltz"}`
put2, err := client.Index().
Index(index_log_order).
Type(type_log_order).
Id("2").
BodyString(order2).
Do(ctx)
if err != nil {
// Handle error
panic(err)
}
fmt.Printf("Indexed order %s to index %s, type %s\n", put2.Id, put2.Index, put2.Type)
// Get order with specified ID
get1, err := client.Get().
Index(index_log_order).
Type(type_log_order).
Id("1").
Do(ctx)
if err != nil {
// Handle error
panic(err)
}
if get1.Found {
fmt.Printf("Got document %s in version %d from index %s, type %s\n", get1.Id, get1.Version, get1.Index, get1.Type)
}
// Flush to make sure the documents got written.
_, err = client.Flush().Index(index_log_order).Do(ctx)
if err != nil {
panic(err)
}
// Search with a term query
termQuery := elastic.NewTermQuery("user", "olivere")
searchResult, err := client.Search().
Index(index_log_order). // search in index index_log_order
Query(termQuery). // specify the query
Sort("user", true). // sort by "user" field, ascending
From(0).Size(10). // take documents 0-9
Pretty(true). // pretty print request and response JSON
Do(ctx) // 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("Query took %d milliseconds\n", searchResult.TookInMillis)
// Each is a convenience function that iterates over hits in a search result.
// It makes sure you don'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 LogOrder
for _, item := range searchResult.Each(reflect.TypeOf(ttyp)) {
if t, ok := item.(LogOrder); ok {
fmt.Printf("LogOrder by %s: %s\n", t.User, t.Message)
}
}
// TotalHits is another convenience function that works even when something goes wrong.
fmt.Printf("Found a total of %d orders\n", searchResult.TotalHits())
// Here's how you iterate through results with full control over each step.
if searchResult.Hits.TotalHits > 0 {
fmt.Printf("Found a total of %d orders\n", 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 LogOrder (could also be just a map[string]interface{}).
var t LogOrder
err := json.Unmarshal(*hit.Source, &t)
if err != nil {
// Deserialization failed
}
// Work with order
fmt.Printf("LogOrder by %s: %s\n", t.User, t.Message)
}
} else {
// No hits
fmt.Print("Found no orders\n")
}
// Update a order by the update API of Elasticsearch.
// We just increment the number of reorders.
update, err := client.Update().Index(index_log_order).Type(type_log_order).Id("1").
Script(elastic.NewScriptInline("ctx._source.reorders += params.num").Lang("painless").Param("num", 1)).
Upsert(map[string]interface{}{"reorders": 0}).
Do(ctx)
if err != nil {
// Handle error
panic(err)
}
fmt.Printf("New version of order %q is now %d\n", update.Id, update.Version)
// ...
// Delete an index.
deleteIndex, err := client.DeleteIndex(index_log_order).Do(ctx)
if err != nil {
// Handle error
panic(err)
}
if !deleteIndex.Acknowledged {
// Not acknowledged
}
}
執行結果
Elasticsearch returned with code 200 and version 6.3.1
Elasticsearch version 6.3.1
Indexed order 1 to index logisitc_order, type order
Indexed order 2 to index logisitc_order, type order
Got document 1 in version 842351749456 from index logisitc_order, type order
Query took 3 milliseconds
LogOrder by olivere: Take Five
LogOrder by olivere: It's a Raggy Waltz
Found a total of 2 orders
Found a total of 2 orders
LogOrder by olivere: Take Five
LogOrder by olivere: It's a Raggy Waltz
New version of order "1" is now 2
相關文章
- go操作elasticsearch示例GoElasticsearch
- csv檔案的寫入和讀取
- 用 logstash 從 kafka 讀取資料寫入 Elasticsearch(qbit)KafkaElasticsearch
- 寫一個簡單的 Facade 示例
- python檔案建立、讀取和寫入Python
- 如何讀取和寫入JSON檔案JSON
- Elasticsearch 的安裝和簡單配置Elasticsearch
- 簡單的 Go 入門教程Go
- 簡單介紹Go 字串比較的實現示例Go字串
- Spark Streaming簡單入門(示例+原理)Spark
- NPOI讀取示例
- JavaScript 寫入與讀取cookieJavaScriptCookie
- Androidxml資料的讀取和寫入(sax,pull,dom,xstream,jsoup)AndroidXMLJS
- 圖片二次取樣簡單示例
- startViewTransition的簡單示例View
- Go 實現 nginx log 讀取 分析 寫入InfluxDB 並用Grafana 顯示GoNginxUXGrafana
- 資料的讀取和寫入,其中Reader便是其中之一
- jsoncpp按寫入順序讀取JSON
- 2 個簡單的 python 指令碼,連線 MySQL 和讀取 ExcelPython指令碼MySqlExcel
- easypoi 讀取 Excel 簡單應用Excel
- 社群內 Go 入門指南的學習,白話精簡示例。Go
- linux讀寫檔案 簡單版Linux
- Python3 進行讀取、修改和寫Excel表格(.xlsx檔案)的常用功能示例PythonExcel
- 從HDFS的寫入和讀取中,我發現了點東西
- HDFS 03 - 你能說說 HDFS 的寫入和讀取過程嗎?
- 淘寶詳情api介面獲取的方式及簡單示例API
- 怎麼提高go讀取標準輸入的速度Go
- Go基礎系列:讀取標準輸入Go
- 一個讀取Gmail郵件的簡單程式AI
- SQL WHERE IN引數化編譯寫法簡單示例SQL編譯
- go kafka 單機寫入 50wGoKafka
- mORMot 1.18 第07章 簡單的讀寫操作ORM
- Go語言中的互斥鎖和讀寫鎖(Mutex和RWMutex)GoMutex
- Java使用ObjectMapper的簡單示例JavaObjectAPP
- Dave Cheney:編寫簡單,可讀,可維護的Go程式碼的十個工程建議Go
- State Processor API:如何讀取,寫入和修改 Flink 應用程式的狀態API
- dotnet學習筆記-專題04-配置的讀取和寫入-01筆記
- go語言簡單入門--常識和資料型別Go資料型別