elasticdsl

library("knitr")
hook_output <- knitr::knit_hooks$get("output")
knitr::knit_hooks$set(output = function(x, options) {
   lines <- options$output.lines
   if (is.null(lines)) {
     return(hook_output(x, options))  # pass to default hook
   }
   x <- unlist(strsplit(x, "\n"))
   more <- "..."
   if (length(lines)==1) {        # first n lines
     if (length(x) > lines) {
       # truncate the output, but add ....
       x <- c(head(x, lines), more)
     }
   } else {
     x <- c(if (abs(lines[1])>1) more else NULL,
            x[lines],
            if (length(x)>lines[abs(length(lines))]) more else NULL
           )
   }
   # paste these lines together
   x <- paste(c(x, ""), collapse = "\n")
   hook_output(x, options)
 })

knitr::opts_chunk$set(
  comment = "#>",
  collapse = TRUE,
  warning = FALSE,
  message = FALSE
)

Project Status: Suspended – Initial development has started, but there has not yet been a stable, usable release; work has been stopped for the time being but the author(s) intend on resuming work. Build Status Build status codecov.io

An R DSL for Elasticsearch

Elasticsearch info

Security

You're fine running ES locally on your machine, but be careful just throwing up ES on a server with a public IP address - make sure to think about security.

Install elasticdsl

install.packages("devtools")
devtools::install_github("ropensci/elasticdsl")
library('elasticdsl')

Setup

Instructions for installing, upgrading, starting Elasticsearch, and loading example data at ropensci/elastic

Initialization

The function elastic::connect() is used before doing anything else to set the connection details to your remote or local elasticdslsearch store. The details created by connect() are written to your options for the current session, and are used by elasticdsl functions.

elastic::connect(es_port = 9200)

Set the index to use

index("shakespeare")

Print query as pretty json

index("shakespeare") %>%
  filter() %>% 
  ids(c(1, 2, 150)) %>%
  explain() # doesn't exist yet

Execute query

res <- index("shakespeare") %>%
  filter() %>% 
  ids(c(1, 2)) %>%
  exec()

n() to get number of results

index("shakespeare") %>%
  ids(c(1, 2)) %>%
  exec() %>% 
  n()

Request size

index("shakespeare") %>%
  filter() %>% 
  prefix(speaker = "we") %>%
  size(2) %>% 
  fields(play_name) %>% 
  exec() %>% 
  n()

Request certain fields

s <- index("shakespeare") %>%
  filter() %>% 
  prefix(speaker = "we") %>%
  size(2)
s %>% fields(play_name) %>% exec() %>% .$hits %>% .$hits
s %>% fields(play_name, text_entry) %>% exec() %>% .$hits %>% .$hits
s %>% fields(play_name, text_entry, line_id) %>% exec() %>% .$hits %>% .$hits

Filters vs. queries

Filters are boolean queries and are much more computationally efficient than queries.

Filters

prefix filter

index("shakespeare") %>%
  filter() %>% 
  prefix(speaker = "we") %>%
  exec() %>% 
  n()

ids filter

index("shakespeare") %>%
  filter() %>% 
  ids(c(1, 2, 150)) %>%
  exec() %>% 
  n()

Queries

geoshape query (filters have a much larger range of geo queries)

index("geoshape") %>%
  geoshape(field = "location", type = "envelope", coordinates = list(c(-30, 50), c(30, 0))) %>% 
  n()

Meta

rofooter



ropensci/elasticdsl documentation built on May 18, 2022, 9:53 a.m.