This repository has been archived by the owner on Sep 9, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathREADME.Rmd
356 lines (246 loc) · 9.31 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
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
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
solrium
=======
```{r echo=FALSE}
knitr::opts_chunk$set(
comment = "#>",
collapse = TRUE,
warning = FALSE,
message = FALSE
)
```
[![Project Status: Active – The project has reached a stable, usable state and is being actively developed.](https://www.repostatus.org/badges/latest/active.svg)](https://www.repostatus.org/#active)
[![cran checks](https://cranchecks.info/badges/worst/solrium)](https://cranchecks.info/pkgs/solrium)
[![rstudio mirror downloads](https://cranlogs.r-pkg.org/badges/solrium?color=2ED968)](https://github.com/r-hub/cranlogs.app)
[![cran version](https://www.r-pkg.org/badges/version/solrium)](https://cran.r-project.org/package=solrium)
**A general purpose R interface to [Solr](https://solr.apache.org/)**
Development is now following Solr v7 and greater - which introduced many changes, which means many functions here may not work with your Solr installation older than v7.
Be aware that currently some functions will only work in certain Solr modes, e.g, `collection_create()` won't work when you are not in Solrcloud mode. But, you should get an error message stating that you aren't.
Currently developing against Solr `v8.2.0`
## Package API and ways of using the package
The first thing to look at is `SolrClient` to instantiate a client connection
to your Solr instance. `ping` and `schema` are helpful functions to look
at after instantiating your client.
There are two ways to use `solrium`:
1. Call functions on the `SolrClient` object
2. Pass the `SolrClient` object to functions
For example, if we instantiate a client like `conn <- SolrClient$new()`, then
to use the first way we can do `conn$search(...)`, and the second way by doing
`solr_search(conn, ...)`. These two ways of using the package hopefully
make the package more user friendly for more people, those that prefer a more
object oriented approach, and those that prefer more of a functional approach.
**Collections**
Functions that start with `collection` work with Solr collections when in
cloud mode. Note that these functions won't work when in Solr standard mode
**Cores**
Functions that start with `core` work with Solr cores when in standard Solr
mode. Note that these functions won't work when in Solr cloud mode
**Documents**
The following functions work with documents in Solr
```
#> - add
#> - delete_by_id
#> - delete_by_query
#> - update_atomic_json
#> - update_atomic_xml
#> - update_csv
#> - update_json
#> - update_xml
```
**Search**
Search functions, including `solr_parse` for parsing results from different
functions appropriately
```
#> - solr_all
#> - solr_facet
#> - solr_get
#> - solr_group
#> - solr_highlight
#> - solr_mlt
#> - solr_parse
#> - solr_search
#> - solr_stats
```
## Install
Stable version from CRAN
```{r eval=FALSE}
install.packages("solrium")
```
Or development version from GitHub
```{r eval=FALSE}
remotes::install_github("ropensci/solrium")
```
```{r}
library("solrium")
```
## Setup
Use `SolrClient$new()` to initialize your connection. These examples use a remote Solr server, but work on any local Solr server.
```{r}
(cli <- SolrClient$new(host = "api.plos.org", path = "search", port = NULL))
```
You can also set whether you want simple or detailed error messages (via `errors`), and whether you want URLs used in each function call or not (via `verbose`), and your proxy settings (via `proxy`) if needed. For example:
```{r eval=FALSE}
SolrClient$new(errors = "complete")
```
Your settings are printed in the print method for the connection object
```{r}
cli
```
For local Solr server setup:
```
bin/solr start -e cloud -noprompt
bin/post -c gettingstarted example/exampledocs/*.xml
```
## Search
```{r}
(res <- cli$search(params = list(q='*:*', rows=2, fl='id')))
```
And you can get search metadata from the attributes:
```{r}
attributes(res)
```
### Search grouped data
Most recent publication by journal
```{r}
cli$group(params = list(q='*:*', group.field='journal', rows=5, group.limit=1,
group.sort='publication_date desc',
fl='publication_date, score'))
```
First publication by journal
```{r}
cli$group(params = list(q = '*:*', group.field = 'journal', group.limit = 1,
group.sort = 'publication_date asc',
fl = c('publication_date', 'score'),
fq = "publication_date:[1900-01-01T00:00:00Z TO *]"))
```
Search group query : Last 3 publications of 2013.
```{r}
gq <- 'publication_date:[2013-01-01T00:00:00Z TO 2013-12-31T00:00:00Z]'
cli$group(
params = list(q='*:*', group.query = gq,
group.limit = 3, group.sort = 'publication_date desc',
fl = 'publication_date'))
```
Search group with format simple
```{r}
cli$group(params = list(q='*:*', group.field='journal', rows=5,
group.limit=3, group.sort='publication_date desc',
group.format='simple', fl='journal, publication_date'))
```
### Facet
```{r}
cli$facet(params = list(q='*:*', facet.field='journal', facet.query=c('cell', 'bird')))
```
### Highlight
```{r}
cli$highlight(params = list(q='alcohol', hl.fl = 'abstract', rows=2))
```
### Stats
```{r}
out <- cli$stats(params = list(q='ecology', stats.field=c('counter_total_all','alm_twitterCount'), stats.facet='journal'))
```
```{r}
out$data
```
### More like this
`solr_mlt` is a function to return similar documents to the one
```{r}
out <- cli$mlt(params = list(q='title:"ecology" AND body:"cell"', mlt.fl='title', mlt.mindf=1, mlt.mintf=1, fl='counter_total_all', rows=5))
```
```{r}
out$docs
```
```{r}
out$mlt
```
### Parsing
`solr_parse` is a general purpose parser function with extension methods `solr_parse.sr_search`, `solr_parse.sr_facet`, and `solr_parse.sr_high`, for parsing `solr_search`, `solr_facet`, and `solr_highlight` function output, respectively. `solr_parse` is used internally within those three functions (`solr_search`, `solr_facet`, `solr_highlight`) to do parsing. You can optionally get back raw `json` or `xml` from `solr_search`, `solr_facet`, and `solr_highlight` setting parameter `raw=TRUE`, and then parsing after the fact with `solr_parse`. All you need to know is `solr_parse` can parse
For example:
```{r}
(out <- cli$highlight(params = list(q='alcohol', hl.fl = 'abstract', rows=2),
raw=TRUE))
```
Then parse
```{r}
solr_parse(out, 'df')
```
### Progress bars
only supported in the core search methods: `search`, `facet`, `group`, `mlt`, `stats`, `high`, `all`
```{r eval = FALSE}
library(httr)
invisible(cli$search(params = list(q='*:*', rows=100, fl='id'), progress = httr::progress()))
|==============================================| 100%
```
### Advanced: Function Queries
Function Queries allow you to query on actual numeric fields in the SOLR database, and do addition, multiplication, etc on one or many fields to sort results. For example, here, we search on the product of counter_total_all and alm_twitterCount, using a new temporary field "_val_"
```{r}
cli$search(params = list(q='_val_:"product(counter_total_all,alm_twitterCount)"',
rows=5, fl='id,title', fq='doc_type:full'))
```
Here, we search for the papers with the most citations
```{r}
cli$search(params = list(q='_val_:"max(counter_total_all)"',
rows=5, fl='id,counter_total_all', fq='doc_type:full'))
```
Or with the most tweets
```{r}
cli$search(params = list(q='_val_:"max(alm_twitterCount)"',
rows=5, fl='id,alm_twitterCount', fq='doc_type:full'))
```
### Using specific data sources
__USGS BISON service__
The occurrences service
```{r}
conn <- SolrClient$new(scheme = "https", host = "bison.usgs.gov", path = "solr/occurrences/select", port = NULL)
conn$search(params = list(q = '*:*', fl = c('decimalLatitude','decimalLongitude','scientificName'), rows = 2))
```
The species names service
```{r}
conn <- SolrClient$new(scheme = "https", host = "bison.usgs.gov", path = "solr/scientificName/select", port = NULL)
conn$search(params = list(q = '*:*'))
```
__PLOS Search API__
Most of the examples above use the PLOS search API... :)
## Solr server management
This isn't as complete as searching functions show above, but we're getting there.
### Cores
```{r eval=FALSE}
conn <- SolrClient$new()
```
Many functions, e.g.:
* `core_create()`
* `core_rename()`
* `core_status()`
* ...
Create a core
```{r eval=FALSE}
conn$core_create(name = "foo_bar")
```
### Collections
Many functions, e.g.:
* `collection_create()`
* `collection_list()`
* `collection_addrole()`
* ...
Create a collection
```{r eval=FALSE}
conn$collection_create(name = "hello_world")
```
### Add documents
Add documents, supports adding from files (json, xml, or csv format), and from R objects (including `data.frame` and `list` types so far)
```{r eval=FALSE}
df <- data.frame(id = c(67, 68), price = c(1000, 500000000))
conn$add(df, name = "books")
```
Delete documents, by id
```{r eval=FALSE}
conn$delete_by_id(name = "books", ids = c(3, 4))
```
Or by query
```{r eval=FALSE}
conn$delete_by_query(name = "books", query = "manu:bank")
```
## Meta
* Please [report any issues or bugs](https://github.com/ropensci/solrium/issues)
* License: MIT
* Get citation information for `solrium` in R doing `citation(package = 'solrium')`
* Please note that this package is released with a [Contributor Code of Conduct](https://ropensci.org/code-of-conduct/). By contributing to this project, you agree to abide by its terms.