ViewQuery not working when using startkey, endkey

Hello guys!

Now, I’m using couchbase for POC.

I have a problem about ViewQuery api issue.
below uri is Filter Results of couchbase console.
?startkey=%5B%2221st_amendment_brewery%22%5D&endkey=%5B%2221st_amendment_brewery.%22%5D&inclusive_end=false&stale=update_after&connection_timeout=60000&limit=10&skip=0

It’s working fine.
but, ViewQuery reslut of java sdk 2.0.1 is nothing
Here is code

String startKey = "[\"21st_amendment_brewery\"]";
String endKey = "[\"21st_amendment_brewery.\"]";
        
ViewQuery query = ViewQuery.from("beer", "brewery_beers");

query.limit(10)
        .startKey(startKey)
        .endKey(endKey);

I spent whole this week for a solve this problem.
finally, i found code at sdk source code.

public ViewQuery startKey(String key) {
    params[PARAM_STARTKEY_OFFSET] = "startkey";
    params[PARAM_STARTKEY_OFFSET+1] = encode("\"" + key + "\"");
    return this;
}

When encoding key for ‘Rest call’ unnecessary value added key.
It should be removed.

can you fix it?

I tried to regist a issue on jira. can not login jira.

Hi, sorry that you spent lots of time on this.

Actually, it’s not a bug - it’s a feature - you were just using the wrong method. If you pass in a string into startKey it will surround it with “”. Since you want to pass in a Json array, use the overload for the array!

.startKey(JsonArray.from(“21st_amendment_brewery”)) and the same for endKey.

Thank you very much for your fast reply @daschl .

The reason of why is below.
As i mentioned before, The result of uri(viewquery.toString()) is different compared couchbase console.
below link is made by couchbase console.

http://192.168.56.101:8092/default/_design/dev_rooms/_view/roomList?stale=false&startkey=[%22room%22%2C%222169170%22]&endkey=[%22room%22%2C%222169170.%22]&inclusive_end=false&connection_timeout=60000&limit=10&skip=0

It’s working.
There is no quotes.

below is result of viewQuery.toString method.

limit=2&startkey=%22%5B%22room%22%2C%222169170%22%5D%22&endkey=%22%5B%22room%22%2C%222169170.%22%5D%22

%22 is quotes.
The value of searching key was surrounded by quotes.
I tried sending this uri to couchbase server.

http://192.168.56.101:8092/default/_design/dev_rooms/_view/roomList?stale=false&startkey=%22%5B%22room%22%2C%222169170%22%5D%22&endkey=%22%5B%22room%22%2C%222169170.%22%5D%22&limit=2

result is error(from couchbase server.)

{"error":"bad_request","reason":"invalid UTF-8 JSON: {{error,garbage_after_value},\"\\\"[\\\"room\\\",\\\"2169170\\\"]\\\"\"}"}

What is the problem?

Can you please post the full code you’re using? I’m pretty sure you’re using the wrong method calls.

Every method where JSON is supported on the view query the code provides overloads for all possible json values. If you pass in a string it will be quoted. In your case you need an array of strings, so you need to pass in a JsonArray so it can properly be converted.

So instead of this:

Do:

query.limit(10)
    .startKey(JsonArray.from("21st_amendment_brewery"))
    .endKey(JsonArray.from("21st_amendment_brewery."));

Hi Daschl,
I use startKey and endKey in Couchbase web console view works, but when I use the java api like this:
query.startKey(JsonArray.from(“football.na.usa:team=cle”))
.endKey(JsonArray.from(“football.na.usa:team=cle\u0FFF”))
.descending(true).limit(10))
I got empty result, should I endcode “\u0FFF”? Please help.

Thanks

I made a mistake here, I set descend to true, that’s why.

That is a useful thread, I’m having very similar problem. 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.

Any tips on what may be wrong?