2 min read

ElasticSearch bulk update and R

Aim

Test the bulk update feature of the wonderful elastic search package https://github.com/ropensci/elastic/tree/bulk-update

Install R packages

#install.packages("curl")
#devtools::install_github("ropensci/elastic@bulk-update")

Set up index with data set

library(elastic)
library(tidyverse)
## Loading tidyverse: ggplot2
## Loading tidyverse: tibble
## Loading tidyverse: tidyr
## Loading tidyverse: readr
## Loading tidyverse: purrr
## Loading tidyverse: dplyr
## Conflicts with tidy packages ----------------------------------------------
## count():   dplyr, elastic
## explain(): dplyr, elastic
## filter():  dplyr, stats
## lag():     dplyr, stats
connect(es_port = 9200)
## transport:  http 
## host:       127.0.0.1 
## port:       9200 
## path:       NULL 
## username:   NULL 
## password:   <secret> 
## errors:     simple 
## headers (names):  NULL
df <- data.frame(name = letters[1:3], size = 1:3, id = 100:102)

invisible(docs_bulk(df, 'foobar', 'foobar', es_ids = FALSE))
## 
  |                                                                       
  |                                                                 |   0%
  |                                                                       
  |=================================================================| 100%

Index is available http://localhost:9200/foobar/foobar/101

Test the update function

df2 <- data.frame(size = c(45, 56), id = 100:101)
df2
##   size  id
## 1   45 100
## 2   56 101
cat('before update')
## before update
Search("foobar", asdf = TRUE)$hits$hits
##   _index  _type _id _score _source.name _source.size _source.id
## 1 foobar foobar 100      1            a            1        100
## 2 foobar foobar 101      1            b            2        101
## 3 foobar foobar 102      1            c            3        102
#(docs_bulk_update(df2, index = 'foobar', type = 'foobar'))
#Search("foobar", asdf = TRUE)$hits$hits

Error using the docs_bulk_update

docs_bulk_update(df2, index = ‘foobar’, type = ‘foobar’)

Error: could not find function “docs_bulk_update”