ropensci / elastic

R client for the Elasticsearch HTTP API
https://docs.ropensci.org/elastic
Other
245 stars 58 forks source link

list indexes in database and list properties in an index #289

Closed tedmoorman closed 1 year ago

tedmoorman commented 2 years ago

Just in case this is useful ...

#' List indexes in Elasticsearch database
#'
#' Creates a single column data frame of the indexes in an Elasticsearch database
#' 
#' @param con The connection to the Elasticsearch database.
#' 
#' @family elastic
#' 
#' @importFrom tibble rownames_to_column
#' @importFrom dplyr select
#' @importFrom dplyr mutate
#' @importFrom dplyr arrange
#' @importFrom stringr str_replace_all
#' @importFrom elastic mapping_get
#' 
#' @return A data frame with the following column: index
#' @export
#' 
#' @examples
#' library(elastic)
#' es <- elastic::connect()
#'
#' indexes <- index_list(es)
index_list <- function(con){
  elastic::mapping_get(con, index = "_all") %>% 
    t() %>% 
    data.frame() %>% 
    t() %>% 
    data.frame() %>% 
    tibble::rownames_to_column("index") %>% 
    dplyr::select(index) %>% 
    dplyr::mutate(index = index %>% stringr::str_replace_all(fixed("."), fixed("-"))) %>% 
    dplyr::arrange(index)
}
#' List properties for an index in Elasticsearch database
#'
#' Creates a single column data frame listing the properties for an index in an Elasticsearch database
#' 
#' @param con The connection to the Elasticsearch database.
#' @param index The index in the Elasticsearch database to be searched.
#' 
#' @family elastic
#' 
#' @importFrom tibble rownames_to_column
#' @importFrom dplyr select
#' @importFrom dplyr arrange
#' @importFrom elastic mapping_get
#' 
#' @return A data frame with the following column: properties
#' @export
#' 
#' @examples
#' library(elastic)
#' es <- elastic::connect()
#'
#' properties <- properties_list(es, index = "the-index-name")
properties_list <- function(con, index){
  elastic::mapping_get(con, index = index)[[index]]$mappings$properties %>% 
    t() %>% 
    data.frame() %>% 
    t() %>% 
    data.frame() %>% 
    tibble::rownames_to_column("properties") %>% 
    dplyr::select(properties) %>% 
    dplyr::arrange(properties)
}
sckott commented 2 years ago

@tedmoorman nice ! I think it's out of scope, but thanks for sharing

maelle commented 1 year ago

For info this package is looking for a new maintainer cf #292 :smile_cat:

maelle commented 1 year ago

I see this should have been closed ("out of scope").