Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ SPRINGONE2GX WASHINGTON, DC Boot Your Search With Spring Christoph Strobl @stroblchristoph
Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ Agenda 2 1999 2004 2010 2014
Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ The Thing with Search • Simple Requirement 4 spring data
Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ The Thing with Search • Simple Requirement • Complex Solution 5
Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ The Thing with Search • Simple Requirement • Complex Solution • Abstraction 6 { fuzzy : { text : "spring" } }
Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ 7 “…you need to understand at least one level of abstraction beneath the one at which you're working. ! Neal Ford
Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ Spring Data Modules 9 Commons Neo4j Gemfire JPA Solr Elasticsearch REST Cassandra Couchbase Redis MongoDB Community modules Core modules Aerospike Hazelcast Crate Incubating KeyValue
Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ Spring Data • Low level Templates 12 template.execute(new DbCallback<CommandResult>() { @Override public CommandResult doInDB(DB db) { return db.command(new BasicDBObject("profile", 2)); } });
Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ Spring Data • Low level Templates • Repositories 13 interface BlogPostRepo extends CrudRepository<BlogPost, String> {} repository.save(blogPost); repository.findAll(); repository.findOne("S2GX-‐2015"); repository.delete(blogPost);
Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ Let’s get started… • Schema • Text Analysis • Score / Boost • Query vs. Filter Query 16
Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ Spring Data MongoDB - Text Index • Text Index 19 db.collection.ensureIndex( { title : "text", content : "text", categories : "text"} ) , { weights : { title : 3, content : 2 } }
Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ Spring Data MongoDB - Text Index • Text Index • Manual Index Creation 20 TextIndexDefinition textIndex = new TextIndexDefinitionBuilder() ! ! ! ! .onField("title", 3F) .onField("content", 2F) .onField("categories") .build(); operations.indexOps(BlogPost.class).ensureIndex(textIndex);
Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ Spring Data MongoDB - Text Index • Text Index • Manual Index Creation • Automatic Index Creation 21 @Document class BlogPost { @Id String id; @TextIndexed(weight = 3) String title; @TextIndexed(weight = 2) String content; @TextIndexed List<String> categories;
Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ Solr - Full Text Search • Requires a capture all field 28 ! <field name="_text_" type="text_general" indexed="true" stored="false" multiValued="true"/> <copyField source="*" dest="_text_"/>
Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ Spring Data Solr • Query Time Boosting 31 ! List<Product> findTop10ByNameOrDescription(@Boost(2) String name, String description); @Document class Product { @Id String id; //... @Score Float score;
Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ Spring Data Solr • Query Time Boosting • Result Highlighting 32 @Highlight(fragsize = 20, snipplets = 3) HighlightPage<Product> findByDescription(String description, Pageable page);
Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ Spring Data Solr • Query Time Boosting • Result Highlighting • Schema API 33 class Product { @Id String id; @Indexed(type = "text_general") String name; @Indexed(name = "cat", type = "string") List<String> category; @Indexed boolean inStock; }
Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ Spring Data Elasticsearch • Indexing 35 @Document(indexName = "publications", type = "book", shards = 1, ... class Book { @Id String id; ! ! ! ! ! PUT /publications/book/1 { "title": "Wheel of Time", ... } index type id @Document(indexName = "publications", type = "article", shards = 1, ... class Article { @Id String id;
Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ Spring Data Elasticsearch • Indexing • Type Mapping • Search API 37 CriteriaQuey cq = new CriteriaQuery(where("location").within(… ! ! ! ! SearchQuery sq = new NativeSearchQueryBuilder().withQuery(matchAllQuery()) .withFilter(boolFilter().must(termFilter("title", "spring"))).build();
Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ Spring Data Elasticsearch • Facets 39 SearchQuery query = new NativeSearchQueryBuilder() .withQuery(matchAllQuery()) .withFacet(new TermFacetRequestBuilder("categoriesFacet") .allTerms() .fields("categories") .descCount()); FacetPage<Article> page = template.queryForPage(query, Article.class);
Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ Spring Data Elasticsearch • Facets • Aggregation 40 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());
Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ 42 Learn More. Stay Connected. @springcentral Spring.io/video projects.spring.io/spring-data/ github.com/spring-projects/spring-data-examples !