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.

B0e9230af37f3a883c88d3f11c00e174?s=128

Christoph Strobl

July 11, 2013
Tweet

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. Christoph Strobl Software Architect Consultant Project Lead Spring Data Solr

    christoph.strobl@pagu.at @stroblchristoph https:/ /github.com/christophstrobl
  3. Apache Solr ...in a hurry...

  4. Connection HTTP q=name:(spring data) AND type:solr&start=0&rows=10

  5. Connection HTTP SolrJ q=name:(spring data) AND type:solr&start=0&rows=10 new SolrQuery("name:(spring data)

    AND type:solr") .setStart(0).setRows(10);
  6. 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(); }
  7. Spring-Data-Solr

  8. Spring-Data-Solr Spring Data offers serveral APIs around a common SPI

    Integration with Apache Solr Community Project with support from Springsource
  9. 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);
  10. Solr Template

  11. SolrTemplate Product product = new Product("foo"); solrTemplate.saveBean(prodcut); solrTemplate.commit(); //...

  12. 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); //...
  13. 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();
  14. Repository

  15. Repository interface ProductRepository extends SolrCrudRepository<Product, String> { }

  16. Repository interface ProductRepository extends SolrCrudRepository<Product, String> { } Product product

    = new Product("foo"); repo.save(product) //...
  17. 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();
  18. 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);
  19. Derived Queries

  20. Derived Queries interface ProductRepository extends SolrCrudRepository<Product, String> { }

  21. Derived Queries interface ProductRepository extends SolrCrudRepository<Product, String> { Page<Product> findByNameStartingWith(String

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

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

    name, Pageable page); List<Product> findByNameOrDescription(@Boost(2) String name, String description); }
  24. /*Named*/ Queries

  25. /*Named*/ Queries interface ProductRepository extends SolrCrudRepository<Product, String> { Page<Product> findByNameStartingWith(String

    name, Pageable page); List<Product> findByNameOrDescription(@Boost(2) String name, String description); }
  26. /*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); }
  27. /*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); }
  28. Demo

  29. Custom Repository Demo

  30. Faceting Demo

  31. Highlighting Demo

  32. Custom Types Demo

  33. Thank you!