zenbook migration
This commit is contained in:
@@ -0,0 +1,13 @@
|
||||
module elasticsearch
|
||||
|
||||
go 1.23.1
|
||||
|
||||
require (
|
||||
github.com/elastic/elastic-transport-go/v8 v8.6.0 // indirect
|
||||
github.com/elastic/go-elasticsearch/v8 v8.15.0 // indirect
|
||||
github.com/go-logr/logr v1.4.2 // indirect
|
||||
github.com/go-logr/stdr v1.2.2 // indirect
|
||||
go.opentelemetry.io/otel v1.31.0 // indirect
|
||||
go.opentelemetry.io/otel/metric v1.31.0 // indirect
|
||||
go.opentelemetry.io/otel/trace v1.31.0 // indirect
|
||||
)
|
||||
@@ -0,0 +1,15 @@
|
||||
github.com/elastic/elastic-transport-go/v8 v8.6.0 h1:Y2S/FBjx1LlCv5m6pWAF2kDJAHoSjSRSJCApolgfthA=
|
||||
github.com/elastic/elastic-transport-go/v8 v8.6.0/go.mod h1:YLHer5cj0csTzNFXoNQ8qhtGY1GTvSqPnKWKaqQE3Hk=
|
||||
github.com/elastic/go-elasticsearch/v8 v8.15.0 h1:IZyJhe7t7WI3NEFdcHnf6IJXqpRf+8S8QWLtZYYyBYk=
|
||||
github.com/elastic/go-elasticsearch/v8 v8.15.0/go.mod h1:HCON3zj4btpqs2N1jjsAy4a/fiAul+YBP00mBH4xik8=
|
||||
github.com/go-logr/logr v1.2.2/go.mod h1:jdQByPbusPIv2/zmleS9BjJVeZ6kBagPoEUsqbVz/1A=
|
||||
github.com/go-logr/logr v1.4.2 h1:6pFjapn8bFcIbiKo3XT4j/BhANplGihG6tvd+8rYgrY=
|
||||
github.com/go-logr/logr v1.4.2/go.mod h1:9T104GzyrTigFIr8wt5mBrctHMim0Nb2HLGrmQ40KvY=
|
||||
github.com/go-logr/stdr v1.2.2 h1:hSWxHoqTgW2S2qGc0LTAI563KZ5YKYRhT3MFKZMbjag=
|
||||
github.com/go-logr/stdr v1.2.2/go.mod h1:mMo/vtBO5dYbehREoey6XUKy/eSumjCCveDpRre4VKE=
|
||||
go.opentelemetry.io/otel v1.31.0 h1:NsJcKPIW0D0H3NgzPDHmo0WW6SptzPdqg/L1zsIm2hY=
|
||||
go.opentelemetry.io/otel v1.31.0/go.mod h1:O0C14Yl9FgkjqcCZAsE053C13OaddMYr/hz6clDkEJE=
|
||||
go.opentelemetry.io/otel/metric v1.31.0 h1:FSErL0ATQAmYHUIzSezZibnyVlft1ybhy4ozRPcF2fE=
|
||||
go.opentelemetry.io/otel/metric v1.31.0/go.mod h1:C3dEloVbLuYoX41KpmAhOqNriGbA+qqH6PQ5E5mUfnY=
|
||||
go.opentelemetry.io/otel/trace v1.31.0 h1:ffjsj1aRouKewfr85U2aGagJ46+MvodynlQ1HYdmJys=
|
||||
go.opentelemetry.io/otel/trace v1.31.0/go.mod h1:TXZkRk7SM2ZQLtR6eoAWQFIHPvzQ06FJAsO1tJg480A=
|
||||
@@ -0,0 +1,116 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"flag"
|
||||
"fmt"
|
||||
"os/signal"
|
||||
"syscall"
|
||||
"time"
|
||||
|
||||
elasticsearch8 "github.com/elastic/go-elasticsearch/v8"
|
||||
"github.com/elastic/go-elasticsearch/v8/typedapi/core/search"
|
||||
"github.com/elastic/go-elasticsearch/v8/typedapi/types"
|
||||
)
|
||||
|
||||
func main() {
|
||||
var hostname string
|
||||
var list, ha bool
|
||||
flag.StringVar(&hostname, "host", "elastic.zerops", "hostname to connect to elastic")
|
||||
flag.BoolVar(&list, "list", false, "whether to list the whole index")
|
||||
flag.BoolVar(&ha, "ha", false, "is the cluster ha?")
|
||||
flag.Parse()
|
||||
|
||||
ctx, stop := signal.NotifyContext(context.Background(), syscall.SIGINT, syscall.SIGTERM)
|
||||
defer stop()
|
||||
|
||||
client, err := elasticsearch8.NewTypedClient(elasticsearch8.Config{
|
||||
Addresses: []string{fmt.Sprintf("http://%s:9200", hostname)},
|
||||
// Logger: &elastictransport.TextLogger{Output: os.Stdout, EnableRequestBody: true},
|
||||
})
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
const indexName = "my_index"
|
||||
indicesExistsResponse, err := client.Indices.Exists(indexName).Do(ctx)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
if !indicesExistsResponse {
|
||||
indicesCreateResponse, err := client.Indices.Create(indexName).Do(ctx)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
fmt.Println("created:", indicesCreateResponse.Index)
|
||||
if !ha {
|
||||
_, err = client.Indices.PutSettings().Indices(indexName).NumberOfReplicas("0").Do(ctx)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
}
|
||||
} else {
|
||||
fmt.Println("already exists:", indexName)
|
||||
}
|
||||
|
||||
if list {
|
||||
size := 10_000
|
||||
searchResponse, err := client.Search().
|
||||
Index(indexName).
|
||||
Request(
|
||||
&search.Request{
|
||||
Query: &types.Query{
|
||||
MatchAll: types.NewMatchAllQuery(),
|
||||
},
|
||||
Size: &size,
|
||||
},
|
||||
).
|
||||
Do(ctx)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
for _, hit := range searchResponse.Hits.Hits {
|
||||
var data map[string]any
|
||||
_ = json.Unmarshal(hit.Source_, &data)
|
||||
fmt.Printf("==> %s\n", data["message"])
|
||||
}
|
||||
}
|
||||
|
||||
for {
|
||||
if ctx.Err() != nil {
|
||||
fmt.Println("context canceled")
|
||||
return
|
||||
}
|
||||
|
||||
message := time.Now().Format(time.TimeOnly)
|
||||
document := map[string]any{
|
||||
"name": "yahoo",
|
||||
"message": message,
|
||||
}
|
||||
indexResponse, err := client.Index(indexName).Request(document).Do(ctx)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
fmt.Printf("%s ==>\n", message)
|
||||
|
||||
getResponse, err := client.Get(indexName, indexResponse.Id_).Do(ctx)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
if !getResponse.Found {
|
||||
fmt.Println("error: document not found")
|
||||
continue
|
||||
}
|
||||
if getResponse.Id_ != indexResponse.Id_ {
|
||||
fmt.Println("error: different ids")
|
||||
continue
|
||||
}
|
||||
|
||||
var data map[string]any
|
||||
_ = json.Unmarshal(getResponse.Source_, &data)
|
||||
fmt.Printf("==> %s\n", data["message"])
|
||||
|
||||
time.Sleep(time.Second)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user