diff --git a/go.mod b/go.mod index adcb46a..3feac1f 100644 --- a/go.mod +++ b/go.mod @@ -9,8 +9,12 @@ require ( github.com/BurntSushi/toml v1.4.0 // indirect github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc // indirect github.com/dustin/go-humanize v1.0.1 // indirect + github.com/elastic/elastic-transport-go/v8 v8.6.0 // indirect + github.com/elastic/go-elasticsearch/v8 v8.15.0 // indirect github.com/glebarez/go-sqlite v1.20.3 // indirect github.com/glebarez/sqlite v1.7.0 // indirect + github.com/go-logr/logr v1.4.1 // indirect + github.com/go-logr/stdr v1.2.2 // indirect github.com/golang-sql/civil v0.0.0-20220223132316-b832511892a9 // indirect github.com/golang-sql/sqlexp v0.1.0 // indirect github.com/google/uuid v1.4.0 // indirect @@ -21,6 +25,9 @@ require ( github.com/microsoft/go-mssqldb v1.6.0 // indirect github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2 // indirect github.com/remyoudompheng/bigfft v0.0.0-20230126093431-47fa9a501578 // indirect + go.opentelemetry.io/otel v1.24.0 // indirect + go.opentelemetry.io/otel/metric v1.24.0 // indirect + go.opentelemetry.io/otel/trace v1.24.0 // indirect golang.org/x/image v0.0.0-20191009234506-e7c1f5e7dbb8 // indirect golang.org/x/sync v0.8.0 // indirect gopkg.in/natefinch/lumberjack.v2 v2.2.1 // indirect diff --git a/go.sum b/go.sum index 3aeb155..49fcaf9 100644 --- a/go.sum +++ b/go.sum @@ -51,6 +51,10 @@ github.com/dnaeon/go-vcr v1.1.0/go.mod h1:M7tiix8f0r6mKKJ3Yq/kqU1OYf3MnfmBWVbPx/ github.com/dnaeon/go-vcr v1.2.0/go.mod h1:R4UdLID7HZT3taECzJs4YgbbH6PIGXB6W/sc5OLb6RQ= github.com/dustin/go-humanize v1.0.1 h1:GzkhY7T5VNhEkwH0PVJgjz+fX1rhBrR7pRT3mDkpeCY= github.com/dustin/go-humanize v1.0.1/go.mod h1:Mu1zIs6XwVuF/gI1OepvI0qD18qycQx+mFykh5fBlto= +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/frankban/quicktest v1.14.6 h1:7Xjx+VpznH+oBnejlPUj8oUpdxnVs4f8XU8WnHkI4W8= github.com/frankban/quicktest v1.14.6/go.mod h1:4ptaffx2x8+WTWXmUCuVU6aPUX1/Mz7zb5vbUoiM6w0= github.com/fsnotify/fsnotify v1.7.0 h1:8JEhPFa5W2WU7YfeZzPNqzMP6Lwt7L2715Ggo0nosvA= @@ -67,6 +71,11 @@ github.com/glebarez/go-sqlite v1.20.3 h1:89BkqGOXR9oRmG58ZrzgoY/Fhy5x0M+/WV48U5z github.com/glebarez/go-sqlite v1.20.3/go.mod h1:u3N6D/wftiAzIOJtZl6BmedqxmmkDfH3q+ihjqxC9u0= github.com/glebarez/sqlite v1.7.0 h1:A7Xj/KN2Lvie4Z4rrgQHY8MsbebX3NyWsL3n2i82MVI= github.com/glebarez/sqlite v1.7.0/go.mod h1:PkeevrRlF/1BhQBCnzcMWzgrIk7IOop+qS2jUYLfHhk= +github.com/go-logr/logr v1.2.2/go.mod h1:jdQByPbusPIv2/zmleS9BjJVeZ6kBagPoEUsqbVz/1A= +github.com/go-logr/logr v1.4.1 h1:pKouT5E8xu9zeFC39JXRDukb6JFQPXM5p5I91188VAQ= +github.com/go-logr/logr v1.4.1/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= github.com/go-playground/assert/v2 v2.2.0 h1:JvknZsQTYeFEAhQwI4qEt9cyV5ONwRHC+lYKSsYSR8s= github.com/go-playground/assert/v2 v2.2.0/go.mod h1:VDjEfimB/XKnb+ZQfWdccd7VUvScMdVu0Titje2rxJ4= github.com/go-playground/locales v0.14.1 h1:EWaQ/wswjilfKLTECiXz7Rh+3BjFhfDFKv/oXslEjJA= @@ -210,6 +219,12 @@ github.com/twitchyliquid64/golang-asm v0.15.1/go.mod h1:a1lVb/DtPvCB8fslRZhAngC2 github.com/ugorji/go/codec v1.2.12 h1:9LC83zGrHhuUA9l16C9AHXAqEV/2wBQ4nkvumAE65EE= github.com/ugorji/go/codec v1.2.12/go.mod h1:UNopzCgEMSXjBc6AOMqYvWC1ktqTAfzJZUZgYf6w6lg= github.com/yuin/goldmark v1.4.13/go.mod h1:6yULJ656Px+3vBD8DxQVa3kxgyrAnzto9xy5taEt/CY= +go.opentelemetry.io/otel v1.24.0 h1:0LAOdjNmQeSTzGBzduGe/rU4tZhMwL5rWgtp9Ku5Jfo= +go.opentelemetry.io/otel v1.24.0/go.mod h1:W7b9Ozg4nkF5tWI5zsXkaKKDjdVjpD4oAt9Qi/MArHo= +go.opentelemetry.io/otel/metric v1.24.0 h1:6EhoGWWK28x1fbpA4tYTOWBkPefTDQnb8WSGXlc88kI= +go.opentelemetry.io/otel/metric v1.24.0/go.mod h1:VYhLe1rFfxuTXLgj4CBiyz+9WYBA8pNGJgDcSFRKBco= +go.opentelemetry.io/otel/trace v1.24.0 h1:CsKnnL4dUAr/0llH9FKuc698G04IrpWV0MQA/Y1YELI= +go.opentelemetry.io/otel/trace v1.24.0/go.mod h1:HPc3Xr/cOApsBI154IU0OI0HJexz+aw5uPdbs3UCjNU= go.uber.org/goleak v1.3.0 h1:2K3zAYmnTNqV73imy9J1T3WC+gmCePx2hEGkimedGto= go.uber.org/goleak v1.3.0/go.mod h1:CoHD4mav9JJNrW/WLlf7HGZPjdw8EucARQHekz1X6bE= go.uber.org/multierr v1.11.0 h1:blXXJkSxSSfBVBlC76pxqeO+LN3aDfLQo+309xJstO0= diff --git a/test/es/TODO.md b/test/es/TODO.md new file mode 100644 index 0000000..63c8fc1 --- /dev/null +++ b/test/es/TODO.md @@ -0,0 +1 @@ +[ ] 之后是可以用 go 做 ES 的文档检索;不需要借助 python。(不过这个功能不是很着急) \ No newline at end of file diff --git a/test/es/es_test.go b/test/es/es_test.go new file mode 100644 index 0000000..8dc160e --- /dev/null +++ b/test/es/es_test.go @@ -0,0 +1,108 @@ +package test + +import ( + "bytes" + "context" + "encoding/json" + "fmt" + "testing" + + "github.com/elastic/go-elasticsearch/v8/esapi" +) + +func TestAddIndex(t *testing.T) { + InitElastic() + + stu := Student{ + Id: "student_1", + Name: "Geek 1", + Age: 22, + Address: "Wuhan", + School: "Hebei School", + } + + marshal, err := json.Marshal(stu) + if err != nil { + fmt.Println(err) + return + } + + indexReq := esapi.IndexRequest{ + Index: StudentIndex, + DocumentID: fmt.Sprintf("%s_%s", StudentIndex, stu.Id), + Body: bytes.NewReader(marshal), + Refresh: "true", + } + if _, err = indexReq.Do(context.Background(), ElasticClient); err != nil { + fmt.Println(err) + return + } +} + +func TestSearch(t *testing.T) { + InitElastic() + + students := make([]*Student, 0) + + keyWord := "Yan" + + var buf bytes.Buffer + query := map[string]interface{}{ + "query": map[string]interface{}{ + "bool": map[string]interface{}{ + "should": []map[string]interface{}{ + {"match": map[string]interface{}{"name": keyWord}}, + {"match": map[string]interface{}{"address": keyWord}}, + {"match": map[string]interface{}{"school": keyWord}}, + }, + }, + }, + } + + if err := json.NewEncoder(&buf).Encode(query); err != nil { + fmt.Println(err) + return + } + + res, err := ElasticClient.Search( + ElasticClient.Search.WithContext(context.Background()), + ElasticClient.Search.WithIndex(StudentIndex), + ElasticClient.Search.WithBody(&buf), + ElasticClient.Search.WithTrackTotalHits(true), + ElasticClient.Search.WithPretty(), + ) + if err != nil || res.IsError() { + fmt.Println(err) + return + } + + defer func() { + _ = res.Body.Close() + }() + + var r map[string]interface{} + if err := json.NewDecoder(res.Body).Decode(&r); err != nil { + fmt.Println(err) + return + } + + for _, hit := range r["hits"].(map[string]interface{})["hits"].([]interface{}) { + if _, v := hit.(map[string]interface{})["_source"]; v { + var model Student + body, err := json.Marshal(hit.(map[string]interface{})["_source"]) + if err != nil { + fmt.Println(err) + continue + } + if err := json.Unmarshal(body, &model); err != nil { + fmt.Println(err) + continue + } + students = append(students, &model) + } + } + + for _, s := range students { + fmt.Printf("%#v \n", s) + } +} diff --git a/test/es/init.go b/test/es/init.go new file mode 100644 index 0000000..00560d4 --- /dev/null +++ b/test/es/init.go @@ -0,0 +1,31 @@ +package test + +import ( + "github.com/elastic/go-elasticsearch/v8" +) + +var ( + ElasticClient *elasticsearch.Client + StudentIndex string = "student_index" +) + +func InitElastic() { + var err error + ElasticClient, err = elasticsearch.NewClient(elasticsearch.Config{ + // Addresses: []string{"http://113.44.68.213:9200"}, + Addresses: []string{"http://127.0.0.1:9200"}, + // Username: "elastic", + // Password: "U8n61yn*Sp4Kvbuqo_K8", + }) + if err != nil { + panic(err) + } +} + +type Student struct { + Id string `json:"id,omitempty"` + Name string `json:"name,omitempty"` + Age int `json:"age,omitempty"` + Address string `json:"address,omitempty"` + School string `json:"school,omitempty"` +}