2

My Spring boot app is trying to get all documents of type Cat from the Couchbasebucket.

There is an index for that:

CREATE INDEX cats_idx ON `cats`(_class) WHERE _class = 'com.example.Cat'

And there is a Repository class:

public interface CatRepository extends CouchbaseRepository<Cat, String>

When calling this from the code

Iterable<Cat> all = catRepository.findAll();

I get this exception:

Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request 
processing failed; nested exception is 
org.springframework.dao.InvalidDataAccessResourceUsageException: View cat/all does not exist.; 
nested exception is com.couchbase.client.java.error.ViewDoesNotExistException: View cat/all does not 
exist.] with root cause 
rx.exceptions.OnErrorThrowable$OnNextValue: OnError while emitting onNext value: com.couchbase.client.java.document.json.JsonObject.class
at rx.exceptions.OnErrorThrowable.addValueAsLastCause(OnErrorThrowable.java:118)
at rx.internal.operators.OnSubscribeMap$MapSubscriber.onNext(OnSubscribeMap.java:73)
at rx.internal.operators.OnSubscribeMap$MapSubscriber.onNext(OnSubscribeMap.java:77)
at rx.internal.producers.SingleProducer.request(SingleProducer.java:65)
at rx.Subscriber.setProducer(Subscriber.java:211)
at rx.internal.operators.OnSubscribeMap$MapSubscriber.setProducer(OnSubscribeMap.java:102)
at rx.internal.operators.OnSubscribeMap$MapSubscriber.setProducer(OnSubscribeMap.java:102)
at rx.internal.operators.OperatorSingle$ParentSubscriber.onCompleted(OperatorSingle.java:113)
at rx.internal.operators.OperatorObserveOn$ObserveOnSubscriber.checkTerminated(OperatorObserveOn.java:281)
at rx.internal.operators.OperatorObserveOn$ObserveOnSubscriber.call(OperatorObserveOn.java:216)
at rx.internal.schedulers.ScheduledAction.run(ScheduledAction.java:55)
at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511)
at java.util.concurrent.FutureTask.run$$$capture(FutureTask.java:266)
at java.util.concurrent.FutureTask.run(FutureTask.java)
at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.access$201(ScheduledThreadPoolExecutor.java:180)
at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.run(ScheduledThreadPoolExecutor.java:293)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
at java.lang.Thread.run(Thread.java:748)
Matthew Groves
  • 25,181
  • 9
  • 71
  • 121
riorio
  • 6,500
  • 7
  • 47
  • 100

2 Answers2

2

The current implementation of the Spring Data SDK still uses views internally for methods like findAll and removeAll (views are no longer used on the SDK version 3.0). So you could either create a view for this document type or implement a new findAll method yourself:

@Query("#{#n1ql.selectEntity} where #{#n1ql.filter} ")
List<Cat> all();

The method should use your cats_idx

PS: The cats_idx is not optimal, you should not store the _class attribute in the index.

deniswsrosa
  • 2,421
  • 1
  • 17
  • 25
  • Can you elaborate more about the problem with the index? – riorio Jan 03 '20 at 17:02
  • storing _class in the index means that you will essentially have the same string "com.example.Cat" a few million times (assuming you have a few million cats). Your index should filter by _class but store more useful attributes: create cats_idx ON `cats`(age, name, race) where _class = 'com.example.Cat' . I wrote this tutorial here https://docs.couchbase.com/tutorials/spring-data-indexes/spring-index.html – deniswsrosa Jan 03 '20 at 19:44
  • Hello, I am getting the same error as mentioned in the question. I tried the solution by using the Query Annotation, but getting the same error again. My doubt is i am not implementing the solution correctly. I am doing the below, please correct me if i missed out something. @RequestMapping (value = "/all", method = RequestMethod.GET) @Query("select * from bucketname") public Iterable getAllCats() { Iterable all = catRepository.findAll(); return all; } – Jenny Feb 22 '21 at 21:52
0

I also faced the same problem when using the couch base and if there is a .findAll() method is getting executed. So I gave the following annotation in the repository class.

@N1qlPrimaryIndexed
@ViewIndexed(designDoc = "product",viewName = "all")
public interface ProductRepository extends CouchbaseRepository<Product,Integer> {
}

The above approach worked for me.

Senthuran
  • 1,583
  • 2
  • 15
  • 19