Hibernate Search Faceting

With the document available from Hibernate Search (https://docs.jboss.org/hibernate/stable/search/reference/en-US/html_single/#query-faceting), nosotros should live able to implement a faceting illustration that returns the faceting champaign together with its count. In the illustration from Hibernate Search, our entity is CD, nosotros utilisation a facet on label field, together with thus it volition grouping the CD past times label, together with count all of each occurrence. But what if nosotros wish to a greater extent than information similar the artist, sales, etc.

Going dorsum to the previous illustration from hibernate where nosotros accept the next entities (Book, Author, Review). Let's country nosotros wish to grouping the books past times author, how should nosotros hit that?


  1. Annotate Author.id alongside @facet.
    @Facets({ @Facet, @Facet(name = "id_facet", forField = "id_for_facet", encoding = FacetEncodingType.STRING) }) @Fields({ @Field(name = "id_for_facet", analyze = Analyze.NO, twosome = @FieldBridge(impl = org.hibernate.search.bridge.builtin.IntegerBridge.class)) }) @Id @Column(name = "id") @GeneratedValue mortal Integer id; 
  2. Create a shape that volition concur the desired entity together with the facet result.
    public shape EntityFacet implements Facet {  mortal lastly Facet delegate;  mortal lastly T entity;   populace EntityFacet(Facet delegate, T entity) {   this.delegate = delegate;   this.entity = entity;  }   @Override  populace String getFacetingName() {   provide delegate.getFacetingName();  }   @Override  populace String getFieldName() {   provide delegate.getFieldName();  }   @Override  populace String getValue() {   provide delegate.getValue();  }   @Override  populace int getCount() {   provide delegate.getCount();  }   @Override  populace Query getFacetQuery() {   provide delegate.getFacetQuery();  }   populace T getEntity() {   provide entity;  }   @Override  populace String toString() {   provide "EntityFacet [delegate=" + delegate + ", entity=" + entity + "]";  } } 
  3. Let's query the facet together with the entity that contains to a greater extent than of the data nosotros want. In this instance the author. Note that nosotros bespeak to add together the faceted champaign inward the includePaths holding inward the @IndexedEmbedded musical note of the master copy entity (Book), or don't specify a champaign together with thus all annotated fields are included.
    FullTextEntityManager fullTextEntityManager = Search.getFullTextEntityManager(em); QueryBuilder qb = fullTextEntityManager.getSearchFactory().buildQueryBuilder().forEntity(Book.class).get();  org.apache.lucene.search.Query luceneQuery = qb.all().createQuery(); FullTextQuery fullTextQuery = fullTextEntityManager.createFullTextQuery(luceneQuery, Book.class);  // define the facet FacetingRequest authorFacet = qb.facet().name("authorIdFacet").onField("authors.id_facet").discrete()   .orderedBy(FacetSortOrder.COUNT_DESC).includeZeroCounts(false).maxFacetCount(5).createFacetingRequest();  // shout upwards facet managing director together with use faceting asking FacetManager facetManager = fullTextQuery.getFacetManager(); facetManager.enableFaceting(authorFacet);  // shout upwards the faceting results List<Facet> facets = facetManager.getFacets("authorIdFacet");  // collect all the ids List<Integer> vcIds = facets.stream().map(p -> Integer.parseInt(p.getValue())).collect(Collectors.toList()); // query all the Authors given the id nosotros faceted above, I intend multiLoad has // been introduced inward HS 5.x List<Author> authors = fullTextEntityManager.unwrap(Session.class).byMultipleIds(Author.class).multiLoad(vcIds);  // fill upwards our container object alongside the facet together with writer entity List<EntityFacet<Author>> entityFacets = novel ArrayList<>(facets.size()); for (int ane = 0; ane < facets.size(); i++) {  entityFacets.add(new EntityFacet<Author>(facets.get(i), authors.get(i))); }  entityFacets.stream().forEach(System.out::println);
For code reference yous may cheque this repository: https://github.com/czetsuya/hibernate-search-demo

Got a question? Don't hesitate to inquire :-)
Next
Previous
Click here for Comments

0 komentar:

Please comment if there are any that need to be asked.