Cross posting from Stack Overflow:
http://stackoverflow.com/questions/3564 ... eld-bridgeIn 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
Code:
"0/10/100/1000"
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:
Code:
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);
document.add(fieldForFacet);
}
}
}
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:
Code:
public void setCategoryPath(String categoryPath) {
this.categoryPath = categoryPath;
String[] pathHolder = categoryPath.split("/");
for (String id : pathHolder) {
this.categoryPathList.add(new CategoryWrap(id));
}
}
@Transient
@IndexedEmbedded
@OneToMany
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;
}
}