ropensci / elastic

R client for the Elasticsearch HTTP API
https://docs.ropensci.org/elastic
Other
245 stars 58 forks source link

Search for value that matches R variable #203

Closed ebdavison closed 6 years ago

ebdavison commented 6 years ago

I need to be able to search for a set of documents where a particular field matches.

My code is like:

library(elastic)
connect("http://10.0.0.1:9200")
customer <- 'example'
res = Search(index="index-*", type="company", q="customer=<<customer>>", size=1000, body = '{
              "_source": ["timeStamp", "custid", "customer"]
              }')

But I have not been able to make this work with either a fixed string 'customer=example' nor can I figure out how to use the R variable as a replacement.

This sort of example is not in the documentation and I need to be able to subset this based on an R variable as I produce a report from Rmarkdown using elastic to gather info for graphs and such.

sckott commented 6 years ago

thx for the question @ebdavison

this is beyond scope of this package I think, but here's a reproducible example using whisker:

library(elastic)
library(whisker)
elastic::connect()
shakespeare <- system.file("examples", "shakespeare_data.json", package = "elastic")
if (!index_exists("shakespeare")) invisible(docs_bulk(shakespeare))
x <- 'KING HENRY IV'
Search(index = "shakespeare",
       q = whisker.render("speaker: {{x}}", list(x = x)), 
       size = 10, 
       body = '{"_source": ["play_name", "speaker"]}', asdf = TRUE)

x <- 'FALSTAFF'
Search(index = "shakespeare",
       q = whisker.render("speaker: {{x}}", list(x = x)), 
       size = 10, 
       body = '{"_source": ["play_name", "speaker"]}', asdf = TRUE)

help?

ebdavison commented 6 years ago

Perfect!!! That is exactly what I needed and works perfectly.

Thanks.

sckott commented 6 years ago

great , glad it works