ropensci / sofa

Easy R interface to CouchDB
https://docs.ropensci.org/sofa/
33 stars 17 forks source link

design_search with start_key and end_key returns all documents, and does not honor the key range #62

Closed ghost closed 4 years ago

ghost commented 6 years ago

Here is the version of sofa I am using:

packageVersion('sofa')
[1] ‘0.2.2.9214’

Here is what I am trying, based on the view having a composite key on an ID value and a timestamp value:

output <- design_search(
  connection,
  'dbname',
  design = 'designname',
  view = 'viewname',
  start_key = list("27ea7008aaf5f9bee3b4efd856241f2d", 1506837600),
  end_key = list("27ea7008aaf5f9bee3b4efd856241f2d", 1509515999)
  )

Here is the result where the keys returned are completely out of range with the first key value having 17 unique values whereas I only specified exactly one, and the second value of the key being totally out of range where as I specified a range above:

length(unique((sapply(output$rows, function(x) x$key[[1]]))))
[1] 17
summary((sapply(output$rows, function(x) x$key[[2]])))
     Min.   1st Qu.    Median      Mean   3rd Qu.      Max. 
1.475e+09 1.495e+09 1.503e+09 1.501e+09 1.510e+09 1.512e+09 

Is there a different way to specify this correctly that I am missing? I also tried as follows with a vector instead of list and the output has no change:

output <- design_search(
  connection,
  'dbname',
  design = 'designname',
  view = 'viewname',
  start_key = c("27ea7008aaf5f9bee3b4efd856241f2d", 1506837600),
  end_key = c("27ea7008aaf5f9bee3b4efd856241f2d", 1509515999)
  )

When I inspect the documents returned, the key is correctly there as a 'list' with two items, the first being an id and the second being a timestamp:

output[[1]]$key
[[1]]
[1] "09f3eef7b47164b0ed9f1b7775f3e5be"

[[2]]
[1] 1511170394
sckott commented 6 years ago

thanks @tumulurig3

can you elaborate on what you mean by composite key?

ghost commented 6 years ago

Meaning the key is made up of two different fields in the document. Some field called 'some_id' that is a document ID of some other document, and a timestamp field that is integer in seconds since epoch. Both are combined to be the key on the view.

sckott commented 6 years ago

apologies, but i'm not familiar with composite/complex keys. any couchdb docs on that?

ghost commented 6 years ago

Here is the cloudant screenshot with one of the documents as example. You see id, key and value columns. Value is the actual document. Key is a combination of 'building_id' and 'timestamp' fields. The id is the cloudant ID of the document that is indexed based on building_id/timestamp combination.

Ignore my terminology about 'composite.' All I mean is that the index on this view is made up of two fields within the document.

picture1

sckott commented 6 years ago

thanks for that.

sckott commented 5 years ago

@ghost sorry for the very long delay.

first, those parameters (startkey, endkey) need to be passed to either params or body. if anything is passed to body, it becomes a POST request, if just params, then it's a GET request.

I needed to tweak the internals a bit, and now you can use startkey and endkey , see eg https://github.com/ropensci/sofa/blob/master/R/design_search.R#L123-L135

however, i still haven't figured out how to make it work for composite keys, maybe you will have luck with that.