Nestor Milyaev Nestor Milyaev - 1 year ago 62
Java Question

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 quiery.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.timestamp], {"visitId":doc.visitId,"visitorId":doc.visitorId,"company","timestamp":doc.timestamp, "userAgent":doc.userAgent, "pathInfo":doc.pathInfo, "channel", "newVisit":doc.newVisit});

Any tips on what may be wrong?

Answer Source

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):

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