| 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/http" |
| 11 |
"net/url" |
| 12 |
"strings" |
| 13 |
|
| 14 |
"gopkg.in/olivere/elastic.v5/uritemplates" |
| 15 |
) |
| 16 |
|
| 17 |
// FieldCapsService allows retrieving the capabilities of fields among multiple indices. |
| 18 |
// |
| 19 |
// See http://www.elastic.co/guide/en/elasticsearch/reference/5.x/search-field-caps.html |
| 20 |
// for details |
| 21 |
type FieldCapsService struct { |
| 22 |
client *Client |
| 23 |
pretty bool |
| 24 |
index []string |
| 25 |
allowNoIndices *bool |
| 26 |
expandWildcards string |
| 27 |
fields []string |
| 28 |
ignoreUnavailable *bool |
| 29 |
bodyJson interface{} |
| 30 |
bodyString string |
| 31 |
} |
| 32 |
|
| 33 |
// NewFieldCapsService creates a new FieldCapsService |
| 34 |
func NewFieldCapsService(client *Client) *FieldCapsService { |
| 35 |
return &FieldCapsService{ |
| 36 |
client: client, |
| 37 |
} |
| 38 |
} |
| 39 |
|
| 40 |
// Index is a list of index names; use `_all` or empty string to perform |
| 41 |
// the operation on all indices. |
| 42 |
func (s *FieldCapsService) Index(index ...string) *FieldCapsService { |
| 43 |
s.index = append(s.index, index...) |
| 44 |
return s |
| 45 |
} |
| 46 |
|
| 47 |
// AllowNoIndices indicates whether to ignore if a wildcard indices expression |
| 48 |
// resolves into no concrete indices. |
| 49 |
// (This includes `_all` string or when no indices have been specified). |
| 50 |
func (s *FieldCapsService) AllowNoIndices(allowNoIndices bool) *FieldCapsService { |
| 51 |
s.allowNoIndices = &allowNoIndices |
| 52 |
return s |
| 53 |
} |
| 54 |
|
| 55 |
// ExpandWildcards indicates whether to expand wildcard expression to |
| 56 |
// concrete indices that are open, closed or both. |
| 57 |
func (s *FieldCapsService) ExpandWildcards(expandWildcards string) *FieldCapsService { |
| 58 |
s.expandWildcards = expandWildcards |
| 59 |
return s |
| 60 |
} |
| 61 |
|
| 62 |
// Fields is a list of fields for to get field capabilities. |
| 63 |
func (s *FieldCapsService) Fields(fields ...string) *FieldCapsService { |
| 64 |
s.fields = append(s.fields, fields...) |
| 65 |
return s |
| 66 |
} |
| 67 |
|
| 68 |
// IgnoreUnavailable is documented as: Whether specified concrete indices should be ignored when unavailable (missing or closed). |
| 69 |
func (s *FieldCapsService) IgnoreUnavailable(ignoreUnavailable bool) *FieldCapsService { |
| 70 |
s.ignoreUnavailable = &ignoreUnavailable |
| 71 |
return s |
| 72 |
} |
| 73 |
|
| 74 |
// Pretty indicates that the JSON response be indented and human readable. |
| 75 |
func (s *FieldCapsService) Pretty(pretty bool) *FieldCapsService { |
| 76 |
s.pretty = pretty |
| 77 |
return s |
| 78 |
} |
| 79 |
|
| 80 |
// BodyJson is documented as: Field json objects containing the name and optionally a range to filter out indices result, that have results outside the defined bounds. |
| 81 |
func (s *FieldCapsService) BodyJson(body interface{}) *FieldCapsService { |
| 82 |
s.bodyJson = body |
| 83 |
return s |
| 84 |
} |
| 85 |
|
| 86 |
// BodyString is documented as: Field json objects containing the name and optionally a range to filter out indices result, that have results outside the defined bounds. |
| 87 |
func (s *FieldCapsService) BodyString(body string) *FieldCapsService { |
| 88 |
s.bodyString = body |
| 89 |
return s |
| 90 |
} |
| 91 |
|
| 92 |
// buildURL builds the URL for the operation. |
| 93 |
func (s *FieldCapsService) buildURL() (string, url.Values, error) { |
| 94 |
// Build URL |
| 95 |
var err error |
| 96 |
var path string |
| 97 |
if len(s.index) > 0 { |
| 98 |
path, err = uritemplates.Expand("/{index}/_field_caps", map[string]string{ |
| 99 |
"index": strings.Join(s.index, ","), |
| 100 |
}) |
| 101 |
} else { |
| 102 |
path = "/_field_caps" |
| 103 |
} |
| 104 |
if err != nil { |
| 105 |
return "", url.Values{}, err |
| 106 |
} |
| 107 |
|
| 108 |
// Add query string parameters |
| 109 |
params := url.Values{} |
| 110 |
if s.pretty { |
| 111 |
params.Set("pretty", "true") |
| 112 |
} |
| 113 |
if s.allowNoIndices != nil { |
| 114 |
params.Set("allow_no_indices", fmt.Sprintf("%v", *s.allowNoIndices)) |
| 115 |
} |
| 116 |
if s.expandWildcards != "" { |
| 117 |
params.Set("expand_wildcards", s.expandWildcards) |
| 118 |
} |
| 119 |
if len(s.fields) > 0 { |
| 120 |
params.Set("fields", strings.Join(s.fields, ",")) |
| 121 |
} |
| 122 |
if s.ignoreUnavailable != nil { |
| 123 |
params.Set("ignore_unavailable", fmt.Sprintf("%v", *s.ignoreUnavailable)) |
| 124 |
} |
| 125 |
return path, params, nil |
| 126 |
} |
| 127 |
|
| 128 |
// Validate checks if the operation is valid. |
| 129 |
func (s *FieldCapsService) Validate() error { |
| 130 |
return nil |
| 131 |
} |
| 132 |
|
| 133 |
// Do executes the operation. |
| 134 |
func (s *FieldCapsService) Do(ctx context.Context) (*FieldCapsResponse, error) { |
| 135 |
// Check pre-conditions |
| 136 |
if err := s.Validate(); err != nil { |
| 137 |
return nil, err |
| 138 |
} |
| 139 |
|
| 140 |
// Get URL for request |
| 141 |
path, params, err := s.buildURL() |
| 142 |
if err != nil { |
| 143 |
return nil, err |
| 144 |
} |
| 145 |
|
| 146 |
// Setup HTTP request body |
| 147 |
var body interface{} |
| 148 |
if s.bodyJson != nil { |
| 149 |
body = s.bodyJson |
| 150 |
} else { |
| 151 |
body = s.bodyString |
| 152 |
} |
| 153 |
|
| 154 |
// Get HTTP response |
| 155 |
res, err := s.client.PerformRequest(ctx, "POST", path, params, body, http.StatusNotFound) |
| 156 |
if err != nil { |
| 157 |
return nil, err |
| 158 |
} |
| 159 |
|
| 160 |
// TODO(oe): Is 404 really a valid response here? |
| 161 |
if res.StatusCode == http.StatusNotFound { |
| 162 |
return &FieldCapsResponse{}, nil |
| 163 |
} |
| 164 |
|
| 165 |
// Return operation response |
| 166 |
ret := new(FieldCapsResponse) |
| 167 |
if err := s.client.decoder.Decode(res.Body, ret); err != nil { |
| 168 |
return nil, err |
| 169 |
} |
| 170 |
return ret, nil |
| 171 |
} |
| 172 |
|
| 173 |
// -- Request -- |
| 174 |
|
| 175 |
// FieldCapsRequest can be used to set up the body to be used in the |
| 176 |
// Field Capabilities API. |
| 177 |
type FieldCapsRequest struct { |
| 178 |
Fields []string `json:"fields"` |
| 179 |
} |
| 180 |
|
| 181 |
// -- Response -- |
| 182 |
|
| 183 |
// FieldCapsResponse contains field capabilities. |
| 184 |
type FieldCapsResponse struct { |
| 185 |
Fields map[string]FieldCaps `json:"fields,omitempty"` |
| 186 |
} |
| 187 |
|
| 188 |
// FieldCaps contains capabilities of an individual field. |
| 189 |
type FieldCaps struct { |
| 190 |
Type string `json:"type"` |
| 191 |
Searchable bool `json:"searchable"` |
| 192 |
Aggregatable bool `json:"aggregatable"` |
| 193 |
Indices []string `json:"indices,omitempty"` |
| 194 |
NonSearchableIndices []string `json:"non_searchable_indices,omitempty"` |
| 195 |
NonAggregatableIndices []string `json:"non_aggregatable_indices,omitempty"` |
| 196 |
} |