| package main |
| |
| import ( |
| "context" |
| "encoding/json" |
| "errors" |
| "fmt" |
| "github.com/olivere/elastic/v7" |
| "reflect" |
| |
| |
| "time" |
| ) |
| |
| type User struct { |
| Name string `json:"name"` |
| Age int `json:"age"` |
| Married bool `json:"married"` |
| Sex string `json:"sex"` |
| 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 = `{ |
| "mappings":{ |
| "properties":{ |
| "name":{ |
| "type":"text", |
| "index": true |
| }, |
| "age":{ |
| "type":"long", |
| "index": true |
| }, |
| "married":{ |
| "type":"boolean", |
| "index": true |
| }, |
| "created":{ |
| "type":"date", |
| "index": true |
| }, |
| "tags":{ |
| "type":"keyword", |
| "index": true |
| }, |
| "location":{ |
| "type":"geo_point", |
| "index": true |
| }, |
| "suggest_field":{ |
| "type":"completion" |
| } |
| } |
| } |
| }` |
| |
| |
| |
| const mapping1 = ` |
| { |
| "properties": { |
| "sex": { |
| "type": "text" |
| } |
| } |
| } |
| }` |
| var ctx = context.Background() |
| var esUrl string = "http://127.0.0.1:9200" |
| func main() { |
| |
| client, err := elastic.NewClient(elastic.SetURL(esUrl), elastic.SetSniff(false)) |
| if err != nil { |
| |
| panic(err) |
| } |
| |
| |
| info, code, err := client.Ping(esUrl).Do(ctx) |
| if err != nil { |
| |
| panic(err) |
| } |
| fmt.Printf("Elasticsearch returned with code>: %d and version %s\n", code, info.Version.Number) |
| |
| esVersion, err := client.ElasticsearchVersion(esUrl) |
| if err != nil { |
| panic(err) |
| } |
| fmt.Printf("es的版本为%s\n", esVersion) |
| |
| exists, err := client.IndexExists("user").Do(ctx) |
| if exists { |
| do, err := client.DeleteIndex("user").Do(ctx) |
| if nil != err { |
| panic(err) |
| } |
| fmt.Println(do) |
| exists, err = client.IndexExists("user").Do(ctx) |
| } |
| |
| if err != nil { |
| panic(err) |
| } |
| if !exists { |
| |
| createIndex, err := client.CreateIndex("user").BodyString(mapping).Do(ctx) |
| if err != nil { |
| |
| panic(err) |
| } |
| if !createIndex.Acknowledged { |
| |
| panic(errors.New("创建失败")) |
| } |
| |
| } |
| |
| _, err = client.PutMapping().Index("user").BodyString(mapping1).Do(ctx) |
| if err != nil { |
| fmt.Println(err) |
| panic(err) |
| } |
| |
| |
| user1 := User{Name:"bob",Sex:"male",Married:false,Age:23} |
| put1,err :=client.Index().Index("user").BodyJson(user1).Id("1").Do(ctx) |
| if err != nil{ |
| panic(err) |
| } |
| fmt.Printf("user3 Indexed user %s to index %s, type %s\n", put1.Id, put1.Index, put1.Type) |
| |
| user2 := `{"name":"mike","sex":"male","married":true,"age":22}` |
| put2, err := client.Index().Index("user").BodyString(user2).Do(ctx) |
| if err != nil{ |
| panic(err) |
| } |
| fmt.Printf("user3 Indexed user %s to index %s, type %s\n", put2.Id, put2.Index, put2.Type) |
| |
| user3 := User{Name:"mike",Sex:"male",Married:false,Age:35} |
| put3,err :=client.Index().Index("user").BodyJson(user3).Do(ctx) |
| if err != nil{ |
| panic(err) |
| } |
| fmt.Printf("user3 Indexed user %s to index %s, type %s\n", put3.Id, put3.Index, put3.Type) |
| |
| get1, err := client.Get().Index("user").Id("1").Do(ctx) |
| if err != nil{ |
| 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) |
| |
| } |
| |
| _, err = client.Flush().Index("user").Do(ctx) |
| if err != nil { |
| panic(err) |
| } |
| |
| |
| var querys []elastic.Query |
| termQuery := elastic.NewTermQuery("name", "mike") |
| querys = append(querys, termQuery) |
| boolQuery := elastic.NewBoolQuery().Must(querys...) |
| |
| searchResult, err := client.Search(). |
| Index("user"). |
| |
| Query(boolQuery). |
| Sort("age", true). |
| From(0).Size(10). |
| Pretty(true). |
| Do(ctx) |
| if err != nil { |
| panic(err) |
| } |
| fmt.Printf("Query took %d milliseconds\n", searchResult.TookInMillis) |
| var user User |
| |
| for _, item1 := range searchResult.Each(reflect.TypeOf(user)) { |
| if u, ok := item1.(User); ok { |
| fmt.Printf("Person by %s,age:%d,married:%t,Sex:%s\n", u.Name, u.Age, u.Married,u.Sex) |
| } |
| } |
| |
| |
| if searchResult.Hits.TotalHits.Value >0{ |
| fmt.Printf("找到的数据总数是 %d \n", searchResult.Hits.TotalHits.Value) |
| for _,hits := range searchResult.Hits.Hits{ |
| u :=User{} |
| err := json.Unmarshal([]byte(hits.Source), &u) |
| if err != nil{ |
| fmt.Println("反序列化失败",err) |
| } |
| fmt.Printf("User by %s,age:%d,married:%t,Sex:%s\n", u.Name, u.Age, u.Married,u.Sex) |
| } |
| }else { |
| fmt.Println("没有搜到用户") |
| } |
| fmt.Println(222) |
| |
| var data []*User |
| for _, h := range searchResult.Hits.Hits { |
| b, err := h.Source.MarshalJSON() |
| if err != nil { |
| panic("操作错误") |
| } |
| var d User |
| if err := json.Unmarshal(b, &d); err != nil { |
| panic("操作错误") |
| } |
| |
| data = append(data, &d) |
| } |
| fmt.Println(data) |
| |
| update, err := client.Update().Index("user").Id("1"). |
| Script(elastic.NewScriptInline("ctx._source.age += params.num").Lang("painless").Param("num", 1)). |
| |
| Do(ctx) |
| if err != nil { |
| |
| panic(err) |
| } |
| fmt.Printf("New version of user %q is now %d\n", update.Id, update.Version) |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| fmt.Printf("New version of user %q is now %d\n", update.Id, update.Version) |
| fmt.Println(update) |
| |
| termQuery = elastic.NewTermQuery("name", "mike") |
| _, err = client.DeleteByQuery().Index("user"). |
| Query(termQuery). |
| Do(ctx) |
| if err != nil { |
| |
| panic(err) |
| } |
| } |
| |