|
| 1 | +#' |
| 2 | +#' Search the foundation component of the ICD-11 |
| 3 | +#' |
| 4 | +#' @param base_url The base URL of the API. Default uses the WHO API server at |
| 5 | +#' https://id.who.int. If you are using a locally deployed server or hosting |
| 6 | +#' your own ICD API server, you should specify the URL of your instance here. |
| 7 | +#' @param client The OAuth2 client produced through a call to `icd_oauth_client()`. |
| 8 | +#' @param q String. Text to be searched. Having the character `%` at the end will |
| 9 | +#' be regarded as a wild card for that word. |
| 10 | +#' @param subtree A string or vector of strings of URIs. If provided, the |
| 11 | +#' search will be performed on the entities provided and their descendants. |
| 12 | +#' @param chapter A string or vector of strings of chapter codes |
| 13 | +#' eg: c("01", "02") When provided, the search will be performed only on |
| 14 | +#' these chapters. |
| 15 | +#' @param flexisearch Logical. Default is FALSE. Changes the search mode to |
| 16 | +#' flexible search. In the regular search mode, the Coding Tool will only give |
| 17 | +#' you results that contain all of the words that you've used in your search. |
| 18 | +#' It accepts different variants or synonyms of the words but essentially it |
| 19 | +#' searches for a result that contains all components of your search. Whereas |
| 20 | +#' in flexible search mode, the results do not have to contain all of the words |
| 21 | +#' that are typed. It would still try to find the best matching phrase but |
| 22 | +#' there may be words in your search that are not matched at all. It is |
| 23 | +#' recommended to use flexible search only when regular search does not |
| 24 | +#' provide a result. |
| 25 | +#' @param flat Logical. Default is FALSE. If set to true the search result |
| 26 | +#' entities are provided in a nested data structure representing the |
| 27 | +#' ICD-11 hierarchy. Otherwise they are listed as flat list of matches. |
| 28 | +#' @param properties A string or a vector of strings for the properties to be |
| 29 | +#' searched. By default the system searches, *"Title"*, *"Synonyms"*, and |
| 30 | +#' *"FullySpecifiedName"*. The valid values that could be used are: *Title"*, |
| 31 | +#' *"Synonym"*, *"NarrowerTerm"*, *"FullySpecifiedName"*, *"Definition"*, and |
| 32 | +#' *"Exclusion"*. |
| 33 | +#' @param release A string specifying the release version of the Foundation to |
| 34 | +#' search from. If not specified, defaults to the latest release version. See |
| 35 | +#' the available versions with `icd_versions`. |
| 36 | +#' @param highlight Logical. Default is FALSE. If set to FALSE the search result |
| 37 | +#' highlighting is turned off and the results don't contain special tags for |
| 38 | +#' highlighting where the results are found within the text. |
| 39 | +#' @param api_version Version of the API. Possible values are `v1` or `v2`. |
| 40 | +#' For example, if you provide value v2, the API will respond in the format of |
| 41 | +#' the version 2 of the API. Default is `v2`. |
| 42 | +#' @param language ICD-API is multi-lingual. By changing this header, you may |
| 43 | +#' make the API respond in different languages. Languages will be available as |
| 44 | +#' the translations of ICD-11 completes. The values are language codes such as |
| 45 | +#' en, es, zh, etc. Depending on the `release_id` specified, the available |
| 46 | +#' languages will vary. Default is English ("en"). |
| 47 | +#' |
| 48 | +#' @return An `httr2_response` object |
| 49 | +#' |
| 50 | +#' @examples |
| 51 | +#' icd_search_foundation(q = "cholera") |
| 52 | +#' |
| 53 | +#' @rdname icd_search |
| 54 | +#' @export |
| 55 | +#' |
| 56 | + |
| 57 | +icd_search_foundation <- function(base_url = "https://id.who.int", |
| 58 | + client = icd_oauth_client(), |
| 59 | + q, |
| 60 | + subtree = NULL, |
| 61 | + chapter = NULL, |
| 62 | + flexisearch = FALSE, |
| 63 | + flat = TRUE, |
| 64 | + properties = NULL, |
| 65 | + release = NULL, |
| 66 | + highlight = FALSE, |
| 67 | + api_version = c("v2", "v1"), |
| 68 | + language = "en") { |
| 69 | + ## Get API version to use ---- |
| 70 | + api_version <- match.arg(api_version) |
| 71 | + |
| 72 | + ## Make base request ---- |
| 73 | + req <- httr2::request(file.path(base_url, "icd/entity/search")) |> |
| 74 | + httr2::req_url_query(q = q) |
| 75 | + |
| 76 | + ## Add query components ---- |
| 77 | + |
| 78 | + ### Subtrees filter ---- |
| 79 | + if (!is.null(subtree)) { |
| 80 | + req <- req |> |
| 81 | + httr2::req_url_query(subtreesFilter = paste(subtree, collapse = ",")) |
| 82 | + } |
| 83 | + |
| 84 | + ### Chapters filter ---- |
| 85 | + if (!is.null(chapter)) { |
| 86 | + req <- req |> |
| 87 | + httr2::req_url_query(chapterFilter = paste(chapter, collapse = ",")) |
| 88 | + } |
| 89 | + |
| 90 | + ### Flexi search and flatResults component ---- |
| 91 | + req <- req |> |
| 92 | + httr2::req_url_query( |
| 93 | + useFlexisearch = ifelse(flexisearch, "true", "false"), |
| 94 | + flatResults = ifelse(flat, "true", "false") |
| 95 | + ) |
| 96 | + |
| 97 | + ### Properties ---- |
| 98 | + if (!is.null(properties)) { |
| 99 | + req <- req |> |
| 100 | + httr2::req_url_query( |
| 101 | + propertiesToBeSearched = paste(properties, collapse = ",") |
| 102 | + ) |
| 103 | + } |
| 104 | + |
| 105 | + ### Release ID ---- |
| 106 | + if (!is.null(release)) { |
| 107 | + req <- req |> |
| 108 | + httr2::req_url_query(releaseId = release) |
| 109 | + } |
| 110 | + |
| 111 | + ### Highlighting ---- |
| 112 | + req <- req |> |
| 113 | + httr2::req_url_query( |
| 114 | + highlightingEnabled = ifelse(highlight, "true", "false") |
| 115 | + ) |
| 116 | + |
| 117 | + ## Add headers ---- |
| 118 | + req <- req |> |
| 119 | + httr2::req_headers( |
| 120 | + Accept = "application/json", |
| 121 | + "API-Version" = api_version, |
| 122 | + "Accept-Language" = language |
| 123 | + ) |> |
| 124 | + ## Authenticate ---- |
| 125 | + httr2::req_oauth_client_credentials( |
| 126 | + client = client, |
| 127 | + scope = "icdapi_access" |
| 128 | + ) |> |
| 129 | + ## Perform request ---- |
| 130 | + httr2::req_perform() |
| 131 | +} |
| 132 | + |
| 133 | + |
0 commit comments