Skip to content
This repository was archived by the owner on Sep 9, 2022. It is now read-only.

Commit 2fa097e

Browse files
committed
fix #117 add support for json request api
1 parent d5677f7 commit 2fa097e

File tree

6 files changed

+134
-0
lines changed

6 files changed

+134
-0
lines changed

NAMESPACE

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@ export(solr_facet)
6666
export(solr_get)
6767
export(solr_group)
6868
export(solr_highlight)
69+
export(solr_json_request)
6970
export(solr_mlt)
7071
export(solr_optimize)
7172
export(solr_parse)

R/SolrClient.R

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,8 @@
111111
#' * `all(name = NULL, params = NULL, body = NULL, callopts=list(),
112112
#' raw=FALSE, parsetype='df', concat=',', optimizeMaxRows = TRUE,
113113
#' minOptimizedRows = 50000L, ...)`
114+
#' * `json_request(name = NULL, body = NULL, callopts=list(),
115+
#' progress = NULL)`
114116
#' * `get(ids, name, fl = NULL, wt = 'json', raw = FALSE, ...)`
115117
#' * `add(x, name, commit = TRUE, commit_within = NULL, overwrite = TRUE,
116118
#' boost = NULL, wt = 'json', raw = FALSE, ...)`
@@ -849,6 +851,19 @@ SolrClient <- R6::R6Class(
849851
},
850852

851853

854+
# JSON request API
855+
json_request = function(name = NULL, body = NULL, callopts=list(),
856+
progress = NULL) {
857+
858+
stopifnot(inherits(body, "list") || is.character(body))
859+
solr_POST_body(
860+
self$make_url(),
861+
if (!is.null(name)) url_handle(name) else self$path,
862+
body, list(), ctype_json(), callopts, self$proxy,
863+
progress = progress)
864+
},
865+
866+
852867
# documents methods
853868
get = function(ids, name, fl = NULL, wt = 'json', raw = FALSE, ...) {
854869
if (!is.null(fl)) fl <- paste0(fl, collapse = ",")

R/solr_json_request.R

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
#' @title Solr json request
2+
#'
3+
#' @description search using the JSON request API
4+
#'
5+
#' @export
6+
#' @param conn A solrium connection object, see [SolrClient]
7+
#' @param name Name of a collection or core. Or leave as \code{NULL} if not needed.
8+
#' @param body (list) a named list, or a valid JSON character string
9+
#' @param callopts Call options passed on to [crul::HttpClient]
10+
#' @param progress a function with logic for printing a progress
11+
#' bar for an HTTP request, ultimately passed down to \pkg{curl}. only supports
12+
#' \code{httr::progress} for now. See the README for an example.
13+
#'
14+
#' @return JSON character string
15+
#' @references See <https://lucene.apache.org/solr/guide/7_6/json-request-api.html>
16+
#' for more information.
17+
#' @note SOLR v7.1 was first version to support this. See
18+
#' <https://issues.apache.org/jira/browse/SOLR-11244>
19+
#' @note POST request only, no GET request available
20+
#' @examples \dontrun{
21+
#' # Connect to a local Solr instance
22+
#' (conn <- SolrClient$new())
23+
#'
24+
#' ## body as JSON
25+
#' a <- conn$json_request("gettingstarted", body = '{"query":"*:*"}')
26+
#' jsonlite::fromJSON(a)
27+
#' ## body as named list
28+
#' b <- conn$json_request("gettingstarted", body = list(query = "*:*"))
29+
#' jsonlite::fromJSON(b)
30+
#'
31+
#' ## body as JSON
32+
#' a <- solr_json_request(conn, "gettingstarted", body = '{"query":"*:*"}')
33+
#' jsonlite::fromJSON(a)
34+
#' ## body as named list
35+
#' b <- solr_json_request(conn, "gettingstarted", body = list(query = "*:*"))
36+
#' jsonlite::fromJSON(b)
37+
#' }
38+
solr_json_request <- function(conn, name = NULL, body = NULL, callopts = list(),
39+
progress = NULL) {
40+
41+
conn$json_request(name = name, body = body, callopts = callopts,
42+
progress = progress)
43+
}

man/SolrClient.Rd

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

man/solr_json_request.Rd

Lines changed: 56 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
context("solr_json_request")
2+
3+
test_that("solr_json_request works", {
4+
skip_on_cran()
5+
6+
if (!collection_exists(conn, "books")) {
7+
collection_create(conn, name = "books")
8+
}
9+
10+
a <- conn$json_request("gettingstarted", body = '{"query":"*:*"}')
11+
alist <- jsonlite::fromJSON(a)
12+
13+
expect_is(a, "character")
14+
expect_match(a, "responseHeader")
15+
expect_match(a, "title")
16+
expect_named(alist, c("responseHeader", "response"))
17+
expect_is(alist$response$docs, "data.frame")
18+
})

0 commit comments

Comments
 (0)