-
Notifications
You must be signed in to change notification settings - Fork 58
/
ingest.R
190 lines (183 loc) · 5.16 KB
/
ingest.R
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
#' Ingest API operations
#'
#' @references
#' <https://www.elastic.co/guide/en/elasticsearch/reference/current/ingest-apis.html>,
#' <https://www.elastic.co/guide/en/elasticsearch/plugins/current/using-ingest-attachment.html>
#' @name ingest
#'
#' @param conn an Elasticsearch connection object, see [connect()]
#' @param id (character) one or more pipeline id's. with delete, you can use
#' a wildcard match
#' @param body body describing pipeline, see examples and Elasticsearch docs
#' @param filter_path (character) fields to return. deafults to all if not given
#' @param index (character) an index. only used in `pipeline_attachment`
#' @param type (character) a type. only used in `pipeline_attachment`. by default
#' ths is set to `NULL` - optional in ES <= v6.3; not allowed in ES >= v6.4
#' @param pipeline (character) a pipeline name. only used in `pipeline_attachment`
#' @param ... Curl args passed on to [crul::verb-POST], [crul::verb-GET],
#' [crul::verb-PUT], or [crul::verb-DELETE]
#'
#' @return a named list
#'
#' @details ingest/pipeline functions available in Elasticsearch v5 and
#' greater
#'
#' @section Attachments:
#' See https://www.elastic.co/guide/en/elasticsearch/plugins/current/ingest-attachment.html
#' You need to install the attachment processor plugin to be able to use
#' attachments in pipelines
#'
#' @examples \dontrun{
#' # connection setup
#' (x <- connect())
#'
#' # create
#' body1 <- '{
#' "description" : "do a thing",
#' "version" : 123,
#' "processors" : [
#' {
#' "set" : {
#' "field": "foo",
#' "value": "bar"
#' }
#' }
#' ]
#' }'
#' body2 <- '{
#' "description" : "do another thing",
#' "processors" : [
#' {
#' "set" : {
#' "field": "stuff",
#' "value": "things"
#' }
#' }
#' ]
#' }'
#' pipeline_create(x, id = 'foo', body = body1)
#' pipeline_create(x, id = 'bar', body = body2)
#'
#' # get
#' pipeline_get(x, id = 'foo')
#' pipeline_get(x, id = 'bar')
#' pipeline_get(x, id = 'foo', filter_path = "*.version")
#' pipeline_get(x, id = c('foo', 'bar')) # get >1
#'
#' # delete
#' pipeline_delete(x, id = 'foo')
#'
#' # simulate
#' ## with pipeline included
#' body <- '{
#' "pipeline" : {
#' "description" : "do another thing",
#' "processors" : [
#' {
#' "set" : {
#' "field": "stuff",
#' "value": "things"
#' }
#' }
#' ]
#' },
#' "docs" : [
#' { "_source": {"foo": "bar"} },
#' { "_source": {"foo": "world"} }
#' ]
#' }'
#' pipeline_simulate(x, body)
#'
#' ## referencing existing pipeline
#' body <- '{
#' "docs" : [
#' { "_source": {"foo": "bar"} },
#' { "_source": {"foo": "world"} }
#' ]
#' }'
#' pipeline_simulate(x, body, id = "foo")
#'
#' # attchments - Note: you need the attachment plugin for this, see above
#' body1 <- '{
#' "description" : "do a thing",
#' "version" : 123,
#' "processors" : [
#' {
#' "attachment" : {
#' "field" : "data"
#' }
#' }
#' ]
#' }'
#' pipeline_create(x, "baz", body1)
#' body_attach <- '{
#' "data": "e1xydGYxXGFuc2kNCkxvcmVtIGlwc3VtIGRvbG9yIHNpdCBhbWV0DQpccGFyIH0="
#' }'
#' if (!index_exists(x, "boomarang")) index_create(x, "boomarang")
#' docs_create(x, 'boomarang', id = 1, body = list(title = "New title"))
#' pipeline_attachment(x, "boomarang", "1", "baz", body_attach)
#' pipeline_get(x, id = 'baz')
#' }
NULL
#' @export
#' @rdname ingest
pipeline_create <- function(conn, id, body, ...) {
is_conn(conn)
pipeline_ver(conn)
url <- conn$make_url()
es_PUT(conn, file.path(url, "_ingest/pipeline", esc(id)), body = body, ...)
}
#' @export
#' @rdname ingest
pipeline_attachment <- function(conn, index, id, pipeline, body, type = NULL,
...) {
is_conn(conn)
pipeline_ver(conn)
url <- conn$make_url()
type <- if (!is.null(type)) esc(type) else "_doc"
es_PUT(conn, file.path(url, index, type, esc(id)),
body = body, args = list(pipeline = pipeline), ...)
}
#' @export
#' @rdname ingest
pipeline_get <- function(conn, id, filter_path = NULL, ...) {
is_conn(conn)
pipeline_ver(conn)
url <- file.path(conn$make_url(), "_ingest/pipeline",
paste0(esc(id), collapse = ","))
es_GET_(conn, url, ec(list(filter_path = filter_path)), ...)
}
#' @export
#' @rdname ingest
pipeline_delete <- function(conn, id, body, ...) {
is_conn(conn)
pipeline_ver(conn)
url <- file.path(conn$make_url(), "_ingest/pipeline", esc(id))
out <- conn$make_conn(url, list(), ...)$delete()
geterror(conn, out)
if (conn$warn) catch_warnings(out)
jsonlite::fromJSON(out$parse("UTF-8"))
}
#' @export
#' @rdname ingest
pipeline_simulate <- function(conn, body, id = NULL, ...) {
is_conn(conn)
pipeline_ver(conn)
url <- conn$make_url()
base <- "_ingest/pipeline"
part <- if (is.null(id)) {
file.path(base, "_simulate")
} else {
file.path(base, esc(id), "_simulate")
}
url <- file.path(url, part)
tt<-conn$make_conn(url, json_type(), ...)$post(body = body, encode = "json")
geterror(conn, tt)
if (conn$warn) catch_warnings(tt)
jsonlite::fromJSON(tt$parse("UTF-8"))
}
pipeline_ver <- function(conn) {
if (conn$es_ver() < 500) {
stop("ingest/pipeline fxns available in ES v5 and greater", call. = FALSE)
}
}