Create indexes a new document into store.
(item *Document)
| 84 | |
| 85 | // Create indexes a new document into store. |
| 86 | func (s *Store) Create(item *Document) error { |
| 87 | payload, err := json.Marshal(item) |
| 88 | if err != nil { |
| 89 | return err |
| 90 | } |
| 91 | |
| 92 | ctx := context.Background() |
| 93 | res, err := esapi.CreateRequest{ |
| 94 | Index: s.indexName, |
| 95 | DocumentID: item.ID, |
| 96 | Body: bytes.NewReader(payload), |
| 97 | }.Do(ctx, s.es) |
| 98 | if err != nil { |
| 99 | return err |
| 100 | } |
| 101 | defer res.Body.Close() |
| 102 | |
| 103 | if res.IsError() { |
| 104 | var e map[string]interface{} |
| 105 | if err := json.NewDecoder(res.Body).Decode(&e); err != nil { |
| 106 | return err |
| 107 | } |
| 108 | return fmt.Errorf("[%s] %s: %s", res.Status(), e["error"].(map[string]interface{})["type"], e["error"].(map[string]interface{})["reason"]) |
| 109 | } |
| 110 | |
| 111 | return nil |
| 112 | } |
| 113 | |
| 114 | // Exists returns true when a document with id already exists in the store. |
| 115 | func (s *Store) Exists(id string) (bool, error) { |
no test coverage detected