()
| 52 | } |
| 53 | |
| 54 | func main() { |
| 55 | es, err := elasticsearch.New() |
| 56 | if err != nil { |
| 57 | fmt.Printf("Error creating the client: %s\n", err) |
| 58 | os.Exit(2) |
| 59 | } |
| 60 | defer func() { |
| 61 | ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second) |
| 62 | defer cancel() |
| 63 | if err := es.Close(ctx); err != nil { |
| 64 | fmt.Printf("Error closing the client: %s\n", err) |
| 65 | } |
| 66 | }() |
| 67 | |
| 68 | fnames = []string{"Alice", "John", "Mary"} |
| 69 | |
| 70 | for i, title := range []string{"One", "Two", "Three", "Four", "Five"} { |
| 71 | articles = append(articles, |
| 72 | model.Article{ |
| 73 | ID: uint(i + 1), |
| 74 | Title: "Test " + title, |
| 75 | Body: "Lorem ipsum dolor sit amet, consectetur adipisicing elit", |
| 76 | Published: time.Now().AddDate(i, 0, 0), |
| 77 | Author: &model.Author{ |
| 78 | FirstName: fnames[rand.Intn(len(fnames))], |
| 79 | LastName: "Smith", |
| 80 | }, |
| 81 | }) |
| 82 | } |
| 83 | |
| 84 | faint.Println("Indexing articles...") |
| 85 | faint.Println(strings.Repeat("━", 80)) |
| 86 | |
| 87 | var b bytes.Buffer |
| 88 | for _, a := range articles { |
| 89 | b.Reset() |
| 90 | |
| 91 | // Encode article to JSON |
| 92 | if _, err := easyjson.MarshalToWriter(a, &b); err != nil { |
| 93 | red.Println("Error decoding response", err) |
| 94 | continue |
| 95 | } |
| 96 | |
| 97 | res, err := es.Index( |
| 98 | "articles", |
| 99 | bytes.NewReader(b.Bytes()), |
| 100 | es.Index.WithDocumentID(strconv.Itoa(int(a.ID))), |
| 101 | // es.Index.WithVersion(-1), // <-- Uncomment to trigger error response |
| 102 | ) |
| 103 | if err != nil { |
| 104 | red.Printf("Error indexing article: %s\n", err) |
| 105 | continue |
| 106 | } |
| 107 | defer res.Body.Close() |
| 108 | |
| 109 | if res.IsError() { |
| 110 | printErrorResponse(res) |
| 111 | continue |
nothing calls this directly
no test coverage detected