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

Christoph Strobl on spring-data-solr

Christoph Strobl on spring-data-solr

More Decks by Enterprise Java User Group Austria

Other Decks in Technology

Transcript

  1. 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
  2. Apache Solr Enterprise Search Server Built on top of Lucene

    Open Interfaces Multiple Collections Clusterable ...
  3. Solr Schema Types Analysers Fields <fieldType name="string" class="solr.StrField" sortMissingLast="true" />

    <fieldType name="lowercase" class="solr.TextField"> <analyzer> <tokenizer class="solr.KeywordTokenizerFactory"/> <filter class="solr.LowerCaseFilterFactory" /> </analyzer> </fieldType> <field name="name_ci" type="lowercase" indexed="true" stored="false"/>
  4. 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(); }
  5. Spring-Data-Solr Spring Data offers serveral APIs around a common SPI

    Integration with Apache Solr Community Project with support from pivotal
  6. 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);
  7. 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();
  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(@Boost(2) String name, String description); } Page<Product> page = repo.findByNameStartingWith("fo", new PageRequest(0,10)); List<Product> list = repo.findByNameOrDescription("foo", "foo");
  10. /*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); }