Last active
October 18, 2021 01:49
-
-
Save un1t/ac188472f5dfe759d14f to your computer and use it in GitHub Desktop.
golang elasticsearch bulk insert
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"fmt" | |
"gopkg.in/olivere/elastic.v2" | |
"strconv" | |
) | |
type Tweet struct { | |
User string `json:"user"` | |
Message string `json:"message"` | |
} | |
func main() { | |
client, err := elastic.NewClient() | |
if err != nil { | |
fmt.Println("%v", err) | |
} | |
n := 0 | |
for i := 0; i < 1000; i++ { | |
bulkRequest := client.Bulk() | |
for j := 0; j < 10000; j++ { | |
n++ | |
tweet := Tweet{User: "olivere", Message: "Package strconv implements conversions to and from string representations of basic data types. " + strconv.Itoa(n)} | |
req := elastic.NewBulkIndexRequest().Index("twitter").Type("tweet").Id(strconv.Itoa(n)).Doc(tweet) | |
bulkRequest = bulkRequest.Add(req) | |
} | |
bulkResponse, err := bulkRequest.Do() | |
if err != nil { | |
fmt.Println(err) | |
} | |
if bulkResponse != nil { | |
} | |
fmt.Println(i) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
self generate doc
_id
?