+1-888-365-2779
Try Now
More in this section

Forums / Developing with Sitefinity / Filtering searchresults by categories

Filtering searchresults by categories

4 posts, 0 answered
  1. Jurgen
    Jurgen avatar
    11 posts
    Registered:
    11 Oct 2011
    08 Jul 2015
    Link to this post

    Hi all,

     I have extended the sitefinity search engine to filter search results by Categories. I found a blog by sitefinity which described how to do this but it's very outdated (2012). 

    The documentation for building a search query does not really exist but I managed to implement it anyway, I thought it could be helpfull to share this information.

     The setup is basically the same as in the blog, only the code for building the query changes:

    public static IEnumerable<IDocument> Search(string term, IEnumerable<string> categories, int take, int skip, out int hitcount)
            {
                var searchFields = new List<string> { "Title", "Subtitle", "Summary", "Description", "Categories" };
     
                var searchService = Telerik.Sitefinity.Services.ServiceBus.ResolveService<ISearchService>();
     
                var queryBuilder = ObjectFactory.Resolve<IQueryBuilder>();
                var query = queryBuilder.BuildQuery(term, searchFields);
                query.IndexName = "search-index";
                query.Skip = skip;
                query.Take = take;
                query.OrderBy = null;
     
                if (categories != null && categories.Any())
                {
                    var CategoryGroup = new SearchQueryGroup
                    {
                        Terms = categories.Select(c => new SearchTerm { Field = "Categories", Value = c }),
                        Operator = QueryOperator.Or,
                    };
                    var SearchGroup = query.SearchGroup;
                    query.SearchGroup = new SearchQueryGroup
                    {
                        Groups = new[] {SearchGroup, CategoryGroup},
                        Operator = QueryOperator.And
                    };
                }
     
                var result = searchService.Search(query);
                hitcount = result.HitCount;
                return result.SetContentLinks();
            }

     

  2. Velizar Bishurov
    Velizar Bishurov avatar
    143 posts
    Registered:
    01 Dec 2016
    12 Jul 2015
    Link to this post
    Hello Jurgen,

    Thank you for sharing the info with the community. We will look at modifying the blog post to indicate that it is only applicable to earlier versions.

    Regards,
    Velizar Bishurov
    Telerik
     
    Do you want to have your say in the Sitefinity development roadmap? Do you want to know when a feature you requested is added or when a bug fixed? Explore the Telerik Sitefinity CMS Ideas&Feedback Portal and vote to affect the priority of the items
     
  3. Omar
    Omar avatar
    3 posts
    Registered:
    10 May 2016
    04 Aug
    Link to this post

    I know this is an old post but I would appreciate any sort of feedback here. I managed to successfully compile and install the above custom search widget into my sitefinity website. I am running Sitefinity 8.0 for what its worth. However, it seems the search is not filtering by category because when I search a term, its returning all the files with the term in it regardless of whether they are in the specified category or not. Any insight?

    I'm using the code exactly as above. I made sure to create a search index, to categorize my files, and to reindex. 

     

  4. Rohit
    Rohit avatar
    0 posts
    Registered:
    25 Oct 2016
    08 Nov
    Link to this post

    Hi,

    How do I fix the assembly references which was explained in the blog?

    Thanks

4 posts, 0 answered