forked from olivere/elastic
-
Notifications
You must be signed in to change notification settings - Fork 0
/
get.go
204 lines (179 loc) · 4.74 KB
/
get.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
// Copyright 2012-2014 Oliver Eilhard. All rights reserved.
// Use of this source code is governed by a MIT-license.
// See http://olivere.mit-license.org/license.txt for details.
package elastic
import (
"encoding/json"
"fmt"
"net/http"
"net/url"
"strings"
"github.com/olivere/elastic/uritemplates"
)
type GetService struct {
client *Client
index string
_type string
id string
routing string
preference string
fields []string
fsc *FetchSourceContext
refresh *bool
realtime *bool
version *int64 // see org.elasticsearch.common.lucene.uid.Versions
versionType string // see org.elasticsearch.index.VersionType
ignoreErrOnGeneratedFields *bool
}
func NewGetService(client *Client) *GetService {
builder := &GetService{
client: client,
_type: "_all",
}
return builder
}
func (b *GetService) String() string {
return fmt.Sprintf("[%v][%v][%v]: routing [%v]",
b.index,
b._type,
b.id,
b.routing)
}
func (b *GetService) Index(index string) *GetService {
b.index = index
return b
}
func (b *GetService) Type(_type string) *GetService {
b._type = _type
return b
}
func (b *GetService) Id(id string) *GetService {
b.id = id
return b
}
func (b *GetService) Parent(parent string) *GetService {
if b.routing == "" {
b.routing = parent
}
return b
}
func (b *GetService) Routing(routing string) *GetService {
b.routing = routing
return b
}
func (b *GetService) Preference(preference string) *GetService {
b.preference = preference
return b
}
func (b *GetService) Fields(fields ...string) *GetService {
if b.fields == nil {
b.fields = make([]string, 0)
}
b.fields = append(b.fields, fields...)
return b
}
func (s *GetService) FetchSource(fetchSource bool) *GetService {
if s.fsc == nil {
s.fsc = NewFetchSourceContext(fetchSource)
} else {
s.fsc.SetFetchSource(fetchSource)
}
return s
}
func (s *GetService) FetchSourceContext(fetchSourceContext *FetchSourceContext) *GetService {
s.fsc = fetchSourceContext
return s
}
func (b *GetService) Refresh(refresh bool) *GetService {
b.refresh = &refresh
return b
}
func (b *GetService) Realtime(realtime bool) *GetService {
b.realtime = &realtime
return b
}
// Version can be MatchAny (-3), MatchAnyPre120 (0), NotFound (-1),
// or NotSet (-2). These are specified in org.elasticsearch.common.lucene.uid.Versions.
// The default is MatchAny (-3).
func (b *GetService) Version(version int64) *GetService {
b.version = &version
return b
}
// VersionType can be "internal", "external", "external_gt", "external_gte",
// or "force". See org.elasticsearch.index.VersionType in Elasticsearch source.
// It is "internal" by default.
func (b *GetService) VersionType(versionType string) *GetService {
b.versionType = versionType
return b
}
func (b *GetService) Do() (*GetResult, error) {
// Build url
urls, err := uritemplates.Expand("/{index}/{type}/{id}", map[string]string{
"index": b.index,
"type": b._type,
"id": b.id,
})
if err != nil {
return nil, err
}
params := make(url.Values)
if b.realtime != nil {
params.Add("realtime", fmt.Sprintf("%v", *b.realtime))
}
if len(b.fields) > 0 {
params.Add("fields", strings.Join(b.fields, ","))
}
if b.routing != "" {
params.Add("routing", b.routing)
}
if b.preference != "" {
params.Add("preference", b.preference)
}
if b.refresh != nil {
params.Add("refresh", fmt.Sprintf("%v", *b.refresh))
}
if b.version != nil {
params.Add("_version", fmt.Sprintf("%d", *b.version))
}
if b.versionType != "" {
params.Add("_version_type", b.versionType)
}
if b.fsc != nil {
for k, values := range b.fsc.Query() {
params.Add(k, strings.Join(values, ","))
}
}
if len(params) > 0 {
urls += "?" + params.Encode()
}
// Set up a new request
req, err := b.client.NewRequest("GET", urls)
if err != nil {
return nil, err
}
// Get response
res, err := b.client.c.Do((*http.Request)(req))
if err != nil {
return nil, err
}
if err := checkResponse(res); err != nil {
return nil, err
}
defer res.Body.Close()
ret := new(GetResult)
if err := json.NewDecoder(res.Body).Decode(ret); err != nil {
return nil, err
}
return ret, nil
}
// -- Result of a get request.
type GetResult struct {
Index string `json:"_index"`
Type string `json:"_type"`
Id string `json:"_id"`
Version int64 `json:"_version,omitempty"`
Source *json.RawMessage `json:"_source,omitempty"`
Found bool `json:"found,omitempty"`
Fields []string `json:"fields,omitempty"`
Error string `json:"error,omitempty"` // used only in MultiGet
}