| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136 |
- package elastic
- import (
- "context"
- "encoding/json"
- "fmt"
- "reflect"
- "time"
- "github.com/olivere/elastic"
- )
- const mapping = `
- {
- "settings":{
- "number_of_shards": 1,
- "number_of_replicas": 0
- },
- "mappings":{
- "tweet":{
- "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"
- }
- }
- }
- }
- }`
- type Tweet struct {
- User string `json:"user"`
- Message string `json:"message"`
- Retweets int `json:"retweets"`
- 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"`
- }
- func Demo() {
- ctx := context.Background()
- client := getElasticClient()
- index := "twitter"
- indexType := "tweet"
- DeleteIndex(index)
- CreateIndex(index, mapping)
- // Index a tweet (using JSON serialization)
- tweet1 := Tweet{User: "olivere", Message: "Take Five", Retweets: 0}
- PutBodyJson(index, indexType, "1", tweet1)
- // Index a second tweet (by string)
- tweet2 := `{"user" : "olivere", "message" : "It's a Raggy Waltz"}`
- PutBodyString(index, indexType, "2", tweet2)
- // Get tweet with specified ID
- get1, err := GetResult(index, indexType, "1")
- if err == nil {
- var tweetGet1 Tweet
- tweetGetByte1, _ := get1.Source.MarshalJSON()
- err := json.Unmarshal(tweetGetByte1, &tweetGet1)
- fmt.Printf("Got err %s, tweetGetByte1 %+v \n", err, tweetGet1)
- }
- // Flush to make sure the documents got written.
- //Flush(index)
- // Search with a term query
- termQuery := elastic.NewTermQuery("user", "olivere")
- searchResult, err := client.Search().
- Index(index). // search in index "twitter"
- 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 {
- panic(err)
- }
- fmt.Printf("Query took %d milliseconds\n", searchResult.TookInMillis)
- var ttyp Tweet
- for _, item := range searchResult.Each(reflect.TypeOf(ttyp)) {
- if t, ok := item.(Tweet); ok {
- fmt.Printf("Tweet by %s: %s\n", t.User, t.Message)
- }
- }
- fmt.Printf("Found a total of %d tweets\n", searchResult.TotalHits())
- if searchResult.Hits.TotalHits > 0 {
- fmt.Printf("Found a total of %d tweets\n", searchResult.Hits.TotalHits)
- for _, hit := range searchResult.Hits.Hits {
- var t Tweet
- err := json.Unmarshal(*hit.Source, &t)
- if err != nil {
- }
- fmt.Printf("Tweet by %s: %s\n", t.User, t.Message)
- }
- } else {
- fmt.Print("Found no tweets\n")
- }
- update, err := client.Update().Index("twitter").Type("tweet").Id("1").
- Script(elastic.NewScriptInline("ctx._source.retweets += params.num").Lang("painless").Param("num", 1)).
- Upsert(map[string]interface{}{"retweets": 0}).
- Do(ctx)
- if err != nil {
- panic(err)
- }
- fmt.Printf("New version of tweet %q is now %d\n", update.Id, update.Version)
- // Delete an index.
- //DeleteIndex(index)
- }
|