ropensci / elastic

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

Same connection across simultaneous shiny sessions? #264

Closed yvanrichard closed 4 years ago

yvanrichard commented 4 years ago

Hi there,

I am developing a Shiny app that uses elastic to search text in a large corpus. The query is entered in a textInput, which is then used by Search() to query the ES index. The result with the highlighted text is then displayed in a DT::datatable, but my issue is that the Search query is shared across sessions, so when a user changes the text query, the table gets updated across all users...

I've got conn <- connect(user = as.character(rnorm(1)), force=T) inside the shinyServer function, which technically should create a connection per session, but no.

Here's the detail of one connection from within the app conn$info(verbose=T):

*   Trying 127.0.0.1...                                                                                                 
* TCP_NODELAY set                                                                                                       
* Connected to 127.0.0.1 (127.0.0.1) port 9200 (#0)                                                                     
* Server auth using Basic with user '-2.30671775224995'                                                                 
> GET / HTTP/1.1                                                                                                        
Host: 127.0.0.1:9200                                                                                                    
Authorization: Basic LTIuMzA2NzE3NzUyMjQ5OTU6                                                                           
User-Agent: libcurl/7.58.0 r-curl/4.0 crul/0.8.4                                                                        
Accept-Encoding: gzip, deflate                                                                                          
Accept: application/json, text/xml, application/xml, */*                                                                

< HTTP/1.1 200 OK                                                                                                       
< content-type: application/json; charset=UTF-8                                                                         
< content-encoding: gzip                                                                                                
< content-length: 299                                                                                                   
<                                                                                                                       
* Connection #0 to host 127.0.0.1 left intact                                                                           
$name                                                                                                                   
[1] "rowi"                                                                                                              

$cluster_name                                                                                                           
[1] "elasticsearch"                                                                                                     

$cluster_uuid                                                                                                           
[1] "klbo9gGeQQeg0NxFSPQcaA"                                                                                            

$version                                                                                                                
$version$number                                                                                                         
[1] "7.3.1"                                                                                                             

$version$build_flavor                                                                                                   
[1] "default"                                                                                                           

$version$build_type
[1] "deb"

$version$build_hash
[1] "4749ba6"

$version$build_date
[1] "2019-08-19T20:19:25.651794Z"

$version$build_snapshot
[1] FALSE

$version$lucene_version
[1] "8.1.0"

$version$minimum_wire_compatibility_version
[1] "6.8.0"

$version$minimum_index_compatibility_version
[1] "6.0.0-beta1"

$tagline
[1] "You Know, for Search"

Is there something I missed? Any help would be appreciated.

Session Info ```r > devtools::session_info() ─ Session info ───────────────────────────────────── setting value version R version 3.6.1 (2019-07-05) os Ubuntu 18.04.3 LTS system x86_64, linux-gnu ui X11 language en_NZ:en collate en_NZ.UTF-8 ctype en_NZ.UTF-8 tz Pacific/Auckland date 2019-09-02 ─ Packages ─────────────────────────────────────── package * version date lib source assertthat 0.2.1 2019-03-21 [1] CRAN (R 3.6.1) backports 1.1.4 2019-04-10 [1] CRAN (R 3.6.1) callr 3.3.1 2019-07-18 [1] CRAN (R 3.6.1) cli 1.1.0 2019-03-19 [1] CRAN (R 3.6.1) crayon 1.3.4 2017-09-16 [1] CRAN (R 3.6.1) crul * 0.8.4 2019-08-02 [1] CRAN (R 3.6.1) curl 4.0 2019-07-22 [1] CRAN (R 3.6.1) data.table * 1.12.2 2019-04-07 [1] CRAN (R 3.6.1) desc 1.2.0 2018-05-01 [1] CRAN (R 3.6.1) devtools 2.1.0 2019-07-06 [1] CRAN (R 3.6.1) digest 0.6.20 2019-07-04 [1] CRAN (R 3.6.1) DT * 0.8 2019-08-07 [1] CRAN (R 3.6.1) elastic * 1.0.0 2019-04-11 [1] CRAN (R 3.6.1) fs 1.3.1 2019-05-06 [1] CRAN (R 3.6.1) glue 1.3.1 2019-03-12 [1] CRAN (R 3.6.1) htmltools 0.3.6 2017-04-28 [1] CRAN (R 3.6.1) htmlwidgets 1.3 2018-09-30 [1] CRAN (R 3.6.1) httpcode 0.2.0 2016-11-14 [1] CRAN (R 3.6.1) httpuv 1.5.1 2019-04-05 [1] CRAN (R 3.6.1) jsonlite 1.6 2018-12-07 [1] CRAN (R 3.6.1) later 0.8.0 2019-02-11 [1] CRAN (R 3.6.1) magrittr 1.5 2014-11-22 [1] CRAN (R 3.6.1) memoise 1.1.0 2017-04-21 [1] CRAN (R 3.6.1) mime 0.7 2019-06-11 [1] CRAN (R 3.6.1) pkgbuild 1.0.5 2019-08-26 [1] CRAN (R 3.6.1) pkgload 1.0.2 2018-10-29 [1] CRAN (R 3.6.1) prettyunits 1.0.2 2015-07-13 [1] CRAN (R 3.6.1) processx 3.4.1 2019-07-18 [1] CRAN (R 3.6.1) promises 1.0.1 2018-04-13 [1] CRAN (R 3.6.1) ps 1.3.0 2018-12-21 [1] CRAN (R 3.6.1) R6 2.4.0 2019-02-14 [1] CRAN (R 3.6.1) Rcpp 1.0.2 2019-07-25 [1] CRAN (R 3.6.1) remotes 2.1.0 2019-06-24 [1] CRAN (R 3.6.1) rlang 0.4.0 2019-06-25 [1] CRAN (R 3.6.1) rprojroot 1.3-2 2018-01-03 [1] CRAN (R 3.6.1) sessioninfo 1.1.1 2018-11-05 [1] CRAN (R 3.6.1) shiny * 1.3.2 2019-04-22 [1] CRAN (R 3.6.1) shinyBS * 0.61 2015-03-31 [1] CRAN (R 3.6.1) testthat 2.2.1 2019-07-25 [1] CRAN (R 3.6.1) usethis 1.5.1 2019-07-04 [1] CRAN (R 3.6.1) withr 2.1.2 2018-03-15 [1] CRAN (R 3.6.1) xtable 1.8-4 2019-04-21 [1] CRAN (R 3.6.1) yrpkg * 1.39 2019-08-12 [1] local [1] /usr/local/lib/R/site-library [2] /usr/lib/R/site-library [3] /usr/lib/R/library ```
sckott commented 4 years ago

thanks for the report @yvanrichard

I need to update the docs to clarify what the result of calling connect() contains. Sorry about the confusion. Will do that.

Calling connect() does not create a connection object like that of a SQL database connection. connect() is a thin wrapper around the Elasticsearch R6 class https://github.com/ropensci/elastic/blob/master/R/Elasticsearch.R#L90-L231 that holds your connection options, etc.

There is only one Elasticsearch instance, correct?

Having said that, I'm not sure the problem is with this package. Since your users aren't altering the data in your Elasticsearch instance (correct?) then perhaps it's the Shiny sessions that aren't being properly separated? I have very limited Shiny knowledge, but is there a chance that the shiny sessions details in your app aren't set up correctly? (not meaning to avoid blame here, just trying to understand)

yvanrichard commented 4 years ago

Eeek... Indeed, it was my mistake. I had the result of the ES query stored in a reactive value, which was declared outside the shinyServer function by mistake, and therefore shared across sessions. Silly me...

Sorry for the bother and thank you for responding, @sckott. And also thank you for your valuable contributions.

sckott commented 4 years ago

glad it was figured out. and side benefit that we clarify the docs a bit more

thx for using the pkg