Slide 1

Slide 1 text

Spring Data Solr I n t r o d u c t i o n t o 2013-09-25, Christoph Strobl @eJUG, 48.299707,14.286896

Slide 2

Slide 2 text

Christoph Strobl Software Engineer Project Lead Spring Data Solr j73x73r@gmail.com @stroblchristoph https:/ /github.com/christophstrobl

Slide 3

Slide 3 text

Apache Solr ...in a hurry...

Slide 4

Slide 4 text

Apache Solr Enterprise Search Server Built on top of Lucene Open Interfaces Multiple Collections Clusterable ...

Slide 5

Slide 5 text

Infrastructure Setup Solr

Slide 6

Slide 6 text

ZK Infrastructure Setup S1L S1R S2L S2R

Slide 7

Slide 7 text

ZK Infrastructure Setup S1L S1R S2L S2R

Slide 8

Slide 8 text

Inside Solr Cores Schema

Slide 9

Slide 9 text

Solr Schema Types Analysers Fields

Slide 10

Slide 10 text

Schemaless since 4.4

Slide 11

Slide 11 text

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);

Slide 12

Slide 12 text

Solr Query public List 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(); }

Slide 13

Slide 13 text

Spring-Data-Solr

Slide 14

Slide 14 text

Spring-Data-Solr Spring Data offers serveral APIs around a common SPI Integration with Apache Solr Community Project with support from pivotal

Slide 15

Slide 15 text

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);

Slide 16

Slide 16 text

Solr Template

Slide 17

Slide 17 text

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 page = solrTemplate.queryForPage( new SimpleQuery(new SimpleStringCriteria("*:*"), Product.class); //... solrTemplate.deleteById("foo"); solrTemplate.commit();

Slide 18

Slide 18 text

Repository

Slide 19

Slide 19 text

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

Slide 20

Slide 20 text

Derived Queries

Slide 21

Slide 21 text

Derived Queries interface ProductRepository extends SolrCrudRepository { Page findByNameStartingWith(String name, Pageable page); List findByNameOrDescription(@Boost(2) String name, String description); } Page page = repo.findByNameStartingWith("fo", new PageRequest(0,10)); List list = repo.findByNameOrDescription("foo", "foo");

Slide 22

Slide 22 text

/*Named*/ Queries

Slide 23

Slide 23 text

/*Named*/ Queries interface ProductRepository extends SolrCrudRepository { @Query("name:?0*") Page findByNameStartingWith(String name, Pageable page); @Query(name="findByNameOrDescriptionBoostNameBy2") List findByNameOrDescription(String name, String description); }

Slide 24

Slide 24 text

Demo

Slide 25

Slide 25 text

Custom Repository

Slide 26

Slide 26 text

Faceting

Slide 27

Slide 27 text

Highlighting

Slide 28

Slide 28 text

Custom Types

Slide 29

Slide 29 text

Questions?

Slide 30

Slide 30 text

Thank you!