Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Introduction to Spring Data Solr @SearchMUC

Introduction to Spring Data Solr @SearchMUC

Short introduction to core concepts of Spring Data and Spring Data Solr at the Search Usergroup Munich.

Christoph Strobl

July 11, 2013
Tweet

More Decks by Christoph Strobl

Other Decks in Technology

Transcript

  1. Spring Data Solr I n t r o d u

    c t i o n t o 2013-07-11, Christoph Strobl @SearchMUC, 48.144728,11.491141
  2. Solr Query public List<Product> findByNameOrDescription(String name, String description) { String

    queryString = "name:" + ClientUtils.escapeQueryChars(name) + " OR description:" + ClientUtils.escapeQueryChars(description); try { SolrQuery query = new SolrQuery(queryString); query.setStart(0).setRows(0); QueryResponse response = solrServer.query(query); long totalValues = response.getResults().getNumFound(); query.setRows(Long.valueOf(totalValues).intValue()); return solrServer.query(query).getBeans(Product.class); } catch (SolrServerException e) { throw ExceptionTranslator.translate(e); } return Collections.emptyList(); }
  3. Spring-Data-Solr Spring Data offers serveral APIs around a common SPI

    Integration with Apache Solr Community Project with support from Springsource
  4. Connection HTTP SolrJ Spring Data Solr new SimpleQuery(new Criteria("name") .is("spring",

    "data").and("type").is("solr") ).setPageRequest(new PageRequest(0, 10)); q=name:(spring data) AND type:solr&start=0&rows=10 new SolrQuery("name:(spring data) AND type:solr") .setStart(0).setRows(10);
  5. SolrTemplate Product product = new Product("foo"); solrTemplate.saveBean(prodcut); solrTemplate.commit(); //... Product

    recalled = solrTemplate.queryForObject( new SimpleQuery(new Criteria("id").is("foo")), Product.class); Page<Product> page = solrTemplate.queryForPage( new SimpleQuery(new SimpleStringCriteria("*:*"), Product.class); //...
  6. SolrTemplate Product product = new Product("foo"); solrTemplate.saveBean(prodcut); solrTemplate.commit(); //... Product

    recalled = solrTemplate.queryForObject( new SimpleQuery(new Criteria("id").is("foo")), Product.class); Page<Product> page = solrTemplate.queryForPage( new SimpleQuery(new SimpleStringCriteria("*:*"), Product.class); //... solrTemplate.deleteById("foo"); solrTemplate.commit();
  7. Repository interface ProductRepository extends SolrCrudRepository<Product, String> { } Product product

    = new Product("foo"); repo.save(product) //... Product recalled = repo.findOne("foo"); Page<Product> page = repo.findAll();
  8. Repository interface ProductRepository extends SolrCrudRepository<Product, String> { } Product product

    = new Product("foo"); repo.save(product) //... Product recalled = repo.findOne("foo"); Page<Product> page = repo.findAll(); //... repo.delete(product);
  9. Derived Queries interface ProductRepository extends SolrCrudRepository<Product, String> { Page<Product> findByNameStartingWith(String

    name, Pageable page); List<Product> findByNameOrDescription(String name, String description); }
  10. Derived Queries interface ProductRepository extends SolrCrudRepository<Product, String> { Page<Product> findByNameStartingWith(String

    name, Pageable page); List<Product> findByNameOrDescription(@Boost(2) String name, String description); }
  11. /*Named*/ Queries interface ProductRepository extends SolrCrudRepository<Product, String> { Page<Product> findByNameStartingWith(String

    name, Pageable page); List<Product> findByNameOrDescription(@Boost(2) String name, String description); }
  12. /*Named*/ Queries interface ProductRepository extends SolrCrudRepository<Product, String> { @Query("name:?0*") Page<Product>

    findByNameStartingWith(String name, Pageable page); List<Product> findByNameOrDescription(@Boost(2) String name, String description); }
  13. /*Named*/ Queries interface ProductRepository extends SolrCrudRepository<Product, String> { @Query("name:?0*") Page<Product>

    findByNameStartingWith(String name, Pageable page); @Query(name="findByNameOrDescriptionBoostNameBy2") List<Product> findByNameOrDescription(String name, String description); }