How do I implement boolean search using SolrCrudRepository

128 views Asked by At

I have a Spring Boot Application with Spring Data and SolrCrudRepository. My first steps were creating repository methods such as

    public Page<SolrDoc> findByTitle(String term, Pageable pageable);
    public Page<SolrDoc> findBySubject(String term, Pageable pageable);
    public Page<SolrDoc> findByType(String term, Pageable pageable);

which work fine. The solr log reports simple queries as one would expect:

path=/select params={q=title:"train"&start=0&sort=id+desc&rows=5&wt=javabin&version=2} hits=32 status=0

However, if I pass a boolean operator in the query such as AND or OR, I do not get the expected results, and the query is logged:

params={q=title:"train+OR+locomotive"&start=0&sort=id+desc&rows=5&wt=javabin&version=2} hits=32 status=0

The problem apparently is that by default, Spring Boot Solr evaluates the search term as a String and searches within the field for that String instead of what I really want, for example

q=title:train+OR+title:locomotive

I know how to write such a query using the solr api, but is there an easy way to construct a method in SolrCrudRepository to do it?

Is there a simple out-of-the-box method call that will accomplish boolean search, or do I need to parse a search string for boolean terms and write a custom query?

1

There are 1 answers

0
Bill T On

The methods one overrides when extending SolrCrudRepository are flexible, but in order to tap into the full functionality of the solr api you need to leverage SolrTemplate as described among other places here.