Nestor Milyaev
Nestor Milyaev

Reputation: 6595

Couchbase Java API not returning values for set range

I'm having a weird problem with Couchbase Java API. I use the following code:

ViewQuery query = ViewQuery.from(BUCKET_NAME, GET_ENTITIES_VIEW_NAME);
... 
// set companyStart, companyEnd as Strings
// set query.limit and query.skip
...
query.startKey(toJsonArray(companyStart, Long.toString(params.getStartDate().getTime())));
        endKey(toJsonArray(companyEnd, Long.toString(params.getEndDate().getTime())));
ViewResult results;
results = bucket.query(query);
...

When I try the said start and endKeys (like ["ROTOR", 146538100000]) in the Couchbase console, the query returns all the expected results.

However, with the Java API the results is empty.

If I comment out the query.startKey and .endKey lines, it faithfully returns all the results for the view.

Here's my view:

function (doc, meta) {
  if(doc.collectorData.documenttypes.terms[0] && doc.collectorData.documenttypes.terms[0]=='EAP:Article') {
    emit([doc.collectorData.userdata.company,doc.timestamp], {"visitId":doc.visitId,"visitorId":doc.visitorId,"company":doc.collectorData.userdata.company,"timestamp":doc.timestamp, "userAgent":doc.userAgent, "pathInfo":doc.pathInfo, "channel":doc.collectorData.channel, "newVisit":doc.newVisit});
   }
}

Any tips on what may be wrong?

Upvotes: 1

Views: 76

Answers (2)

Nestor Milyaev
Nestor Milyaev

Reputation: 6595

Simon's tips were most helpful. The mystery had a simple solution: I forgot to publish the view, and the old view only had one-field key, so the query was executing successfully yet returning nothing! A comment somewhere through the thread on https://forums.couchbase.com/t/not-able-to-get-all-rows-returned-from-couchbase-view/5020/23 provided the enlightenment.

Upvotes: 0

Simon Baslé
Simon Baslé

Reputation: 28351

You are using Long.toString, so it is not equivalent to what you used in the couchbase console. That would be equivalent to ["ROTOR", "146538100000"]. A subtle but meaningful difference!

Try with the following snippet instead (I explicitly used JsonArray.from as well, just to remove any ambiguity):

query
    .startKey(JsonArray.from(companyStart, params.getStartDate().getTime()))
    .endKey(JsonArray.from(companyEnd, params.getEndDate().getTime()));

Upvotes: 2

Related Questions