Setting query params on ViewQuery


#1

Hi

I’m trying to set parameters on a viewQuery like

ViewQuery query = ViewQuery.from("program", "by_starttime_and_channel");
		ComplexKey startKey = ComplexKey.of(Arrays.asList(date.getYear(), date.getMonth(), date.getDayOfMonth(), 0, 0, 0), channel);
		ComplexKey endKey = ComplexKey.of(Arrays.asList(date.getYear(), date.getMonth(), date.getDayOfMonth(), 23, 59, 59), channel);
		query.startKey(JsonArray.from(startKey));
		query.endKey(JsonArray.from(endKey));
		ViewResult result = bucket.query(query);

The view is defined as

   function (doc, meta) {
      if(doc.program.pro_publish.ppu_start_timestamp_presentation) {
        emit([dateToArray(doc.program.pro_publish.ppu_start_timestamp_presentation), doc.program.pro_publish.ppu_channel], null); 
      }
     }

When the app is running I get an exception

java.lang.IllegalArgumentException: Unsupported type for JsonArray: class com.couchbase.client.protocol.views.ComplexKey
	at com.couchbase.client.java.document.json.JsonArray.from(JsonArray.java:91)

I have not been able to dig up any documentation on how to set start- and endKeys on viewQueries ???

I’m using v. 2.1.0 java client sdk


#2

hi @Preben,
Where are you getting the ComplexKey from? Looks like you’ve kept a dependency to the old generation (1.4?) of the SDK…

Directly constructing a JsonArray instead of a ComplexKey should do it:

JsonArray startKey = JsonArray.from(
    JsonArray.from(
        date.getYear(), 
        date.getMonth(),
        date.getDayOfMonth(),
        0,
        0,
        0),
    channel);

query.startKey(startKey);

#3

Thanks and yeah there was an old 1.4 version lurking around :smile: .
I guess the best examples I found used ComplexKey. Some new examples on the 2.x.x versions would be useful.


#4

@Preben in this terms it’s very similar. You get the same options on the previous Query object, but ComplexKey was just a workaround for the lack of proper JSON types. Now we have JsonObject and JsonArray directly so you can express all JSON supported types without wrappers like ComplexKey.


#5

Yeah, it’s true view documentation for 2.1 SDK doesn’t have a snippet for startKey/endKey…
For reference, here is the latest doc on view querying : http://docs.couchbase.com/developer/java-2.1/querying-views.html

Note that we are currently considering how best to favor newest documentations in search engines results :sunny: