// The simplest usage of background bulk indexing func ExampleBulkIndexer_simple() { c := elastigo.NewConn() indexer := c.NewBulkIndexerErrors(10, 60) indexer.Start() indexer.Index("twitter", "user", "1", "", "", nil, nil, `{"name":"bob"}`) indexer.Stop() }
// The inspecting the response func ExampleBulkIndexer_responses() { c := elastigo.NewConn() indexer := c.NewBulkIndexer(10) // Create a custom Sender Func, to allow inspection of response/error indexer.Sender = func(buf *bytes.Buffer) error { // @buf is the buffer of docs about to be written respJson, err := c.DoCommand("POST", "/_bulk", nil, buf) if err != nil { // handle it better than this fmt.Println(string(respJson)) } return err } indexer.Start() for i := 0; i < 20; i++ { indexer.Index("twitter", "user", strconv.Itoa(i), "", "", nil, nil, `{"name":"bob"}`) } indexer.Stop() }
// for testing func main() { flag.Parse() log.SetFlags(log.Ltime | log.Lshortfile) c := elastigo.NewConn() c.Domain = *eshost response, _ := c.Index("twitter", "tweet", "1", nil, NewTweet("kimchy", "Search is cool")) c.Flush() log.Printf("Index OK: %v", response.Ok) searchresponse, err := c.Search("twitter", "tweet", nil, "{\"query\" : {\"term\" : { \"user\" : \"kimchy\" }}}") if err != nil { log.Println("error during search:" + err.Error()) log.Fatal(err) } // try marshalling to tweet type var t Tweet bytes, err := searchresponse.Hits.Hits[0].Source.MarshalJSON() if err != nil { log.Fatalf("err calling marshalJson:%v", err) } json.Unmarshal(bytes, t) log.Printf("Search Found: %s", t) response, _ = c.Get("twitter", "tweet", "1", nil) log.Printf("Get: %v", response.Exists) exists, _ := c.Exists("twitter", "tweet", "1", nil) log.Printf("Exists: %v", exists) c.Flush() countResponse, _ := c.Count("twitter", "tweet", nil, nil) log.Printf("Count: %v", countResponse.Count) response, _ = c.Delete("twitter", "tweet", "1", map[string]interface{}{"version": -1, "routing": ""}) log.Printf("Delete OK: %v", response.Ok) response, _ = c.Get("twitter", "tweet", "1", nil) log.Printf("Get: %v", response.Exists) healthResponse, _ := c.Health() log.Printf("Health: %v", healthResponse.Status) c.UpdateSettings("transient", "discovery.zen.minimum_master_nodes", 2) }
func main() { c := elastigo.NewConn() log.SetFlags(log.LstdFlags) flag.Parse() // Trace all requests c.RequestTracer = func(method, url, body string) { log.Printf("Requesting %s %s", method, url) log.Printf("Request body: %s", body) } fmt.Println("host = ", *host) // Set the Elasticsearch Host to Connect to c.Domain = *host // Index a document _, err := c.Index("testindex", "user", "docid_1", nil, `{"name":"bob"}`) exitIfErr(err) // Index a doc using a map of values _, err = c.Index("testindex", "user", "docid_2", nil, map[string]string{"name": "venkatesh"}) exitIfErr(err) // Index a doc using Structs _, err = c.Index("testindex", "user", "docid_3", nil, MyUser{"wanda", 22}) exitIfErr(err) // Search Using Raw json String searchJson := `{ "query" : { "term" : { "Name" : "wanda" } } }` out, err := c.Search("testindex", "user", nil, searchJson) if len(out.Hits.Hits) == 1 { fmt.Println("%v", out.Hits.Hits[0].Source) } exitIfErr(err) }