Skip to content

Instantly share code, notes, and snippets.

@hillar
Created January 29, 2014 02:37
Show Gist options
  • Save hillar/8680809 to your computer and use it in GitHub Desktop.
Save hillar/8680809 to your computer and use it in GitHub Desktop.
R read data from elasticsearch
library(httr)
library (plyr)
set_config(config=structure(list(noproxy = c("127.0.0.1")), .Names = "noproxy", class = "config"));
set_config(config=accept_json());
u <- "http://127.0.0.1:9200/_all/_search"
# time series by fix period 1d
p <- "{\"facets\":{\"histo1\":{\"histogram\":{\"field\":\"yourtimefield\",\"time_interval\":\"1d\"}}},\"size\":0}"
r <- content(POST(u,body=p));
f<-r$facets$histo1$entries
df <- ldply (f, data.frame)
df$key <- as.POSIXct(df$key/1000, origin="1970-01-01")
plot(df,type='l')
# top 9 + other term
p <- "{\"facets\":{\"terms1\":{\"terms\":{\"field\":\"yourtermname\",\"size\": 9,\"order\": \"count\"}}}}"
r <- content(POST(u,body=p));
f <- r$facets$terms1$terms
df <- ldply (f, data.frame)
df <- rbind(df,data.frame(list(term=c('other'),count=r$facets$terms$other)))
library(ggplot)
qplot(term,count,data=df)+ geom_bar()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment