# NOT RUN {
# Get a scroll_id
res <- Search(index = 'shakespeare', q="a*", scroll="1m")
res$`_scroll_id`
# Setting search_type=scan turns off sorting of results, is faster
res <- Search(index = 'shakespeare', q="a*", scroll="1m", search_type = "scan")
res$`_scroll_id`
# Pass scroll_id to scroll function
scroll(scroll_id = res$`_scroll_id`)
# Get all results - one approach is to use a while loop
res <- Search(index = 'shakespeare', q="a*", scroll="5m", search_type = "scan")
out <- list()
hits <- 1
while(hits != 0){
res <- scroll(scroll_id = res$`_scroll_id`)
hits <- length(res$hits$hits)
if(hits > 0)
out <- c(out, res$hits$hits)
}
length(out)
out[[1]]
# clear scroll
## individual scroll id
res <- Search(index = 'shakespeare', q="a*", scroll="5m", search_type = "scan")
scroll_clear(scroll_id = res$`_scroll_id`)
## many scroll ids
res1 <- Search(index = 'shakespeare', q="c*", scroll="5m", search_type = "scan")
res2 <- Search(index = 'shakespeare', q="d*", scroll="5m", search_type = "scan")
nodes_stats(metric = "indices")$nodes[[1]]$indices$search$open_contexts
scroll_clear(scroll_id = c(res1$`_scroll_id`, res2$`_scroll_id`))
nodes_stats(metric = "indices")$nodes[[1]]$indices$search$open_contexts
## all scroll ids
res1 <- Search(index = 'shakespeare', q="f*", scroll="1m", search_type = "scan")
res2 <- Search(index = 'shakespeare', q="g*", scroll="1m", search_type = "scan")
res3 <- Search(index = 'shakespeare', q="k*", scroll="1m", search_type = "scan")
scroll_clear(all = TRUE)
## sliced scrolling
body1 <- '{
"slice": {
"id": 0,
"max": 2
},
"query": {
"match" : {
"text_entry" : "a*"
}
}
}'
body2 <- '{
"slice": {
"id": 1,
"max": 2
},
"query": {
"match" : {
"text_entry" : "a*"
}
}
}'
res1 <- Search(index = 'shakespeare', scroll="1m", body = body1)
res2 <- Search(index = 'shakespeare', scroll="1m", body = body2)
scroll(scroll_id = res1$`_scroll_id`)
scroll(scroll_id = res2$`_scroll_id`)
out1 <- list()
hits <- 1
while(hits != 0){
tmp1 <- scroll(scroll_id = res1$`_scroll_id`)
hits <- length(tmp1$hits$hits)
if(hits > 0)
out1 <- c(out1, tmp1$hits$hits)
}
out2 <- list()
hits <- 1
while(hits != 0){
tmp2 <- scroll(scroll_id = res2$`_scroll_id`)
hits <- length(tmp2$hits$hits)
if(hits > 0)
out2 <- c(out2, tmp2$hits$hits)
}
c(
lapply(out1, "[[", "_source"),
lapply(out2, "[[", "_source")
)
# }
Run the code above in your browser using DataLab