Spring Data Apache Solr - Querying



The concept Query methods and Custom Query methods are easily accessible here because the repository associated with SolrCrudRepository works on top of PagingAndSortingRepository which in turns extends CrudRepository. Thus by default all the methods like save(), findOne(), findById(), findAll(), count(), delete(), deleteById() etc are accesible and can be used. Other than this it also has access to all the methods associated with Paging and sorting. Spring Data Apache Solr comes with a rich set of query approaches such as −

  • Method Name Query Generation

  • Custom Query With @Query Annotation

  • Named Query

Lets explore the above query technique defined by the Spring Data Solr API.

Method Name Query Generation

These are the usual query methods which get generated based on the methods name of the attribute of our domain object, such as −

List<Users> findByName(String name);

Custom Query with @Query Annotation

We can also create our search query using @Query annotation. Lets define a custom query and use @Query annotation.

@Query("id:*?0* OR name:*?0*")
public Page<Users> findByCustomQuery(String searchTerm, Pageable pageable);

The above custom query will fetch a record from the Solr database by performing a lookup on the id and name of a user. and it will return the results. Lets invoke this method and try fetching out the result −

usersRepository.findByCustomQuery("Kallis", PageRequest.of(0, 5)).forEach(S
ystem.out::println);

The above statement is saying to fetch a user based on the name Kallis, and obtain the first−page result with the size of max 5 records. The output of the above statement will be −

Customer [id=1, name=Kallis]

Named Query

This type of query is similar to Custom Query with @Query Annotation, except these queries are declared in a separate properties file. Lets create a properties file named namedQueries.properties(We can give any name) in parallel to application.properties file. Now lets add our first named query in that file.

Users.findByNamedQuery=id:*?0* OR name:*?0*

After adding the above file and query to that file our project structure will look like this.

Project Structure

Lets add this file information as class path in SolrConfig file. Add namedQueriesLocation = "classpath:namedQueries.properties" as attribute under @EnableSolrRepositories. Our updated SolrConfig file will be

import org.apache.solr.client.solrj.SolrClient;
import org.apache.solr.client.solrj.impl.HttpSolrClient;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.solr.core.SolrTemplate;
import org.springframework.data.solr.repository.config.EnableSolrRepositori
es;
@Configuration
@EnableSolrRepositories(basePackages = "com.tutorialspoint.repository", nam
edQueriesLocation = "classpath:namedQueries.properties")
@ComponentScan
public class SolrConfig {
   @Bean
   public SolrClient solrClient() {
      return new HttpSolrClient.Builder("http://localhost:8983/solr").bui
      ld();
   }
   @Bean
   public SolrTemplate solrTemplate(SolrClient client) throws Exception {
      return new SolrTemplate(client);
   }
}

Now, lets move to our repository and add a custom query to invoke this named query.

@Query(name = "Users.findByNamedQuery")
public Page<Users> findByNamedQuery(String searchTerm, Pageable pageable);

Note− @Query annotation is optional here and not required in case the qury method name findByNamedQuery matches with the query name used in properties file. Lets invoke above method and check the result.

usersRepository.findByNamedQuery("Wilson", PageRequest.of(0, 5)).forEach(Sy
stem.out::println);

The above statement will return result of first page with at most 5 records, as follows −

Customer [id=3, name=Wilson Monk]
Advertisements