Skip to content

Commit d00a175

Browse files
committed
sort change to support multiple sort keys
1 parent 723abdd commit d00a175

File tree

1 file changed

+28
-0
lines changed

1 file changed

+28
-0
lines changed

R/search-pv.R

+28
Original file line numberDiff line numberDiff line change
@@ -271,7 +271,35 @@ search_pv <- function(query,
271271
result <- process_resp(result)
272272
if (!all_pages) return(result)
273273

274+
# Here we ignore the user's sort and instead have the API sort by the primary
275+
# key for the requested endpoint. This simplifies the paging's after parameter.
276+
# If we call the API with more than a primary sort, the after parameter would
277+
# have to be an array of all the sort fields' values.
278+
# After we've retrieved all the data we'll sort using the user's sort
279+
280+
# Doing this also protects users from needing to know the peculiarities
281+
# of the API's sort. Example: if a user requests a sort of
282+
# {"patent_date":"asc"}, on paging the after parameter may skip
283+
# to the next issue date before having retured all the data for the last
284+
# patent_date in the previous request - depending on where the
285+
# patent_dates change relative to the API's page breaks.
286+
# (Say the last patent in a retrieved page is the first patent
287+
# of a particular date, we wouldn't want the after parameter to
288+
# to begin the next page of data after this date.)
289+
290+
# We also need to insure we'll have the value of the primary sort field.
291+
# The API throws an error if the sort field is not present in the fields list
292+
# **Should we remember if we added the primary_sort_key and remove it before
293+
# returning data to the user?
294+
primary_sort_key <- get_default_sort(endpoint)
295+
296+
if (!names(primary_sort_key) %in% fields) fields <- c(fields, names(primary_sort_key))
297+
298+
arg_list <- to_arglist(fields, page, per_page, primary_sort_key)
274299
full_data <- request_apply(result, method, query, base_url, arg_list, api_key, ...)
300+
301+
# apply the user's sort
302+
data.table::setorderv(full_data, names(sort), ifelse(as.vector(sort)=="asc", 1, -1))
275303
result$data[[1]] <- full_data
276304

277305
result

0 commit comments

Comments
 (0)