Skip to content

Commit

Permalink
add all possible API search queries
Browse files Browse the repository at this point in the history
  • Loading branch information
ernestguevarra committed Feb 11, 2024
1 parent c6c7fad commit b020da6
Show file tree
Hide file tree
Showing 7 changed files with 123 additions and 23 deletions.
3 changes: 2 additions & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@ Imports:
Suggests:
covr,
httpuv,
spelling
spelling,
tibble
Encoding: UTF-8
LazyData: true
Language: en-GB
Expand Down
87 changes: 74 additions & 13 deletions R/icd_search.R
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,16 @@
#' Search the foundation component of the ICD-11
#'
#' @param base_url The base URL of the API. Default uses the WHO API server at
#' https://id.who.int/icd/entity
#' @param client The OAuth2 client produced through a call to `icd_oauth_client()`
#' https://id.who.int. If you are using a locally deployed server or hosting
#' your own ICD API server, you should specify the URL of your instance here.
#' @param client The OAuth2 client produced through a call to `icd_oauth_client()`.
#' @param q String. Text to be searched. Having the character `%` at the end will
#' be regarded as a wild card for that word
#' be regarded as a wild card for that word.
#' @param subtree A string or vector of strings of URIs. If provided, the
#' search will be performed on the entities provided and their descendants.
#' @param chapter A string or vector of strings of chapter codes
#' eg: c("01", "02") When provided, the search will be performed only on
#' these chapters.
#' @param flexisearch Logical. Default is FALSE. Changes the search mode to
#' flexible search. In the regular search mode, the Coding Tool will only give
#' you results that contain all of the words that you've used in your search.
Expand All @@ -15,16 +21,24 @@
#' that are typed. It would still try to find the best matching phrase but
#' there may be words in your search that are not matched at all. It is
#' recommended to use flexible search only when regular search does not
#' provide a result
#' provide a result.
#' @param flat Logical. Default is FALSE. If set to true the search result
#' entities are provided in a nested data structure representing the
#' ICD-11 hierarchy. Otherwise they are listed as flat list of matches
#' ICD-11 hierarchy. Otherwise they are listed as flat list of matches.
#' @param properties A string or a vector of strings for the properties to be
#' searched. By default the system searches, *"Title"*, *"Synonyms"*, and
#' *"FullySpecifiedName"*. The valid values that could be used are: *Title"*,
#' *"Synonym"*, *"NarrowerTerm"*, *"FullySpecifiedName"*, *"Definition"*, and
#' *"Exclusion"*.
#' @param release A string specifying the release version of the Foundation to
#' search from. If not specified, defaults to the latest release version. See
#' the available versions with `icd_versions`.
#' @param highlight Logical. Default is FALSE. If set to FALSE the search result
#' highlighting is turned off and the results don't contain special tags for
#' highlighting where the results are found within the text
#' highlighting where the results are found within the text.
#' @param api_version Version of the API. Possible values are `v1` or `v2`.
#' For example, if you provide value v2, the API will respond in the format of
#' the version 2 of the API. Default is `v2`
#' the version 2 of the API. Default is `v2`.
#' @param language ICD-API is multi-lingual. By changing this header, you may
#' make the API respond in different languages. Languages will be available as
#' the translations of ICD-11 completes. The values are language codes such as
Expand All @@ -36,36 +50,83 @@
#' @examples
#' icd_search_foundation(q = "cholera")
#'
#' @rdname icd_search
#' @export
#'

icd_search_foundation <- function(base_url = "https://id.who.int/icd",
icd_search_foundation <- function(base_url = "https://id.who.int",
client = icd_oauth_client(),
q = NULL,
q,
subtree = NULL,
chapter = NULL,
flexisearch = FALSE,
flat = TRUE,
properties = NULL,
release = NULL,
highlight = FALSE,
api_version = c("v2", "v1"),
language = "en") {
## Get API version to use ----
api_version <- match.arg(api_version)

httr2::request(file.path(base_url, "entity/search")) |>
## Make base request ----
req <- httr2::request(file.path(base_url, "icd/entity/search")) |>
httr2::req_url_query(q = q)

## Add query components ----

### Subtrees filter ----
if (!is.null(subtree)) {
req <- req |>
httr2::req_url_query(subtreesFilter = paste(subtree, collapse = ","))
}

### Chapters filter ----
if (!is.null(chapter)) {
req <- req |>
httr2::req_url_query(chapterFilter = paste(chapter, collapse = ","))
}

### Flexi search and flatResults component ----
req <- req |>
httr2::req_url_query(
q = q,
useFlexisearch = ifelse(flexisearch, "true", "false"),
flatResults = ifelse(flat, "true", "false"),
flatResults = ifelse(flat, "true", "false")
)

### Properties ----
if (!is.null(properties)) {
req <- req |>
httr2::req_url_query(
propertiesToBeSearched = paste(properties, collapse = ",")
)
}

### Release ID ----
if (!is.null(release)) {
req <- req |>
httr2::req_url_query(releaseId = release)
}

### Highlighting ----
req <- req |>
httr2::req_url_query(
highlightingEnabled = ifelse(highlight, "true", "false")
) |>
)

## Add headers ----
req <- req |>
httr2::req_headers(
Accept = "application/json",
"API-Version" = api_version,
"Accept-Language" = language
) |>
## Authenticate ----
httr2::req_oauth_client_credentials(
client = client,
scope = "icdapi_access"
) |>
## Perform request ----
httr2::req_perform()
}

Expand Down
6 changes: 6 additions & 0 deletions R/utils.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
#'
#'
#'
#'
#'
#'
6 changes: 6 additions & 0 deletions data-raw/icd_linearization_tables.R
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,9 @@ icd11_linearization_mms$Version <- header_values |>
(\(x) x[2:3])() |>
paste(collapse = ":")

icd11_linearization_mms <- icd11_linearization_mms |>
tibble::tibble()

usethis::use_data(icd11_linearization_mms, overwrite = TRUE, compress = "xz")

### Simple Table MMS ----
Expand All @@ -60,4 +63,7 @@ icd11_simple_table_mms <- read_xlsx(
sheet = 1, data_only = TRUE
)

icd11_simple_table_mms |>
tibble::tibble()

usethis::use_data(icd11_simple_table_mms, overwrite = TRUE, compress = "xz")
Binary file modified data/icd11_linearization_mms.rda
Binary file not shown.
4 changes: 4 additions & 0 deletions inst/WORDLIST
Original file line number Diff line number Diff line change
Expand Up @@ -6,21 +6,25 @@ ClassKind
CodeFactor
Codecov
DepthInKind
FullySpecifiedName
GroupingX
ICF
IsLeaf
IsResidual
Lifecycle
LinearizationMiniOutput
NarrowerTerm
OAuth
ORCID
PrimaryLocation
RC
SimpleTabulation
SupportedClassifications
URIs
WIP
api
depthinkind
eg
es
etc
fileName
Expand Down
40 changes: 31 additions & 9 deletions man/icd_search_foundation.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit b020da6

Please sign in to comment.