Skip to content

Bulk search function in the package that I missed? #287

@tedmoorman

Description

@tedmoorman

Just curious if there was a bulk search function in the package that I may have missed? I ended up writing a function to perform the bulk search. I'm not sure if something like this might be worth adding to the package if currently missing. If it exists, I'd love to know about it!

library(elastic)
library(dplyr)

elastic_search <- function(df, name, con, index, slop = 0, limit = 50) {
  my_df_mod <- df %>% 
    mutate(
      row = row_number(),
      max_row = max(row),
      to_search = if_else(max_row == row,
                     paste0('{"match_phrase": {"name": {"query": "', {{ name }}, '", "slop":', slop, '}}}'),
                     paste0('{"match_phrase": {"name": {"query": "', {{ name }}, '", "slop":', slop, '}}},'))
    ) %>% 
    select(to_search)
  
  tf <- tempfile(fileext = ".json")
  cat('{"query": {"bool": {"should": [', file = tf, sep = "\n", append = TRUE)
  for (to_search in my_df_mod) {
    cat(to_search, file = tf, sep = "\n", append = TRUE)
  }
  cat(']}}}', file = tf, sep = "\n", append = TRUE)
  
  Search(con, index=index, body=readLines(tf), sort = "_score:asc", asdf = TRUE, size = limit)$hits$hits
}

my_df <- data.frame(
matrix(
  c(
    "john doe",
    "jane doe"
  ),
  nrow = 2,
  ncol = 1,
  byrow = TRUE,
  dimnames = list(NULL,
                  c("name"))
),
stringsAsFactors = FALSE
)

x <- connect()

result_df <- elastic_search(my_df, name, x, "index-name-here", slop = 100, limit = 200)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions