cancel
Showing results for 
Search instead for 
Did you mean: 
Read only

how to get only sub categories in category facet?

Accepted Solutions (0)

Answers (2)

Answers (2)

Former Member
0 Likes

ok, I little tricky to do because solr does not support relations like any relational DB would do. create a field called category hierarchy and pass the entire category hierarchy as json in a single field.

For example, you have a product which is under Bags, while doing solr indexing, create a value provider that pushes the whole category hierarchy as JSON to solr.

sample json will be { "categoryName": "Bags" subcategories: { "Linen Bags", "Leather Bags", "Plastic Bags" } }

when creating facets, retrieve the JSON from categoryHierarchy field and convert it into Java object

 public class CategoryHierarchy {
    private String categoryName;
    private List<String> subCategories;

    public String getCategoryName() {
         return this.categoryName;
    }
 }

and do

 CategoryHierarchy ch = convertFromJson(categoryHierarchySolrField); <br>
 if(categorySearchedFor.equalsIgnoreCase(ch.getCategoryName())) {
    createFacet()
 } 
 else {
   // do nothing.
 }

See google GSON api for converting between Java & Json. it is very lightweight API.

Former Member
0 Likes

Hi . Can you please elaborate ? If you have a category structure like below Categories -> Women -> Accessories -> Bags -> Bag-Product

What do you wish to display as a facet ?

Note: Bag-Product is a product here.

0 Likes

Hi when i search for a category it should return sub categories of that category in category facet. Suppose if i search for Accessories it should return only sub-categories of Accessories.

Actually solr returns facets based on indexed products, some products mapped to multiple products, so when search for a category it returns other subcategories also.

if Bag-Product mapped to bags and belts, when i click on bags category page i am getting belts also in fecet.