in opensearchapi/api.index.go [88:224]
func (r IndexRequest) Do(ctx context.Context, transport Transport) (*Response, error) {
var (
method string
path strings.Builder
params map[string]string
)
if r.DocumentID != "" {
method = "PUT"
} else {
method = "POST"
}
if r.DocumentType == "" {
r.DocumentType = "_doc"
}
path.Grow(1 + len(r.Index) + 1 + len(r.DocumentType) + 1 + len(r.DocumentID))
path.WriteString("/")
path.WriteString(r.Index)
if r.DocumentType != "" {
path.WriteString("/")
path.WriteString(r.DocumentType)
}
if r.DocumentID != "" {
path.WriteString("/")
path.WriteString(r.DocumentID)
}
params = make(map[string]string)
if r.IfPrimaryTerm != nil {
params["if_primary_term"] = strconv.FormatInt(int64(*r.IfPrimaryTerm), 10)
}
if r.IfSeqNo != nil {
params["if_seq_no"] = strconv.FormatInt(int64(*r.IfSeqNo), 10)
}
if r.OpType != "" {
params["op_type"] = r.OpType
}
if r.Pipeline != "" {
params["pipeline"] = r.Pipeline
}
if r.Refresh != "" {
params["refresh"] = r.Refresh
}
if r.RequireAlias != nil {
params["require_alias"] = strconv.FormatBool(*r.RequireAlias)
}
if r.Routing != "" {
params["routing"] = r.Routing
}
if r.Timeout != 0 {
params["timeout"] = formatDuration(r.Timeout)
}
if r.Version != nil {
params["version"] = strconv.FormatInt(int64(*r.Version), 10)
}
if r.VersionType != "" {
params["version_type"] = r.VersionType
}
if r.WaitForActiveShards != "" {
params["wait_for_active_shards"] = r.WaitForActiveShards
}
if r.Pretty {
params["pretty"] = "true"
}
if r.Human {
params["human"] = "true"
}
if r.ErrorTrace {
params["error_trace"] = "true"
}
if len(r.FilterPath) > 0 {
params["filter_path"] = strings.Join(r.FilterPath, ",")
}
req, err := newRequest(method, path.String(), r.Body)
if err != nil {
return nil, err
}
if len(params) > 0 {
q := req.URL.Query()
for k, v := range params {
q.Set(k, v)
}
req.URL.RawQuery = q.Encode()
}
if r.Body != nil {
req.Header[headerContentType] = headerContentTypeJSON
}
if len(r.Header) > 0 {
if len(req.Header) == 0 {
req.Header = r.Header
} else {
for k, vv := range r.Header {
for _, v := range vv {
req.Header.Add(k, v)
}
}
}
}
if ctx != nil {
req = req.WithContext(ctx)
}
res, err := transport.Perform(req)
if err != nil {
return nil, err
}
response := Response{
StatusCode: res.StatusCode,
Body: res.Body,
Header: res.Header,
}
return &response, nil
}