Debian Codegraph @

indices_exists_type.go

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