| 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 |
// IndicesGetMappingService retrieves the mapping definitions for an index or |
| 17 |
// index/type. |
| 18 |
// |
| 19 |
// See https://www.elastic.co/guide/en/elasticsearch/reference/5.2/indices-get-mapping.html |
| 20 |
// for details. |
| 21 |
type IndicesGetMappingService struct { |
| 22 |
client *Client |
| 23 |
pretty bool |
| 24 |
index []string |
| 25 |
typ []string |
| 26 |
local *bool |
| 27 |
ignoreUnavailable *bool |
| 28 |
allowNoIndices *bool |
| 29 |
expandWildcards string |
| 30 |
} |
| 31 |
|
| 32 |
// NewGetMappingService is an alias for NewIndicesGetMappingService. |
| 33 |
// Use NewIndicesGetMappingService. |
| 34 |
func NewGetMappingService(client *Client) *IndicesGetMappingService { |
| 35 |
return NewIndicesGetMappingService(client) |
| 36 |
} |
| 37 |
|
| 38 |
// NewIndicesGetMappingService creates a new IndicesGetMappingService. |
| 39 |
func NewIndicesGetMappingService(client *Client) *IndicesGetMappingService { |
| 40 |
return &IndicesGetMappingService{ |
| 41 |
client: client, |
| 42 |
index: make([]string, 0), |
| 43 |
typ: make([]string, 0), |
| 44 |
} |
| 45 |
} |
| 46 |
|
| 47 |
// Index is a list of index names. |
| 48 |
func (s *IndicesGetMappingService) Index(indices ...string) *IndicesGetMappingService { |
| 49 |
s.index = append(s.index, indices...) |
| 50 |
return s |
| 51 |
} |
| 52 |
|
| 53 |
// Type is a list of document types. |
| 54 |
func (s *IndicesGetMappingService) Type(types ...string) *IndicesGetMappingService { |
| 55 |
s.typ = append(s.typ, types...) |
| 56 |
return s |
| 57 |
} |
| 58 |
|
| 59 |
// AllowNoIndices indicates whether to ignore if a wildcard indices |
| 60 |
// expression resolves into no concrete indices. |
| 61 |
// This includes `_all` string or when no indices have been specified. |
| 62 |
func (s *IndicesGetMappingService) AllowNoIndices(allowNoIndices bool) *IndicesGetMappingService { |
| 63 |
s.allowNoIndices = &allowNoIndices |
| 64 |
return s |
| 65 |
} |
| 66 |
|
| 67 |
// ExpandWildcards indicates whether to expand wildcard expression to |
| 68 |
// concrete indices that are open, closed or both.. |
| 69 |
func (s *IndicesGetMappingService) ExpandWildcards(expandWildcards string) *IndicesGetMappingService { |
| 70 |
s.expandWildcards = expandWildcards |
| 71 |
return s |
| 72 |
} |
| 73 |
|
| 74 |
// Local indicates whether to return local information, do not retrieve |
| 75 |
// the state from master node (default: false). |
| 76 |
func (s *IndicesGetMappingService) Local(local bool) *IndicesGetMappingService { |
| 77 |
s.local = &local |
| 78 |
return s |
| 79 |
} |
| 80 |
|
| 81 |
// IgnoreUnavailable indicates whether specified concrete indices should be |
| 82 |
// ignored when unavailable (missing or closed). |
| 83 |
func (s *IndicesGetMappingService) IgnoreUnavailable(ignoreUnavailable bool) *IndicesGetMappingService { |
| 84 |
s.ignoreUnavailable = &ignoreUnavailable |
| 85 |
return s |
| 86 |
} |
| 87 |
|
| 88 |
// Pretty indicates that the JSON response be indented and human readable. |
| 89 |
func (s *IndicesGetMappingService) Pretty(pretty bool) *IndicesGetMappingService { |
| 90 |
s.pretty = pretty |
| 91 |
return s |
| 92 |
} |
| 93 |
|
| 94 |
// buildURL builds the URL for the operation. |
| 95 |
func (s *IndicesGetMappingService) buildURL() (string, url.Values, error) { |
| 96 |
var index, typ []string |
| 97 |
|
| 98 |
if len(s.index) > 0 { |
| 99 |
index = s.index |
| 100 |
} else { |
| 101 |
index = []string{"_all"} |
| 102 |
} |
| 103 |
|
| 104 |
if len(s.typ) > 0 { |
| 105 |
typ = s.typ |
| 106 |
} else { |
| 107 |
typ = []string{"_all"} |
| 108 |
} |
| 109 |
|
| 110 |
// Build URL |
| 111 |
path, err := uritemplates.Expand("/{index}/_mapping/{type}", map[string]string{ |
| 112 |
"index": strings.Join(index, ","), |
| 113 |
"type": strings.Join(typ, ","), |
| 114 |
}) |
| 115 |
if err != nil { |
| 116 |
return "", url.Values{}, err |
| 117 |
} |
| 118 |
|
| 119 |
// Add query string parameters |
| 120 |
params := url.Values{} |
| 121 |
if s.pretty { |
| 122 |
params.Set("pretty", "1") |
| 123 |
} |
| 124 |
if s.ignoreUnavailable != nil { |
| 125 |
params.Set("ignore_unavailable", fmt.Sprintf("%v", *s.ignoreUnavailable)) |
| 126 |
} |
| 127 |
if s.allowNoIndices != nil { |
| 128 |
params.Set("allow_no_indices", fmt.Sprintf("%v", *s.allowNoIndices)) |
| 129 |
} |
| 130 |
if s.expandWildcards != "" { |
| 131 |
params.Set("expand_wildcards", s.expandWildcards) |
| 132 |
} |
| 133 |
if s.local != nil { |
| 134 |
params.Set("local", fmt.Sprintf("%v", *s.local)) |
| 135 |
} |
| 136 |
return path, params, nil |
| 137 |
} |
| 138 |
|
| 139 |
// Validate checks if the operation is valid. |
| 140 |
func (s *IndicesGetMappingService) Validate() error { |
| 141 |
return nil |
| 142 |
} |
| 143 |
|
| 144 |
// Do executes the operation. It returns mapping definitions for an index |
| 145 |
// or index/type. |
| 146 |
func (s *IndicesGetMappingService) Do(ctx context.Context) (map[string]interface{}, error) { |
| 147 |
// Check pre-conditions |
| 148 |
if err := s.Validate(); err != nil { |
| 149 |
return nil, err |
| 150 |
} |
| 151 |
|
| 152 |
// Get URL for request |
| 153 |
path, params, err := s.buildURL() |
| 154 |
if err != nil { |
| 155 |
return nil, err |
| 156 |
} |
| 157 |
|
| 158 |
// Get HTTP response |
| 159 |
res, err := s.client.PerformRequest(ctx, "GET", path, params, nil) |
| 160 |
if err != nil { |
| 161 |
return nil, err |
| 162 |
} |
| 163 |
|
| 164 |
// Return operation response |
| 165 |
var ret map[string]interface{} |
| 166 |
if err := s.client.decoder.Decode(res.Body, &ret); err != nil { |
| 167 |
return nil, err |
| 168 |
} |
| 169 |
return ret, nil |
| 170 |
} |