This repository has been archived by the owner on May 10, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 6
/
README.Rmd
190 lines (145 loc) · 5.1 KB
/
README.Rmd
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
elasticdsl
=======
```{r echo=FALSE}
library("knitr")
hook_output <- knitr::knit_hooks$get("output")
knitr::knit_hooks$set(output = function(x, options) {
lines <- options$output.lines
if (is.null(lines)) {
return(hook_output(x, options)) # pass to default hook
}
x <- unlist(strsplit(x, "\n"))
more <- "..."
if (length(lines)==1) { # first n lines
if (length(x) > lines) {
# truncate the output, but add ....
x <- c(head(x, lines), more)
}
} else {
x <- c(if (abs(lines[1])>1) more else NULL,
x[lines],
if (length(x)>lines[abs(length(lines))]) more else NULL
)
}
# paste these lines together
x <- paste(c(x, ""), collapse = "\n")
hook_output(x, options)
})
knitr::opts_chunk$set(
comment = "#>",
collapse = TRUE,
warning = FALSE,
message = FALSE
)
```
[![Project Status: Suspended – Initial development has started, but there has not yet been a stable, usable release; work has been stopped for the time being but the author(s) intend on resuming work.](https://www.repostatus.org/badges/latest/suspended.svg)](https://www.repostatus.org/#suspended)
[![Build Status](https://api.travis-ci.org/ropensci/elasticdsl.svg)](https://travis-ci.org/ropensci/elasticdsl)
[![Build status](https://ci.appveyor.com/api/projects/status/r810moreouuq18ox?svg=true)](https://ci.appveyor.com/project/sckott/elasticdsl)
[![codecov.io](https://codecov.io/github/ropensci/elasticdsl/coverage.svg?branch=master)](https://codecov.io/github/ropensci/elasticdsl?branch=master)
**An R DSL for [Elasticsearch](http://elasticsearch.org)**
## Elasticsearch info
* [Elasticsearch home page](http://elasticsearch.org)
* [API docs](http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/index.html)
* This client is being developed under `v1.4` of Elasticsearch.
## Security
You're fine running ES locally on your machine, but be careful just throwing up ES on a server with a public IP address - make sure to think about security.
* [Shield](https://www.elastic.co/products/shield) - This is a paid product provided by Elastic - so probably only applicable to enterprise users
* DIY security - there are a variety of techniques for securing your Elasticsearch. A number of resources are collected in a [blog post](http://recology.info/2015/02/secure-elasticsearch/) - tools include putting your ES behind something like Nginx, putting basic auth on top of it, using https, etc.
## Install elasticdsl
```{r eval=FALSE}
install.packages("devtools")
devtools::install_github("ropensci/elasticdsl")
```
```{r}
library('elasticdsl')
```
## Setup
Instructions for installing, upgrading, starting Elasticsearch, and loading example data at [ropensci/elastic](https://github.com/ropensci/elastic#install-elasticsearch)
## Initialization
The function `elastic::connect()` is used before doing anything else to set the connection details to your remote or local elasticdslsearch store. The details created by `connect()` are written to your options for the current session, and are used by `elasticdsl` functions.
```{r}
elastic::connect(es_port = 9200)
```
## Set the index to use
```{r output.lines = 1:10}
index("shakespeare")
```
## Print query as pretty json
```{r eval=FALSE}
index("shakespeare") %>%
filter() %>%
ids(c(1, 2, 150)) %>%
explain() # doesn't exist yet
```
## Execute query
```{r}
res <- index("shakespeare") %>%
filter() %>%
ids(c(1, 2)) %>%
exec()
```
## n() to get number of results
```{r}
index("shakespeare") %>%
ids(c(1, 2)) %>%
exec() %>%
n()
```
## Request size
```{r}
index("shakespeare") %>%
filter() %>%
prefix(speaker = "we") %>%
size(2) %>%
fields(play_name) %>%
exec() %>%
n()
```
## Request certain fields
```{r}
s <- index("shakespeare") %>%
filter() %>%
prefix(speaker = "we") %>%
size(2)
```
```{r output.lines = 1:10}
s %>% fields(play_name) %>% exec() %>% .$hits %>% .$hits
```
```{r output.lines = 1:10}
s %>% fields(play_name, text_entry) %>% exec() %>% .$hits %>% .$hits
```
```{r output.lines = 1:10}
s %>% fields(play_name, text_entry, line_id) %>% exec() %>% .$hits %>% .$hits
```
## Filters vs. queries
Filters are boolean queries and are much more computationally efficient than queries.
### Filters
`prefix` filter
```{r}
index("shakespeare") %>%
filter() %>%
prefix(speaker = "we") %>%
exec() %>%
n()
```
`ids` filter
```{r}
index("shakespeare") %>%
filter() %>%
ids(c(1, 2, 150)) %>%
exec() %>%
n()
```
### Queries
`geoshape` query (filters have a much larger range of geo queries)
```{r}
index("geoshape") %>%
geoshape(field = "location", type = "envelope", coordinates = list(c(-30, 50), c(30, 0))) %>%
n()
```
## Meta
* Please [report any issues or bugs](https://github.com/ropensci/elasticdsl/issues)
* License: MIT
* Get citation information for `elasticdsl` in R doing `citation(package = 'elasticdsl')`
* Please note that this project is released with a [Contributor Code of Conduct](CONDUCT.md). By participating in this project you agree to abide by its terms.
[![rofooter](http://ropensci.org/public_images/github_footer.png)](http://ropensci.org)