Hibernate Books

All times are UTC - 5 hours [ DST ]

Post new topic Reply to topic  [ 1 post ] 
Author Message
 Post subject: Setting Facets via a Field Bridge
PostPosted: Sat Feb 27, 2016 1:53 am 

Joined: Sat Feb 27, 2016 1:47 am
Posts: 14
Cross posting from Stack Overflow: http://stackoverflow.com/questions/3564 ... eld-bridge

In Hibernate Search 5.3 the @Facet annotation was introduced and must be used to store a field as a Facet. What I'm wondering is if their is a way to add a field as a facet in a custom field bridge, without using the @Facet annotation.

My use case is that I have a products with categories, and the category tree path is stored as a string like so


I want to be able to get the counts of all products in a category using facets. The count of a category should include the counts of all sub-categories, hence why each product should have a facet for every parent category

So what I'd like to do is something like:

public class CategoryPathFacetFieldBridge implements FieldBridge {
    public void set(String name, Object value, Document document, LuceneOptions luceneOptions) {
        String[] pathHolder = ((String)value).split("/");
        for (String id : pathHolder) {
            Field fieldForFacet = new Field(name, id, Field.Store.YES, Field.Index.NOT_ANALYZED);

I have actually come up with a workaround, but my preferred method would be to use a custom field bridge as above. Does anyone know if this is possible in hibernate search 5.3+ ?

My workaround looks like so:

public void setCategoryPath(String categoryPath) {
    this.categoryPath = categoryPath;
    String[] pathHolder = categoryPath.split("/");
    for (String id : pathHolder) {
        this.categoryPathList.add(new CategoryWrap(id));

public List<CategoryWrap> getCategoryPathList() {
    return this.categoryPathList;

class CategoryWrap{

    @Field(analyze = Analyze.NO)
    @Facet(encoding = FacetEncodingType.STRING)
    String categoryId;

    public CategoryWrap(String categoryId) {
        this.categoryId = categoryId;

Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 1 post ] 

All times are UTC - 5 hours [ DST ]

You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.