MCPcopy
hub / github.com/elastic/go-elasticsearch / Create

Method Create

_examples/xkcdsearch/store.go:86–112  ·  view source on GitHub ↗

Create indexes a new document into store.

(item *Document)

Source from the content-addressed store, hash-verified

84
85// Create indexes a new document into store.
86func (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.
115func (s *Store) Exists(id string) (bool, error) {

Callers 12

CreateIndexMethod · 0.45
storeDocumentMethod · 0.45
setupIndexFunction · 0.45
mainFunction · 0.45
mainFunction · 0.45
setupIndexFunction · 0.45
mainFunction · 0.45
setupMethod · 0.45
setupDemoDataFunction · 0.45
initFunction · 0.45
initFunction · 0.45
initFunction · 0.45

Calls 4

IsErrorMethod · 0.80
DoMethod · 0.65
CloseMethod · 0.65
StatusMethod · 0.45

Tested by

no test coverage detected