Is there an alternative to startKey and endKey in Views using Java SDK 1.4?


#1

Hi,

I wish to obtain results from a couchbase view by dynamically passing a query parameter. Eg. When a user types ‘ab’, Couchbase must return a list of document IDs from the bucket that start with ‘ab’-> ‘abc’, ‘abcd’, ‘abc123’ and so on.
For this purpose, I’ve created a design document, a view - ‘sampleview and a map function’.

                DesignDocument designDoc = new DesignDocument(bucket);
                String viewName = "sampleview";
                String mapFunction =
                        "function (doc, meta) {\n" +
                        "    emit(meta.id);\n" +         
                        "}";
                ViewDesign viewDesign = new ViewDesign(viewName,mapFunction);
                designDoc.getViews().add(viewDesign);
                client.createDesignDoc( designDoc );

In my java application that uses couchbase SDK 1.4, I would like to do something like this:

        View view = client.getView(bucketname, "sampleview");
        String startKey = "[\"ab\"]";
        String endKey = "[\"ab\uefff\"]";
        ViewResponse result = client.query(view, viewquery); //BUT THIS ISNT SUPPORTED BY SDK 1.4
        for(ViewRow row : result) { 
             //do some stuff
         }

My question is, in SDK 1.4, I’m unable to use startKey and endKey because Viewquery is available from SDK 2 and above. Due to various reasons my team doesn’t want to upgrade to 1.4. So, I’d like to make this work with SDK 1.4 only. Any suggestions?

Note: The feature I am looking for is similar to the SQL ‘like’ operator.


#2

You could use setRange()


#3

Worked like a charm. Thank you so much!!