| 1 |
// Copyright 2012-present Oliver Eilhard. All rights reserved. |
| 2 |
// Use of this source code is governed by a MIT-license. |
| 3 |
// See http://olivere.mit-license.org/license.txt for details. |
| 4 |
|
| 5 |
package elastic |
| 6 |
|
| 7 |
import ( |
| 8 |
"context" |
| 9 |
"fmt" |
| 10 |
"net/url" |
| 11 |
"strings" |
| 12 |
|
| 13 |
"gopkg.in/olivere/elastic.v5/uritemplates" |
| 14 |
) |
| 15 |
|
| 16 |
// IndicesPutMappingService allows to register specific mapping definition |
| 17 |
// for a specific type. |
| 18 |
// |
| 19 |
// See https://www.elastic.co/guide/en/elasticsearch/reference/5.2/indices-put-mapping.html |
| 20 |
// for details. |
| 21 |
type IndicesPutMappingService struct { |
| 22 |
client *Client |
| 23 |
pretty bool |
| 24 |
typ string |
| 25 |
index []string |
| 26 |
masterTimeout string |
| 27 |
ignoreUnavailable *bool |
| 28 |
allowNoIndices *bool |
| 29 |
expandWildcards string |
| 30 |
updateAllTypes *bool |
| 31 |
timeout string |
| 32 |
bodyJson map[string]interface{} |
| 33 |
bodyString string |
| 34 |
} |
| 35 |
|
| 36 |
// NewPutMappingService is an alias for NewIndicesPutMappingService. |
| 37 |
// Use NewIndicesPutMappingService. |
| 38 |
func NewPutMappingService(client *Client) *IndicesPutMappingService { |
| 39 |
return NewIndicesPutMappingService(client) |
| 40 |
} |
| 41 |
|
| 42 |
// NewIndicesPutMappingService creates a new IndicesPutMappingService. |
| 43 |
func NewIndicesPutMappingService(client *Client) *IndicesPutMappingService { |
| 44 |
return &IndicesPutMappingService{ |
| 45 |
client: client, |
| 46 |
index: make([]string, 0), |
| 47 |
} |
| 48 |
} |
| 49 |
|
| 50 |
// Index is a list of index names the mapping should be added to |
| 51 |
// (supports wildcards); use `_all` or omit to add the mapping on all indices. |
| 52 |
func (s *IndicesPutMappingService) Index(indices ...string) *IndicesPutMappingService { |
| 53 |
s.index = append(s.index, indices...) |
| 54 |
return s |
| 55 |
} |
| 56 |
|
| 57 |
// Type is the name of the document type. |
| 58 |
func (s *IndicesPutMappingService) Type(typ string) *IndicesPutMappingService { |
| 59 |
s.typ = typ |
| 60 |
return s |
| 61 |
} |
| 62 |
|
| 63 |
// Timeout is an explicit operation timeout. |
| 64 |
func (s *IndicesPutMappingService) Timeout(timeout string) *IndicesPutMappingService { |
| 65 |
s.timeout = timeout |
| 66 |
return s |
| 67 |
} |
| 68 |
|
| 69 |
// MasterTimeout specifies the timeout for connection to master. |
| 70 |
func (s *IndicesPutMappingService) MasterTimeout(masterTimeout string) *IndicesPutMappingService { |
| 71 |
s.masterTimeout = masterTimeout |
| 72 |
return s |
| 73 |
} |
| 74 |
|
| 75 |
// IgnoreUnavailable indicates whether specified concrete indices should be |
| 76 |
// ignored when unavailable (missing or closed). |
| 77 |
func (s *IndicesPutMappingService) IgnoreUnavailable(ignoreUnavailable bool) *IndicesPutMappingService { |
| 78 |
s.ignoreUnavailable = &ignoreUnavailable |
| 79 |
return s |
| 80 |
} |
| 81 |
|
| 82 |
// AllowNoIndices indicates whether to ignore if a wildcard indices |
| 83 |
// expression resolves into no concrete indices. |
| 84 |
// This includes `_all` string or when no indices have been specified. |
| 85 |
func (s *IndicesPutMappingService) AllowNoIndices(allowNoIndices bool) *IndicesPutMappingService { |
| 86 |
s.allowNoIndices = &allowNoIndices |
| 87 |
return s |
| 88 |
} |
| 89 |
|
| 90 |
// ExpandWildcards indicates whether to expand wildcard expression to |
| 91 |
// concrete indices that are open, closed or both. |
| 92 |
func (s *IndicesPutMappingService) ExpandWildcards(expandWildcards string) *IndicesPutMappingService { |
| 93 |
s.expandWildcards = expandWildcards |
| 94 |
return s |
| 95 |
} |
| 96 |
|
| 97 |
// UpdateAllTypes, if true, indicates that all fields that span multiple indices |
| 98 |
// should be updated (default: false). |
| 99 |
func (s *IndicesPutMappingService) UpdateAllTypes(updateAllTypes bool) *IndicesPutMappingService { |
| 100 |
s.updateAllTypes = &updateAllTypes |
| 101 |
return s |
| 102 |
} |
| 103 |
|
| 104 |
// Pretty indicates that the JSON response be indented and human readable. |
| 105 |
func (s *IndicesPutMappingService) Pretty(pretty bool) *IndicesPutMappingService { |
| 106 |
s.pretty = pretty |
| 107 |
return s |
| 108 |
} |
| 109 |
|
| 110 |
// BodyJson contains the mapping definition. |
| 111 |
func (s *IndicesPutMappingService) BodyJson(mapping map[string]interface{}) *IndicesPutMappingService { |
| 112 |
s.bodyJson = mapping |
| 113 |
return s |
| 114 |
} |
| 115 |
|
| 116 |
// BodyString is the mapping definition serialized as a string. |
| 117 |
func (s *IndicesPutMappingService) BodyString(mapping string) *IndicesPutMappingService { |
| 118 |
s.bodyString = mapping |
| 119 |
return s |
| 120 |
} |
| 121 |
|
| 122 |
// buildURL builds the URL for the operation. |
| 123 |
func (s *IndicesPutMappingService) buildURL() (string, url.Values, error) { |
| 124 |
var err error |
| 125 |
var path string |
| 126 |
|
| 127 |
// Build URL: Typ MUST be specified and is verified in Validate. |
| 128 |
if len(s.index) > 0 { |
| 129 |
path, err = uritemplates.Expand("/{index}/_mapping/{type}", map[string]string{ |
| 130 |
"index": strings.Join(s.index, ","), |
| 131 |
"type": s.typ, |
| 132 |
}) |
| 133 |
} else { |
| 134 |
path, err = uritemplates.Expand("/_mapping/{type}", map[string]string{ |
| 135 |
"type": s.typ, |
| 136 |
}) |
| 137 |
} |
| 138 |
if err != nil { |
| 139 |
return "", url.Values{}, err |
| 140 |
} |
| 141 |
|
| 142 |
// Add query string parameters |
| 143 |
params := url.Values{} |
| 144 |
if s.pretty { |
| 145 |
params.Set("pretty", "1") |
| 146 |
} |
| 147 |
if s.ignoreUnavailable != nil { |
| 148 |
params.Set("ignore_unavailable", fmt.Sprintf("%v", *s.ignoreUnavailable)) |
| 149 |
} |
| 150 |
if s.allowNoIndices != nil { |
| 151 |
params.Set("allow_no_indices", fmt.Sprintf("%v", *s.allowNoIndices)) |
| 152 |
} |
| 153 |
if s.expandWildcards != "" { |
| 154 |
params.Set("expand_wildcards", s.expandWildcards) |
| 155 |
} |
| 156 |
if s.updateAllTypes != nil { |
| 157 |
params.Set("update_all_types", fmt.Sprintf("%v", *s.updateAllTypes)) |
| 158 |
} |
| 159 |
if s.timeout != "" { |
| 160 |
params.Set("timeout", s.timeout) |
| 161 |
} |
| 162 |
if s.masterTimeout != "" { |
| 163 |
params.Set("master_timeout", s.masterTimeout) |
| 164 |
} |
| 165 |
return path, params, nil |
| 166 |
} |
| 167 |
|
| 168 |
// Validate checks if the operation is valid. |
| 169 |
func (s *IndicesPutMappingService) Validate() error { |
| 170 |
var invalid []string |
| 171 |
if s.typ == "" { |
| 172 |
invalid = append(invalid, "Type") |
| 173 |
} |
| 174 |
if s.bodyString == "" && s.bodyJson == nil { |
| 175 |
invalid = append(invalid, "BodyJson") |
| 176 |
} |
| 177 |
if len(invalid) > 0 { |
| 178 |
return fmt.Errorf("missing required fields: %v", invalid) |
| 179 |
} |
| 180 |
return nil |
| 181 |
} |
| 182 |
|
| 183 |
// Do executes the operation. |
| 184 |
func (s *IndicesPutMappingService) Do(ctx context.Context) (*PutMappingResponse, error) { |
| 185 |
// Check pre-conditions |
| 186 |
if err := s.Validate(); err != nil { |
| 187 |
return nil, err |
| 188 |
} |
| 189 |
|
| 190 |
// Get URL for request |
| 191 |
path, params, err := s.buildURL() |
| 192 |
if err != nil { |
| 193 |
return nil, err |
| 194 |
} |
| 195 |
|
| 196 |
// Setup HTTP request body |
| 197 |
var body interface{} |
| 198 |
if s.bodyJson != nil { |
| 199 |
body = s.bodyJson |
| 200 |
} else { |
| 201 |
body = s.bodyString |
| 202 |
} |
| 203 |
|
| 204 |
// Get HTTP response |
| 205 |
res, err := s.client.PerformRequest(ctx, "PUT", path, params, body) |
| 206 |
if err != nil { |
| 207 |
return nil, err |
| 208 |
} |
| 209 |
|
| 210 |
// Return operation response |
| 211 |
ret := new(PutMappingResponse) |
| 212 |
if err := s.client.decoder.Decode(res.Body, ret); err != nil { |
| 213 |
return nil, err |
| 214 |
} |
| 215 |
return ret, nil |
| 216 |
} |
| 217 |
|
| 218 |
// PutMappingResponse is the response of IndicesPutMappingService.Do. |
| 219 |
type PutMappingResponse struct { |
| 220 |
Acknowledged bool `json:"acknowledged"` |
| 221 |
} |