0

I have a PagingAndSortingRepository:

public interface BrowserLinkPagination extends PagingAndSortingRepository<BrowserLink, Long> {
    List<BrowserLink> findByUserAndUriLike(User user, String uri, Pageable pageable);

}

Now what I want to do is to search for multiple words in the uri column. Like comes pretty close, but it is order dependent on how the words occur in the string.
EDIT for clarification: Order dependences is exactly what I not want. I want the search strings to be independent of order, so LIKE is not what I am looking for.

I guess this is pretty common to find, having several search terms to look for in a string, I know I could implement it by providing the SQL to execute, but I am curiuous if there is a way to express that in terms of spring data? am

I am using postgresql for production and h2 for development / tests.

Neil Stockton
  • 11,383
  • 3
  • 34
  • 29
sveri
  • 1,372
  • 1
  • 13
  • 28
  • I think don't think it solves your issue, but you may take a look at "IN": http://stackoverflow.com/questions/23902954/spring-data-can-it-find-by-two-values-of-the-same-field-without-writing-the-imp – alexbt Jun 28 '16 at 10:31
  • As far as I can tell `IN`looks for exact matches like ids or similar things. I want more of a substring search. – sveri Jun 28 '16 at 11:20
  • If the order is important what is holding you back from using `LIKE` with something like `'%first%second%third%'`? Performance will be crappy but you should be able to find what you're looking for. – daniel.eichten Jun 28 '16 at 12:01
  • I updated the post that order dependence is what I not want. – sveri Jun 28 '16 at 12:42
  • Then you should be looking at document store such as Elasticsearch or Mongodb. – techtabu Jun 28 '16 at 14:33

2 Answers2

1

After reading more about the topic it is kind of obvious I need some kind of fulltext search. I will start using the one provided by postgresql. I found a short working example: http://rachbelaid.com/postgres-full-text-search-is-good-enough/

Thanks for the comments.

sveri
  • 1,372
  • 1
  • 13
  • 28
1

Use In in method name. In Your case it could look like that:

public interface BrowserLinkPagination extends PagingAndSortingRepository<BrowserLink, Long> {
    List<BrowserLink> findByUserAndUriIn(User user, List<String> uri, Pageable pageable);
}

When You are using the standard API created by Spring, the usage from the browser URI is pretty simple - just type in the address:

/your_api/browserlink/search/findByUserAndUriIn?user=xxx&uri=uri1,uri2,uri3
thorinkor
  • 958
  • 1
  • 11
  • 20