Search code examples
spring-data-couchbase

How to write a Spring JPA repository method name to activate the Distinct clause in Couchbase?


Working on a Java application that uses Spring Data Couchbase 2.2.0.RELEASE...

Starting with a list of JSON objects that represent Book objects:

[
    {id: 123, title: "Abc", category: "A"},
    {id: 456, title: "Efg", category: "B"},
    {id: 789, title: "Abc", category: "A"}
]

The array of Book objects are inserted into Couchbase. Later, the application would like get a list of distinct book titles back based on a category filter. Following some of the Spring documentation, I've arrived at this method name in the BookRepository interface:

List<Book> findDistinctTitleByCategory(String category);

However, the query that is created by Spring does not contain the Distinct clause for title. Here's is the final query that Spring sends to the CB cluster where bucket name here is default:

Executing N1QL query: {"statement":"SELECT META(`default`).id AS _ID, META(`default`).cas AS _CAS, `default`.* FROM `default` WHERE (`category` = \"A\")","scan_consistency":"not_bounded"}

Am I writing the method name wrong?


Solution

  • SDC currently does not support query derivation for distinct. I have created a ticket for enhancement here. In the meantime, you can work around by directly using @Query instead of n1ql.selectEntity, provide the select part.

    If you are fetching only the title, SDC supports projections.

    interface OnlyTitle {
        String getTitle(); 
    }
    
    @Query(...)
    OnlyTitle findDistinctTitleByCategory(String category);