3. Miscellaneous Solr Operation Support

Abstract

This chapter covers additional support for Solr operations (such as faceting) that cannot be directly accessed via the repository interface. It is recommended to add those operations as custom implementation as described in Section 1.4, “Custom implementations” .

3.1 Partial Updates

PartialUpdates can be done using PartialUpdate which implements Update .

[Note]Note
Partial updates require Solr 4.x. With Solr 4.0.0 it is not possible to update mulitvalue fields.

Example 3.1. 

PartialUpdate update = new PartialUpdate("id", "123");
update.add("name", "updated-name");
solrTemplate.saveBean(update);

3.2 Projection

Projections can be applied via @Query using the fields value.

Example 3.2. 

@Query(fields = { "name", "id" })
List<ProductBean> findByNameStartingWith(String name);

3.3 Faceting

Faceting cannot be directly applied using the SolrRepository but the SolrTemplate holds support for this feature.

Example 3.3. 

FacetQuery query = new SimpleFacetQuery(new Criteria(Criteria.WILDCARD).expression(Criteria.WILDCARD))
  .setFacetOptions(new FacetOptions().addFacetOnField("name").setFacetLimit(5));
FacetPage<Product> page = solrTemplate.queryForFacetPage(query, Product.class);

Facets on fields and/or queries can also be defined using @Facet . Please mind that the result will be a FacetPage .

@Query(value = "*:*")
@Facet(fields = { "name" }, limit = 5)
FacetPage<Product> findAllFacetOnPopularity(Pageable page);

3.4 Filter Query

Filter Queries improve query speed and do not influence document score. It is recommended to implement geospatial search as filter query.

[Note]Note
Please note that in solr, unless otherwise specified, all units of distance are kilometers and points are in degrees of latitude,longitude.

Example 3.4. 

Query query = new SimpleQuery(new Criteria("category").is("supercalifragilisticexpialidocious"));
FilterQuery fq = new SimpleFilterQuery(new Criteria("store")
  .near(new GeoLocation(48.305478, 14.286699), new Distance(5)));
query.addFilterQuery(fq);

Simple filter queries can also be defined using @Query .

@Query(value = "*:*", filters = { "inStock:true", "popularity:[* TO 3]" })
List<Product> findAllFilterAvailableTrueAndPopularityLessThanEqual3();