1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169
| package main
import ( "context" "fmt" "github.com/elastic/go-elasticsearch/v8/typedapi/core/search" "github.com/elastic/go-elasticsearch/v8/typedapi/some" "github.com/elastic/go-elasticsearch/v8/typedapi/types" "time"
"github.com/elastic/go-elasticsearch/v8" "github.com/spf13/cast" )
type Review struct { ID int64 `json:"id"` UserID int64 `json:"userID"` Score uint8 `json:"score"` Content string `json:"content"` Tags []Tag `json:"tags"` Status int `json:"status"` PublishTime time.Time `json:"publishTime"` }
type Tag struct { Code int `json:"code"` Title string `json:"title"` }
var cfg = elasticsearch.Config{ Addresses: []string{ "http://localhost:9200", }, }
func createClient() *elasticsearch.TypedClient { client, err := elasticsearch.NewTypedClient(cfg) if err != nil { fmt.Printf("elasticsearch.NewTypedClient failed,err:", err) return nil } return client }
func createIndex(client *elasticsearch.TypedClient) { resp, err := client.Indices.Create("my-review-1").Do(context.Background()) if err != nil { fmt.Printf("create index failed, err:%v\n", err) return } fmt.Printf("index:%#v\n", resp.Index) }
func indexDocument(client *elasticsearch.TypedClient) { d1 := Review{ ID: 1, UserID: 78123, Score: 4, Content: "这是一个好评", Tags: []Tag{ { Code: 100, Title: "好评", }, { Code: 1100, Title: "物超所值", }, { Code: 9000, Title: "有图", }, }, Status: 2, PublishTime: time.Now(), }
resp, err := client.Index("my-review-1"). Id(cast.ToString(d1.ID)).Document(d1). Do(context.Background()) if err != nil { fmt.Printf("client.Index failed ,err:%v", err) return } fmt.Printf("result:%#v\n", resp.Result) }
func getDocument(client *elasticsearch.TypedClient, id string) { resp, err := client.Get("my-review-id", id).Do(context.Background()) if err != nil { fmt.Printf("get document by id failed,err:%v\n", err) } fmt.Printf("fileds:%s\n", resp.Fields) }
func searchDocument(client *elasticsearch.TypedClient) { client.Search(). Index("my-review-1"). Query(&types.Query{ MatchPhrase: map[string]types.MatchPhraseQuery{ "content": {Query: "好评"}, }, }). Do(context.Background()) }
func aggregationDemo(client *elasticsearch.TypedClient) { avgScoreAgg, err := client.Search(). Index("my-review-1"). Request( &search.Request{ Size: some.Int(0), Aggregations: map[string]types.Aggregations{ "avg_score": { Avg: &types.AverageAggregation{ Field: some.String("score"), }, }, }, }, ).Do(context.Background()) if err != nil { fmt.Printf("aggregation failed, err:%v\n", err) return } fmt.Printf("avgScore:%#v\n", avgScoreAgg.Aggregations["avg_score"]) }
func updateDocument(client *elasticsearch.TypedClient) { d1 := Review{ ID: 1, UserID: 147982601, Score: 5, Content: "这是一个修改后的好评!", Tags: []Tag{ {1000, "好评"}, {9000, "有图"}, }, Status: 2, PublishTime: time.Now(), } resp, err := client.Update("my-review-1", "1").Doc(d1).Do(context.Background()) if err != nil { fmt.Printf("update document failed, err:%v\n", err) return } fmt.Printf("result:%v\n", resp.Result) }
func deleteDocument(client *elasticsearch.TypedClient) { resp, err := client.Delete("my-review-1", "1").Do(context.Background()) if err != nil { fmt.Printf("delete document failed, err:%v\n", err) return } fmt.Printf("result:%v\n", resp.Result) }
func deleteIndex(client *elasticsearch.TypedClient) { resp, err := client.Indices. Delete("my-review-1"). Do(context.Background()) if err != nil { fmt.Printf("delete document failed, err:%v\n", err) return } fmt.Printf("Acknowledged:%v\n", resp.Acknowledged) }
|