-
Notifications
You must be signed in to change notification settings - Fork 0
/
document.go
65 lines (51 loc) · 1.37 KB
/
document.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
package zinc
import "context"
// DocumentService provides methods to create documents.
type DocumentService struct {
cli *Client
index string
data any
}
// NewDocumentService returns a fully initialized DocumentService.
func NewDocumentService(cli *Client) *DocumentService {
return &DocumentService{
cli: cli,
}
}
// SetIndex sets the target index of the document.
func (d *DocumentService) SetIndex(index string) *DocumentService {
d.index = index
return d
}
// SetData sets the target data of the document.
func (d *DocumentService) SetData(data any) *DocumentService {
d.data = data
return d
}
// Create creates a new document.
func (d *DocumentService) Create(ctx context.Context) (DocumentCreateResponse, error) {
var resp DocumentCreateResponse
_, err := d.cli.c.R().
SetResult(&resp).
SetBody(d.data).
SetPathParam("index", d.index).
Post("/{index}/_doc")
if err != nil {
return DocumentCreateResponse{}, err
}
return resp, nil
}
// CreateWithID creates a new document with a custom ID.
func (d *DocumentService) CreateWithID(ctx context.Context, id string) (DocumentCreateResponse, error) {
var resp DocumentCreateResponse
_, err := d.cli.c.R().
SetResult(&resp).
SetBody(d.data).
SetPathParam("index", d.index).
SetPathParam("id", id).
Post("/{index}/_doc/{id}")
if err != nil {
return DocumentCreateResponse{}, err
}
return resp, nil
}