Kentico smart search by category

Targutai Yesugei asked on June 13, 2017 19:27

Hello!

I want to be able to search site content by category name, and created search index, which is more or less working. There are two problems i encountered:

  1. I specified path, and marked only "Include categories" in created index, so i expect Kentico will search only by category name, but it displays pages with occurences in title. What should i do to fix it?

  2. While developing .ascx file, i wrote something like this:

     protected void Page_Load(object sender, EventArgs e)
     {
        SearchResultList.Indexes = SmartSearchIndexes.CategorySearch;
    
        var childCategories = CategoryInfoProvider.GetChildCategories(123, siteId: 1).AsEnumerable();
    
        categoryProductSearchFilter.FilterMode = SearchFilterModeEnum.DropdownList;
        categoryProductSearchFilter.FilterAutoPostback = true;
        var categoryFilterValues = new StringBuilder();
        categoryFilterValues.Append(";;All\n");
        foreach (var childCategory in childCategories)
            categoryFilterValues.Append("documentcategories;" + childCategory.CategoryDisplayName + ";"                 + childCategory.CategoryDisplayName + "\n");
    
        categoryProductSearchFilter.FilterValues = categoryFilterValues.ToString();
        categoryProductSearchFilter.FilterClause = "+"; 
    }
    

    Should i somehow pass query from dropdown list or kentico do this by default? Currently, i've got nulls in SearchContext.CurrentParameters, SearchContext.CurrentSearchResults, SearchContext.CurrentSearchDocuments while debugging.

    Or (which is more possible) i need an example for a working filter, because i can't find any:c

Recent Answers


Mike Wills answered on June 13, 2017 21:50

Hi Targutai,

It looks like the category name is being used to execute a content search, but what you really want is a field search. Here's information on Lucene's search field syntax. https://lucene.apache.org/core/2_9_4/queryparsersyntax.html#Fields

So if you are searching on two category names, instead of this: +Category1 +Category2

You'd want the search syntax to be something like this: Categories:("Category1" "Category2")

You may need to double-check what fields are actually in the Lucene index. I'm not sure Categories is the right name. You can use the tool Luke to open the Smart Search index and examine its contents. http://www.getopt.org/luke/

Mike

0 votesVote for this answer Mark as a Correct answer

Roman Hutnyk answered on June 13, 2017 23:08

It sounds like you need just category filter vs. full search. If so, this could be easily implemented with repeater or any other listing web part by passing category into it.

0 votesVote for this answer Mark as a Correct answer

Targutai Yesugei answered on June 14, 2017 05:55

Hello, Mike! Not sure, that i need field search, I need to get all pages which have categoryname from query. Anyway, thx for advice, i'll check it.

Roman, sounds sweet, but i also need a dropdown list with category names, and i want to get it as easily as i can (out of box better): that's why i used this filter - one line of code created it. If i can create dropdown which will pass query to repeater automatically, i'll use you suggestion, thank you!

0 votesVote for this answer Mark as a Correct answer

Mike Wills answered on June 14, 2017 07:24

Hi Targutai,

This post from Brenden provides detailed steps on how to use a SmartSearch category filter to filter a list of pages. https://devnet.kentico.com/questions/custom-category-filter-for-blogposts

If you want to use a Repeater instead, you could use the same query he provides to create a solution with two repeaters. With the first repeater, create a dropdown of categories using the query. With that repeater, you could add a query string to the page when a category is selected. Then, in a second repeater, the one that displays results, you can set the where condition using a macro that reads the query string.

I would probably decide between SmartSearch and a repeater based on performance needs.

Mike

1 votesVote for this answer Mark as a Correct answer

Chetan Sharma answered on June 14, 2017 11:02

Smart Search would be an over kill here. This is what I would do:-

Assuming every page has a DocumentCategory

  1. Use a repeater with a Custom Query and pass Category to Search in the Where clause of Repeater using Query String Macros.

  2. In the custom query you will need to perform the inner join between View_CMS_Tree_Joined which has all information for your pages, CMS_Category, CMS_DocumentCategory.

Let me know if you need an exact query.

Cheers, Chetan

1 votesVote for this answer Mark as a Correct answer

Targutai Yesugei answered on June 15, 2017 06:28

I used Mike's suggestion with two repeater as simpliest for me.

Anyway, thanks you all for advices!

0 votesVote for this answer Mark as a Correct answer

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