Search By Category
Hi
I have tried to do a search by category code is below. However when i preform the search it does not get items based on the categories. Does anyone have any advice?
var service = Telerik.Sitefinity.Services.ServiceBus.ResolveService<
ISearchService
>();
var queryBuilder = ObjectFactory.Resolve<
IQueryBuilder
>();
ISearchQuery searchQuery = queryBuilder.BuildQuery(query, searchFields);
searchQuery.IndexName = indexName;
searchQuery.HighlightedFields = highlightedFields;
var currentFilter = searchQuery.Filter;
var categoriesFilter = new SearchFilter();
categoriesFilter.Operator = QueryOperator.Or;
categoriesFilter.AddClause(new SearchFilterClause("Category", category, FilterOperator.Contains));
if (currentFilter != null) categoriesFilter.AddFilter(currentFilter);
searchQuery.Filter = categoriesFilter;
IResultSet result = service.Search(searchQuery);
Hello,
I would suggest checking whether the field name is actually "Category". By default when adding a categories field to a dynamic module for example the name is "Categories".
Also check whether the field has been added to the additional fields for indexing of the search index.
Regards,
Velizar Bishurov
Telerik
Hi Velizar,
It is definitely called category, I had catgories originally but when I inspected the index using luke.net it was showing up as blank.
Thanks
Hi,
Try the following sample code:
var control =
this
.control;
var service = Telerik.Sitefinity.Services.ServiceBus.ResolveService<ISearchService>();
var queryBuilder = ObjectFactory.Resolve<IQueryBuilder>();
var searchQuery = queryBuilder.BuildQuery(query, control.SearchFields);
searchQuery.IndexName = catalogue;
searchQuery.Skip = skip;
searchQuery.Take = take;
searchQuery.OrderBy =
null
;
searchQuery.HighlightedFields = control.HighlightedFields;
var searchQueryGroup =
new
SearchQueryGroup();
searchQueryGroup.Operator = QueryOperator.Or;
var term =
new
SearchTerm();
term.Field =
"Categoy"
;
term.Value =
"CategoryName"
;
searchQueryGroup.AddTerm(term);
var searchQueryGroup2 =
new
SearchQueryGroup();
searchQueryGroup2.Operator = QueryOperator.Or;
var termContent =
new
SearchTerm();
termContent.Field =
"Content"
;
termContent.Value = query +
"*"
;
searchQueryGroup2.AddTerm(termContent);
var termTitle =
new
SearchTerm();
termTitle.Field =
"Title"
;
termTitle.Value = query +
"*"
;
searchQueryGroup2.AddTerm(termTitle);
searchQuery.SearchGroup = searchQueryGroup;
searchQuery.SearchGroup.AddGroup(searchQueryGroup2);
IResultSet result = service.Search(searchQuery);
hitCount = result.HitCount;
return
result.SetContentLinks();
Hi Velizar
I have tried to implement your code and still no luck. Is it possibly the way in which it is stored within the lucence index.
I used luke.net to grab a screenshot of the field in the index, see attached image.
Everything else above works just the categories, I am trying to search the Blogs based on category, if that helps to understand what I am trying to achieve.
Thanks
Hello,
I have tried this on my end and indeed searching by category does not work for blog posts as the content inbound pipe does not have the necessary logic to persist the categories as string to the index.
I was able to achieve the desired behavior by extending the ContentInboundPipe and adding a custom field in the search index as additional field for indexing (one that the blog posts don't have). In my case in the additional fields for indexing I added the field "CustomCategories" and extended the content inbound pipe in the following way:
using
System;
using
System.Collections.Generic;
using
System.Linq;
using
System.Web;
using
Telerik.OpenAccess;
using
Telerik.Sitefinity.Model;
using
Telerik.Sitefinity.Blogs.Model;
using
Telerik.Sitefinity.Publishing;
using
Telerik.Sitefinity.Publishing.Pipes;
using
Telerik.Sitefinity.Taxonomies;
using
Telerik.Sitefinity.Taxonomies.Model;
namespace
SitefinityWebApp
public
class
CustomContentInboundPipe : ContentInboundPipe
private
const
string
BlogPostTypeName =
"Telerik.Sitefinity.Blogs.Model.BlogPost"
;
protected
override
void
SetProperties(WrapperObject wrapperObject, Telerik.Sitefinity.Model.IContent contentItem)
base
.SetProperties(wrapperObject, contentItem);
if
(contentItem.GetType().FullName == BlogPostTypeName)
var blogPost = contentItem
as
BlogPost;
var categoriesIds = blogPost.GetValue<TrackedList<Guid>>(
"Category"
).ToList();
var taxonomyManager = TaxonomyManager.GetManager();
var categories = taxonomyManager.GetTaxa<HierarchicalTaxon>().Where(t => categoriesIds.Contains(t.Id)).Select(t => t.Name);
wrapperObject.SetOrAddProperty(
"CustomCategories"
,
string
.Join(
","
, categories));
protected
void
Application_Start(
object
sender, EventArgs e)
PublishingModule.Configured += PublishingModule_Configured;
private
void
PublishingModule_Configured(
object
sender, EventArgs e)
PublishingSystemFactory.UnregisterPipe(ContentInboundPipe.PipeName);
PublishingSystemFactory.RegisterPipe(ContentInboundPipe.PipeName,
typeof
(CustomContentInboundPipe));
var control =
this
.control;
var service = Telerik.Sitefinity.Services.ServiceBus.ResolveService<ISearchService>();
var queryBuilder = ObjectFactory.Resolve<IQueryBuilder>();
var searchQuery = queryBuilder.BuildQuery(query, control.SearchFields);
searchQuery.IndexName = catalogue;
searchQuery.Skip = skip;
searchQuery.Take = take;
searchQuery.OrderBy =
null
;
searchQuery.HighlightedFields = control.HighlightedFields;
var searchQueryGroup =
new
SearchQueryGroup();
searchQueryGroup.Operator = QueryOperator.Or;
var term =
new
SearchTerm();
term.Field =
"CustomCategories"
;
term.Value =
"CategoryName"
;
searchQueryGroup.AddTerm(term);
var searchQueryGroup2 =
new
SearchQueryGroup();
searchQueryGroup2.Operator = QueryOperator.Or;
var termContent =
new
SearchTerm();
termContent.Field =
"Content"
;
termContent.Value = query +
"*"
;
searchQueryGroup2.AddTerm(termContent);
var termTitle =
new
SearchTerm();
termTitle.Field =
"Title"
;
termTitle.Value = query +
"*"
;
searchQueryGroup2.AddTerm(termTitle);
searchQuery.SearchGroup = searchQueryGroup;
searchQuery.SearchGroup.AddGroup(searchQueryGroup2);
IResultSet result = service.Search(searchQuery);
hitCount = result.HitCount;
return
result.SetContentLinks();