Smart Serach Filter - Category Drop Down

Tracey Penberthy asked on April 3, 2017 18:20

Hi

I am trying to create a custom web part that contains a categories smart search filter but I can't seem to get it to work.

Here's my code to set up the filter (subcats is a list of CategoryInfo):

cmsCategorySearchFilter.SearchWebpartID = dialogWebPartID;
cmsCategorySearchFilter.FilterMode = SearchFilterModeEnum.DropdownList;
cmsCategorySearchFilter.FilterAutoPostback = true;
StringBuilder catFilterValues = new StringBuilder();
catFilterValues.Append(";;All\n");
foreach (var s in subcats)
{
    catFilterValues.Append("documentcategories;" + s.CategoryDisplayName + ";" + s.CategoryDisplayName + "\n");
}
cmsCategorySearchFilter.FilterValues = catFilterValues.ToString();
cmsCategorySearchFilter.FilterClause = "Must";
cmsCategorySearchFilter.FilterIsConditional = false;

I suspect my filter value syntax is wrong but I can't find any examples. Anyone know what it should be?

Many Thanks Tracey

Recent Answers


Peter Mogilnitski answered on April 3, 2017 21:38 (last edited on April 3, 2017 22:23)

Assuming you want to create a category filer for smart search.You don't need a web part. What you are trying to accomplish can be done without it.

I use checkbox list as example, but you can render drop down or radio button list or whatever you want. It will be even more simple. Create a TEXT/XML transformation under for example Root page type, call it CategoryCheckboxList:

<input type="checkbox" name="CategoryID" id="cat{%CategoryID%}" value="{%CategoryID%}"><label for="cat{%CategoryID%}">{%CategoryDisplayName%}</label><br/>

Now. Go to your page and add static text web part and put macro inside: {% GlobalObjects.Categories.Where("CategoryParentID = 1111").ApplyTransformation("CMS.Root.CategoryCheckboxList")#%}.

Now you should get list of check boxes on your page. Use jQuery to reload the page with categoryid= when a user clicks a checkbox so your the query string will be ?CategoryId=123,456.

Now add smart search results web part to your page (check "Search on each page load") and in search condition you should form something likes this: +documentcategoryids:("123"||"456") if your query string ?CategoryId=123,456. For drop down or radio button list the condition is even more simple, you always have one category: +documentcategoryids: (int){%QueryString("CategoryID")%}

This is just a general idea. Smart search filter is limited in terms of layout and design. I can share more if you are interested.

0 votesVote for this answer Mark as a Correct answer

Trevor Fayas answered on April 4, 2017 00:30

The Kentico Smart Search is Lucene.net, so you can look at the format here.

Before you even build your filter, make sure you have the right syntax. You can do this by rendering the Smart Search with a "Filter" in the web part, using Lucene syntax.

Looking at your code, one issue is you may need to add the + if you're trying to say "any result MUST have this category." You should also make sure "Include categories" is checked in your Smart Search Index's Allowed content.

Additionally though you shouldn't need a custom web part, there is already a Smart Search Filter that you can configure to get the Categories.

0 votesVote for this answer Mark as a Correct answer

Tracey Penberthy answered on April 4, 2017 10:38

Thanks for your responses Peter and Trevor, it's been really helpful

I got my filter working by adding the '+' to the filter value and removing the following

cmsCategorySearchFilter.FilterClause = "Must";
cmsCategorySearchFilter.FilterIsConditional = false;

As you both brought it up I should just mention that the reason I have a smart filter in a custom web part is to create a document listing widget that is searchable and filterable. It contains a smart search dialog, smart search filter and smart search results control. This is so that content editors can add the widget to a page (this isn't possible just using the controls themselves as the web part ids that are required e.g. SearchWebpartID for the smart search filter and ResultWebpartID for the smart search dialog control are not accessible in widgets)

0 votesVote for this answer Mark as a Correct answer

   Please, sign in to be able to submit a new answer.