Using CategoryQuery in Lucene search



I'm trying including categories in Episerver Search, but I'm having some issues getting any hits. I currently have this:

        public virtual SearchResults Search(string searchText, IEnumerable searchRoots, HttpContextBase context, string languageBranch, int maxResults)
            var query = CreateQuery(searchText, searchRoots, context, languageBranch);
            return _searchHandler.GetSearchResults(query, 1, maxResults);

        private IQueryExpression CreateQuery(string searchText, IEnumerable searchRoots, HttpContextBase context, string languageBranch)
            var query = new GroupQuery(LuceneOperator.AND);
            var categoryQuery = new CategoryQuery(LuceneOperator.OR);

            return query;

In the query expression I'm currently only including the category query. But when doing the actual search I'm getting zero hits.

I know the search is working, as I am able to search for freetext via:

            var query = new GroupQuery(LuceneOperator.AND);

            query.QueryExpressions.Add(new FieldQuery(searchText));

This is what I've tried so far:

  • Adding an additional CategoryList property to a page type, and adding the [Searchable] attribute to that property.
  • Force indexing using the hidden admin-page ("/EPiServer/CMS/Admin/indexcontent.aspx") after adding the "Something" category to a page.
  • Opened the search index in Luke and I'm able to browse the index based off categories, and can see my dummy page containing the "Something" category.

I'm starting to thinking that my query syntax may be wrong? 

Dec 04, 2015 10:15

This seems like a bug - I would expect it to work based on your attempted troubleshooting steps and the code.

If I remember correctly, you should be able to work around this by using a FieldQuery with lucene syntax: "field: value" in the query expression. If you take a look at the index in Luke, you can grab the field name for the category.

Edit: I just remembered that the query expression syntax doesn't work as a pass through, that is in Commerce. Ted Nyberg has an example of a CustomFieldQuery to be able to pass in the field you wish to scope your FieldQuery to - the example is in this article as "CustomFieldQuery":

Pasted below for redundancy - all credit to Ted.

public class CustomFieldQuery : IQueryExpression
    public CustomFieldQuery(string queryExpression, string fieldName)
        Expression = queryExpression;
        Field = fieldName;
        Boost = null;
    public CustomFieldQuery(string queryExpression, string fieldName, float boost)
        Expression = queryExpression;
        Field = fieldName;
        Boost = boost;
    public string GetQueryExpression()
        return string.Format("{0}:({1}{2})", 
            Boost.HasValue ? string.Concat("^", Boost.Value.ToString(CultureInfo.InvariantCulture).Replace(",", ".")) : string.Empty );
    public string Field { get; set; }
    public string Expression { get; set; }
    public float? Boost { get; set; }

Hope this helps,


Edited, Dec 04, 2015 16:07

Thanks you. 

I am aware of the Ted's blog post regarding custom fields in Lucene, and I might use that approch. But I would prefer to use the built in category field query.

I'll reach out to Epi-support and see what they have to say.

Dec 09, 2015 9:17

When looking at the index in Luke, I noticed that it is the CategoryId that is saved, not the Category value.

I was able to get it working by passing the category ID instead, by using CategoryRepository like so:

var categoryRepository = ServiceLocator.Current.GetInstance<CategoryRepository>();

var myCategory = categoryRepository.Get("Something");


Dec 09, 2015 14:12
This topic was created over six months ago and has been resolved. If you have a similar question, please create a new topic and refer to this one.
* You are NOT allowed to include any hyperlinks in the post because your account hasn't associated to your company. User profile should be updated.