/
api.document.index.go
56 lines (48 loc) · 1.21 KB
/
api.document.index.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
package api
import (
"net/http"
)
func newDocumentIndexFunc(transport Transport) DocumentIndex {
return func(index string, doc Doc, o ...func(*DocumentIndexRequest)) (*Response, error) {
body:=doc.Content()
r := DocumentIndexRequest{BaseRequest: BaseRequest{Index: index, Body: &body}}
r.uris=r.getUris
r.method=r.getMethod
for _, f := range o {
f(&r)
}
return r.Do(r.ctx, transport)
}
}
type DocumentIndex func(index string, doc Doc, o ...func(*DocumentIndexRequest)) (*Response, error)
func (f DocumentIndex) WithDocumentID(v string) func(*DocumentIndexRequest) {
return func(r *DocumentIndexRequest) {
r.DocumentID = v
}
}
func (f DocumentIndex) WithOpType(v string) func(*DocumentIndexRequest) {
return func(r *DocumentIndexRequest) {
r.OpType = v
r.AddParam("op_type", v)
}
}
type DocumentIndexRequest struct {
BaseRequest
DocumentID string
OpType string
}
func (r *DocumentIndexRequest) getMethod() string {
if r.DocumentID != "" {
return http.MethodPut
} else {
return http.MethodPost
}
}
func (r *DocumentIndexRequest) getUris() []string {
var uris []string
uris = append(uris, r.Index, "_doc")
if r.DocumentID != "" {
uris = append(uris, r.DocumentID)
}
return uris
}