// for testing func main() { flag.Parse() log.SetFlags(log.Ltime | log.Lshortfile) api.Domain = *eshost response, _ := core.Index(true, "twitter", "tweet", "1", NewTweet("kimchy", "Search is cool")) indices.Flush() log.Printf("Index OK: %v", response.Ok) searchresponse, err := core.SearchRequest(true, "twitter", "tweet", "{\"query\" : {\"term\" : { \"user\" : \"kimchy\" }}}", "", 0) if err != nil { log.Println("error during search:" + err.Error()) log.Fatal(err) } // try marshalling to tweet type var t Tweet json.Unmarshal(searchresponse.Hits.Hits[0].Source, t) log.Printf("Search Found: %s", t) response, _ = core.Get(true, "twitter", "tweet", "1") log.Printf("Get: %v", response.Exists) exists, _ := core.Exists(true, "twitter", "tweet", "1") log.Printf("Exists: %v", exists) indices.Flush() countResponse, _ := core.Count(true, "twitter", "tweet") log.Printf("Count: %v", countResponse.Count) response, _ = core.Delete(true, "twitter", "tweet", "1", -1, "") log.Printf("Delete OK: %v", response.Ok) response, _ = core.Get(true, "twitter", "tweet", "1") log.Printf("Get: %v", response.Exists) healthResponse, _ := cluster.Health(true) log.Printf("Health: %v", healthResponse.Status) cluster.State("transient", "discovery.zen.minimum_master_nodes", 2) }
// elasticsearchHealthCheck() determines if elasticsearch is healthy func elasticsearchHealthCheck() error { start := time.Now() lastError := time.Now() minUptime := time.Second * 2 timeout := time.Second * 30 schemaFile := localDir("resources/controlplane.json") for { if healthResponse, err := cluster.Health(true); err == nil && (healthResponse.Status == "green" || healthResponse.Status == "yellow") { if buffer, err := os.Open(schemaFile); err != nil { glog.Fatalf("problem reading %s", err) return err } else { http.Post("http://localhost:9200/controlplane", "application/json", buffer) buffer.Close() } } else { lastError = time.Now() glog.V(2).Infof("Still trying to connect to elastic: %v: %s", err, healthResponse) } if time.Since(lastError) > minUptime { break } if time.Since(start) > timeout { return fmt.Errorf("Could not startup elastic search container.") } time.Sleep(time.Millisecond * 1000) } glog.Info("elasticsearch container started, browser at http://localhost:9200/_plugin/head/") return nil }
// for testing func main() { flag.Parse() log.SetFlags(log.Ltime | log.Lshortfile) api.Domain = *eshost core.VerboseLogging = true response, _ := core.Index("twitter", "tweet", "1", nil, NewTweet("kimchy", "Search is cool")) indices.Flush() log.Printf("Index OK: %v", response.Ok) searchresponse, err := core.SearchRequest("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, _ = core.Get("twitter", "tweet", "1", nil) log.Printf("Get: %v", response.Exists) exists, _ := core.Exists("twitter", "tweet", "1", nil) log.Printf("Exists: %v", exists) indices.Flush() countResponse, _ := core.Count("twitter", "tweet", nil) log.Printf("Count: %v", countResponse.Count) response, _ = core.Delete("twitter", "tweet", "1", map[string]interface{}{"version": -1, "routing": ""}) log.Printf("Delete OK: %v", response.Ok) response, _ = core.Get("twitter", "tweet", "1", nil) log.Printf("Get: %v", response.Exists) healthResponse, _ := cluster.Health() log.Printf("Health: %v", healthResponse.Status) cluster.UpdateSettings("transient", "discovery.zen.minimum_master_nodes", 2) }