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

Boot your Search with Spring

Boot your Search with Spring

Christoph Strobl

April 29, 2015
Tweet

More Decks by Christoph Strobl

Other Decks in Programming

Transcript

  1. Unless otherwise indicated, these slides are © 2013-2015 Pivotal Software,

    Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ 2 Agenda 1999 2004 2010 2014
  2. Unless otherwise indicated, these slides are © 2013-2015 Pivotal Software,

    Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ 3 The Thing with Search
  3. Unless otherwise indicated, these slides are © 2013-2015 Pivotal Software,

    Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ The Thing with Search § Simple Requirement spring data
  4. § Simple Requirement sprung data Unless otherwise indicated, these slides

    are © 2013-2015 Pivotal Software, Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ The Thing with Search
  5. Unless otherwise indicated, these slides are © 2013-2015 Pivotal Software,

    Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ 6 The Thing with Search § Simple Requirement § Complex Solution
  6. Unless otherwise indicated, these slides are © 2013-2015 Pivotal Software,

    Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ 7 The Thing with Search § Simple Requirement § Complex Solution § Abstraction {
    fuzzy  :  {  text  :  "sprung"  }
 }
  7. Unless otherwise indicated, these slides are © 2013-2015 Pivotal Software,

    Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ 8 The Thing with Search “…you need to understand at least one level of abstraction beneath the one at which you're working. ! Neal Ford
  8. Unless otherwise indicated, these slides are © 2013-2015 Pivotal Software,

    Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ 9 Basics Spring Data
  9. Unless otherwise indicated, these slides are © 2013-2015 Pivotal Software,

    Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ The Goal… “…provide a familiar and consistent Spring based programming model retaining store specific features and capabilities” (O.Gierke) 10
  10. Unless otherwise indicated, these slides are © 2013-2015 Pivotal Software,

    Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ Spring Data Modules 11 Commons Neo4j Gemfire JPA Solr ElasticSearch REST Cassandra Couchbase Redis MongoDB Community
 modules Core
 modules Aerospike Hazelcast Crate Incubating
  11. Unless otherwise indicated, these slides are © 2013-2015 Pivotal Software,

    Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ Release train 12 03/2015 Fowler 03/2013 Arora 09/2013 Babbage 02/2014 Codd 05/2014 Dijkstra 09/2014 Evans 09/2015 Gosling* image source: wikipedia CC:BY
  12. Unless otherwise indicated, these slides are © 2013-2015 Pivotal Software,

    Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ Spring Data Release train BOM <dependency> <groupId>org.springframework.data</groupId> <artifactId>spring-data-releasetrain</artifactId> <version>Fowler-RELEASE</version> <scope>import</scope> <type>pom</type> </dependency> 13 <dependency> <groupId>org.springframework.data</groupI <artifactId>spring-data-rest-webmvc</arti </dependency> <dependency> <groupId>org.springframework.data</groupI <artifactId>spring-data-mongodb</artifactI <dependencyManagement>
 <dependencies>
  13. Unless otherwise indicated, these slides are © 2013-2015 Pivotal Software,

    Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ 14 Spring Data § Low level Templates template.execute(new  DbCallback<CommandResult>()  {      @Override      public  CommandResult  doInDB(DB  db)  {          return  db.command(new  BasicDBObject("profile",  2));      }   });
  14. Unless otherwise indicated, these slides are © 2013-2015 Pivotal Software,

    Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ 15 Spring Data § Low level Templates § Repositories interface  BlogPostRepo  extends  CrudRepository<BlogPost,  String>  {} repository.save(blogPost);   repository.findAll();   repository.findOne("SpringIO-­‐2015");   repository.delete(blogPost);
  15. Unless otherwise indicated, these slides are © 2013-2015 Pivotal Software,

    Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ 16 Spring Data § Low level Templates § Repositories § Derived Methods interface  BlogPostRepo  extends  CrudRepository<BlogPost,  String>  {
    List<BlogPost>  findByTitle(String  title);      Page<BlogPost>  findByTitle(String  title,  Pageable  page);      void  deleteByAuthor(Author  author);      Stream<BlogPost>  findBlogPostBy();     }
  16. Unless otherwise indicated, these slides are © 2013-2015 Pivotal Software,

    Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ 17 Basics Spring Boot
  17. Unless otherwise indicated, these slides are © 2013-2015 Pivotal Software,

    Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ 18 Spring Boot …lets you pair program with the Spring team. Josh Long source: https://twitter.com/rob_winch/status/364871658483351552
  18. Unless otherwise indicated, these slides are © 2013-2015 Pivotal Software,

    Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ 19 Spring Boot Keeps you focused Minimal ramp up time Batteries included Gets out of the way quickly Only for prototyping Just an embedded container Beginners only Bad experience
  19. Unless otherwise indicated, these slides are © 2013-2015 Pivotal Software,

    Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ 20 $text $search your Documents Spring Data
  20. Unless otherwise indicated, these slides are © 2013-2015 Pivotal Software,

    Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ 21 Spring Data MongoDB - Text Index § Text Index db.collection.ensureIndex(      {  title  :  "text",  content  :  "text",  categories  :  "text"}       )                                                                                                                    ,      {  weights  :  {  title  :  3,  content  :  2  }  }
  21. Unless otherwise indicated, these slides are © 2013-2015 Pivotal Software,

    Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ 22 Spring Data MongoDB - Text Index § Text Index § Manual Index Creation TextIndexDefinition  textIndex  =  new  TextIndexDefinitionBuilder()       ! ! ! !    .onField("title",  3F)      .onField("content",  2F)      .onField("categories")      .build();   operations.indexOps(BlogPost.class).ensureIndex(textIndex);
  22. Unless otherwise indicated, these slides are © 2013-2015 Pivotal Software,

    Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ 23 Spring Data MongoDB - Text Index § Text Index § Manual Index Creation § Automatic Index Creation @Document   class  BlogPost  {      @Id  String  id;      @TextIndexed(weight  =  3)  String  title;      @TextIndexed(weight  =  2)  String  content;      @TextIndexed  List<String>  categories;
  23. Unless otherwise indicated, these slides are © 2013-2015 Pivotal Software,

    Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ 24 Spring Data MongoDB - Text Query § Querying TextCriteria  criteria  =  forDefaultLanguage().matching("release");   TextQuery  query  =  query(criteria);   ! ! ! List<BlogPost>  blogPosts  =  operations.find(query,  BlogPost.class); query.addCriteria(where("categories").in("Releases"));
  24. Unless otherwise indicated, these slides are © 2013-2015 Pivotal Software,

    Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ 25 Spring Data MongoDB - Text Query § Querying § Scoring Results TextCriteria  criteria  =  forDefaultLanguage().matching("release");   TextQuery  query  =  query(criteria);   ! ! ! List<BlogPost>  blogPosts  =  operations.find(query,  BlogPost.class); ! query.setScoreFieldName("score");   query.sortByScore(); @Document   class  BlogPost  {      @Id  String  id;      //...      @ReadonlyProperty  Float  score;            
  25. Unless otherwise indicated, these slides are © 2013-2015 Pivotal Software,

    Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ 26 Spring Data MongoDB - Text Query § Querying § Scoring Results § Derived queries interface  BlogPostRepo  extends  CrudRepository<BlogPost,  String>  {          List<BlogPost>  findAllBy(TextCriteria  criteria);   ! ! } @Document   class  BlogPost  {      @Id  String  id;      //...      @TextScore  Float  score;                              List<BlogPost>  findAllByOrderByScoreDesc(TextCriteria  criteria);
  26. Demo Unless otherwise indicated, these slides are 
 © 2013-2015

    Pivotal Software, Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ Spring Data MongoDB
  27. Unless otherwise indicated, these slides are © 2013-2015 Pivotal Software,

    Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ 28 Spring Data
  28. Unless otherwise indicated, these slides are © 2013-2015 Pivotal Software,

    Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ 29 Spring Data Solr - Solr Schema § Schema <?xml version="1.0" encoding="UTF-8" ?> <schema name="example" version="1.5"> <fields> <field name="id" type="string" indexed="true" stored="true" required="true" multiValued="false" /> <field name="name" type="text_general" indexed="true" stored="true"/> <field name="cat" type="string" indexed="true" stored="true" multiValued="true"/> <field name="features" type="text_general" indexed="true" stored="true" multiValued="true"/> <field name="weight" type="float" indexed="true" stored="true"/> <field name="price" type="float" indexed="true" stored="true"/> <field name="popularity" type="int" indexed="true" stored="true" /> <field ... <fieldType name="text_general" class="solr.TextField" positionIncrementGa.. <analyzer type="index"> <tokenizer class="solr.StandardTokenizerFactory"/> <filter class="solr.StopFilterFactory" ignoreCase="true" words=“stop..
 <filter class="solr.LowerCaseFilterFactory"/> </analyzer> <analyzer type="query"> <tokenizer class="solr.StandardTokenizerFactory"/> <filter class="solr.StopFilterFactory" ignoreCase="true" words=“stopwor.. <filter class="solr.SynonymFilterFactory" synonyms="synonyms.txt" igno… <filter class="solr.LowerCaseFilterFactory"/> </analyzer> </fieldType>
  29. Unless otherwise indicated, these slides are © 2013-2015 Pivotal Software,

    Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ 30 Spring Data Solr - Solr Schema § Schema § Managed Schema <?xml version="1.0" encoding="UTF-8" ?> <config> <schemaFactory class="ManagedIndexSchemaFactory"> <bool name="mutable">true</bool> <str name="managedSchemaResourceName">managed-schema</str> </schemaFactory> <...
  30. Unless otherwise indicated, these slides are © 2013-2015 Pivotal Software,

    Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ 31 Spring Data Solr § Query Time Boosting ! List<Product>  findTop10ByNameOrDescription(@Boost(2)  String  name,  
                                                                                      String  description); @Document   class  Product  {      @Id  String  id;      //...      @Score  Float  score;    
  31. Unless otherwise indicated, these slides are © 2013-2015 Pivotal Software,

    Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ 32 Spring Data Solr § Query Time Boosting § Result Highlighting @Highlight(fragsize  =  20,  snipplets  =  3)   HighlightPage<Product>  findByDescription(String  description,  Pageable  page);
  32. Unless otherwise indicated, these slides are © 2013-2015 Pivotal Software,

    Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ 33 Spring Data Solr § Query Time Boosting § Result Highlighting § Schema API class  Product  {      @Id  String  id;      @Indexed(type  =  "text_general")  String  name;      @Indexed(name  =  "cat",  type  =  "string")  List<String>  category;      @Indexed  boolean  inStock;   }
  33. Demo Unless otherwise indicated, these slides are 
 © 2013-2015

    Pivotal Software, Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ Spring Data Solr
  34. Unless otherwise indicated, these slides are © 2013-2015 Pivotal Software,

    Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ 35 ! ! Spring Data
  35. Unless otherwise indicated, these slides are © 2013-2015 Pivotal Software,

    Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ 36 Spring Data Elasticsearch § Indexing @Document(indexName  =  "publications",  type  =  "article",  shards  =  1,  ...   class  Article  {      @Id  String  id;   ! @Document(indexName  =  "publications",  type  =  "book",  shards  =  1,  ...   class  Book  {      @Id  String  id;
  36. Unless otherwise indicated, these slides are © 2013-2015 Pivotal Software,

    Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ 37 Spring Data Elasticsearch § Indexing § Type Mapping class  Article  {      @Id  String  id;      String  name;      @Field(format  =  DateFormat.date,  type=Date)  String  date;      @GeoPointField  String  location      @MultiField(mainField  =  @Field(type  =  String,  index  =  analyzed),
                            otherFields  =  {@NestedField(dotSuffix  =  "sort",  indexAnalyzer  =  "keyword"
    List<String>  authors;  
  37. Unless otherwise indicated, these slides are © 2013-2015 Pivotal Software,

    Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ 38 Spring Data Elasticsearch § Indexing § Type Mapping § Search API CriteriaQuey  cq  =  new  CriteriaQuery(where("location").within(…
 ! SearchQuery  sq  =  new  NativeSearchQueryBuilder().withQuery(matchAllQuery())      .withFilter(boolFilter().must(termFilter("id",  documentId))).build();  
  38. Unless otherwise indicated, these slides are © 2013-2015 Pivotal Software,

    Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ 39 Spring Data Elasticsearch § Facets SearchQuery  query  =  new  NativeSearchQueryBuilder()
    .withQuery(matchAllQuery())
    .withFacet(new  TermFacetRequestBuilder("categoriesFacet")
        .allTerms()
        .fields("categories")
        .descCount());   FacetPage<Article>  page  =  template.queryForPage(query,  Article.class);
  39. Unless otherwise indicated, these slides are © 2013-2015 Pivotal Software,

    Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ 40 Spring Data Elasticsearch § Facets   § Aggregation SearchQuery  query  =  new  NativeSearchQueryBuilder()
    .withQuery(matchAllQuery())
    .withSearchType(SearchType.COUNT)
    .withTypes("article")
    .addAggregation(terms("subjects").field("subject"))
    .build()   Aggregations  aggregations  =  template.queryForPage(query,
    response  -­‐>  response.getAggregations());
  40. Demo Unless otherwise indicated, these slides are 
 © 2013-2015

    Pivotal Software, Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ Spring Data Elasticsearch
  41. Unless otherwise indicated, these slides are © 2013-2015 Pivotal Software,

    Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ Where’s the code? 42
  42. Unless otherwise indicated, these slides are © 2013-2015 Pivotal Software,

    Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ More Boot, Spring Data, MongoDB, Neo4j, Elasticsearch…. 
 TODAY § 15:00 Master Spring Boot auto-configuration (Stéphane Nicoll) § 16:30 Spring Boot is made for tooling (Yann Cébron & Stéphane Nicoll) ! TOMORROW § 10:00 MongoDB and Spring – Two leaves of a same tree (Norberto Leite) § 11:30 Spring Data REST – Repositories meet hypermedia (Oliver Gierke) § 12:30 Building High Performance Applications with Spring Data Neo4j 4.0 (Michael Hunger & Vince Bickers) § 14:30 Workshop Building “Bootiful” Microservices with Spring Cloud (Josh Long) § 15:30 Scaling real time search and analytics with Elasticsearch (Clinton Gormley) 43
  43. Unless otherwise indicated, these slides are © 2013-2015 Pivotal Software,

    Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ 44 Learn More. Stay Connected. Twitter: twitter.com/springcentral YouTube: spring.io/video LinkedIn: spring.io/linkedin Google Plus: spring.io/gplus