0

I know there is a way to access reduced view results using the Couchbase java sdk. What I am currently unable to do is use spring-data to access the reduced view. Is this possible?

view:

View byIdGetDateStats = DefaultView.create("byIdGetDateStats",
            "function (doc, meta) {"
            + "if(doc.docType == \"com.bd.idm.model.DayLog\"){"
            + "   emit(doc.userid, doc.date)"
            +"    }}"
            ,"_stats"
            );

When I attempt to use spring-data to access the view like this:

Query query = new Query();
query.setKey(ComplexKey.of(userid)).setReduce(true).setGroup(true);
ViewResult result = repo.findByIdGetDateStats(query);

Error Message

java.lang.IllegalStateException: Failed to execute CommandLineRunner
...

Caused by: java.lang.RuntimeException: Failed to access the view
...

Caused by: java.util.concurrent.ExecutionException: OperationException: SERVER: query_parse_error Reason: Invalid URL parameter 'group' or  'group_level' for non-reduce view.
....
Caused by: com.couchbase.client.protocol.views.ViewException: query_parse_error Reason: Invalid URL parameter 'group' or  'group_level' for non-reduce view.

....

DavidActualX
  • 55
  • 1
  • 5

1 Answers1

1

No, Spring-Data-Couchbase 1.x will always set reduce to false.

If you want to use a reduced view and get the ViewResult, use the CouchbaseTemplate#queryView() method.

You can still do so in a repository by defining custom repository method (see this chapter of the doc, you should be able to call getCouchbaseOperations in your implementation).

Simon Baslé
  • 27,105
  • 5
  • 69
  • 70
  • Thanks for the response. While your answer seems correct I wasn't able to get it to work due to failures. 2015-07-15 10:14:05.854 ERROR 59644 --- [ main] o.s.boot.SpringApplication : Application startup failed – DavidActualX Jul 15 '15 at 14:17
  • I was able to work past my issues and it does indeed work. Thanks again! – DavidActualX Jul 15 '15 at 15:35
  • note that there's a 2.0 in the works (https://github.com/spring-projects/spring-data-couchbase/tree/2.0.x, **unreleased**) where the reduce is expected to work with the following kind of interface: `@View(viewName="byUserIdWithReduce") public long countByUserIdEquals(String userId);` (note that group is for now not planned and that you have to use a valid property of your entity, here `userId`, in your method name for query derivation to work) => will result in `reduce=true&&key=userIdValue` – Simon Baslé Jul 15 '15 at 15:44