This repository has been archived by the owner on Oct 21, 2024. It is now read-only.
forked from olivere/elastic
-
Notifications
You must be signed in to change notification settings - Fork 4
/
index.go
217 lines (190 loc) · 4.39 KB
/
index.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
205
206
207
208
209
210
211
212
213
214
215
216
217
// Copyright 2012-2015 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/url"
"gopkg.in/olivere/elastic.v2/uritemplates"
)
// IndexResult is the result of indexing a document in Elasticsearch.
type IndexResult struct {
Index string `json:"_index"`
Type string `json:"_type"`
Id string `json:"_id"`
Version int `json:"_version"`
Created bool `json:"created"`
}
// IndexService adds documents to Elasticsearch.
type IndexService struct {
client *Client
index string
_type string
id string
routing string
parent string
opType string
refresh *bool
version *int64
versionType string
timestamp string
ttl string
timeout string
bodyString string
bodyJson interface{}
pretty bool
}
func NewIndexService(client *Client) *IndexService {
builder := &IndexService{
client: client,
}
return builder
}
func (b *IndexService) Index(name string) *IndexService {
b.index = name
return b
}
func (b *IndexService) Type(_type string) *IndexService {
b._type = _type
return b
}
func (b *IndexService) Id(id string) *IndexService {
b.id = id
return b
}
func (b *IndexService) Routing(routing string) *IndexService {
b.routing = routing
return b
}
func (b *IndexService) Parent(parent string) *IndexService {
b.parent = parent
return b
}
// OpType is either "create" or "index" (the default).
func (b *IndexService) OpType(opType string) *IndexService {
b.opType = opType
return b
}
func (b *IndexService) Refresh(refresh bool) *IndexService {
b.refresh = &refresh
return b
}
func (b *IndexService) Version(version int64) *IndexService {
b.version = &version
return b
}
// VersionType is either "internal" (default), "external",
// "external_gt", "external_gte", or "force".
// See http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/docs-index_.html#_version_types
// for details.
func (b *IndexService) VersionType(versionType string) *IndexService {
b.versionType = versionType
return b
}
func (b *IndexService) Timestamp(timestamp string) *IndexService {
b.timestamp = timestamp
return b
}
func (b *IndexService) TTL(ttl string) *IndexService {
b.ttl = ttl
return b
}
func (b *IndexService) Timeout(timeout string) *IndexService {
b.timeout = timeout
return b
}
func (b *IndexService) BodyString(body string) *IndexService {
b.bodyString = body
return b
}
func (b *IndexService) BodyJson(json interface{}) *IndexService {
b.bodyJson = json
return b
}
func (b *IndexService) Pretty(pretty bool) *IndexService {
b.pretty = pretty
return b
}
func (b *IndexService) Do() (*IndexResult, error) {
// Build url
var path, method string
if b.id != "" {
// Create document with manual id
method = "PUT"
path = "/{index}/{type}/{id}"
} else {
// Automatic ID generation
// See: http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/docs-index_.html#index-creation
method = "POST"
path = "/{index}/{type}/"
}
path, err := uritemplates.Expand(path, map[string]string{
"index": b.index,
"type": b._type,
"id": b.id,
})
if err != nil {
return nil, err
}
// Parameters
params := make(url.Values)
if b.pretty {
params.Set("pretty", "true")
}
if b.routing != "" {
params.Set("routing", b.routing)
}
if b.parent != "" {
params.Set("parent", b.parent)
}
if b.opType != "" {
params.Set("op_type", b.opType)
}
if b.refresh != nil && *b.refresh {
params.Set("refresh", "true")
}
if b.version != nil {
params.Set("version", fmt.Sprintf("%d", *b.version))
}
if b.versionType != "" {
params.Set("version_type", b.versionType)
}
if b.timestamp != "" {
params.Set("timestamp", b.timestamp)
}
if b.ttl != "" {
params.Set("ttl", b.ttl)
}
if b.timeout != "" {
params.Set("timeout", b.timeout)
}
/*
routing string
parent string
opType string
refresh *bool
version *int64
versionType string
timestamp string
ttl string
*/
// Body
var body interface{}
if b.bodyJson != nil {
body = b.bodyJson
} else {
body = b.bodyString
}
// Get response
res, err := b.client.PerformRequest(method, path, params, body)
if err != nil {
return nil, err
}
// Return result
ret := new(IndexResult)
if err := json.Unmarshal(res.Body, ret); err != nil {
return nil, err
}
return ret, nil
}