knitr::opts_chunk$set(
  comment = "#>",
  warning = FALSE,
  message = FALSE
)

Document management

Installation

Stable version from CRAN

install.packages("solrium")

Or the development version from GitHub

install.packages("devtools")
devtools::install_github("ropensci/solrium")

Load

library("solrium")

Initialize connection. By default, you connect to http://localhost:8983

(conn <- SolrClient$new())

Create documents from R objects

For now, only lists and data.frame's supported.

data.frame

df <- data.frame(id = c(67, 68), price = c(1000, 500000000))
conn$add(df, "books")

list

conn$delete_by_id(1:2, "books")
ss <- list(list(id = 1, price = 100), list(id = 2, price = 500))
conn$add(ss, "books")

Delete documents

By id

Add some documents first

conn$delete_by_id(1:3, "gettingstarted")
docs <- list(list(id = 1, price = 100, name = "brown"),
             list(id = 2, price = 500, name = "blue"),
             list(id = 3, price = 2000L, name = "pink"))
conn$add(docs, "gettingstarted")

And the documents are now in your Solr database

conn$search(name = "gettingstarted", params = list(q = "*:*", rows = 3))

Now delete those documents just added

conn$delete_by_id(ids = c(1, 2, 3), "gettingstarted")

And now they are gone

conn$search("gettingstarted", params = list(q = "*:*", rows = 4))

By query

Add some documents first

conn$add(docs, "gettingstarted")

And the documents are now in your Solr database

conn$search("gettingstarted", params = list(q = "*:*", rows = 5))

Now delete those documents just added

conn$delete_by_query(query = "(name:blue OR name:pink)", "gettingstarted")

And now they are gone

conn$search("gettingstarted", params = list(q = "*:*", rows = 5))

Update documents from files

This approach is best if you have many different things you want to do at once, e.g., delete and add files and set any additional options. The functions are:

There are separate functions for each of the data types as they take slightly different parameters - and to make it more clear that those are the three input options for data types.

JSON

file <- system.file("examples", "books.json", package = "solrium")
conn$update_json(file, "books")

Add and delete in the same file

Add a document first, that we can later delete

ss <- list(list(id = 456, name = "cat"))
conn$add(ss, "books")

Now add a new document, and delete the one we just made

file <- system.file("examples", "add_delete.xml", package = "solrium")
cat(readLines(file), sep = "\n")
conn$update_xml(file, "books")

Notes

Note that update_xml() and update_json() have exactly the same parameters, but simply use different data input formats. update_csv() is different in that you can't provide document or field level boosts or other modifications. In addition update_csv() can accept not just csv, but tsv and other types of separators.



ropensci/solrium documentation built on Sept. 12, 2022, 3:01 p.m.