November Happy Hour will be moved to Thursday December 5th.

Using CategoryQuery in Lucene search

Vote:
 

Hi,

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);
            categoryQuery.Items.Add("Something");
            query.QueryExpressions.Add(categoryQuery);

            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? 

#142189
Dec 04, 2015 10:15
Vote:
 

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": http://tedgustaf.com/blog/2013/4/add-custom-fields-to-the-episerver-search-index-with-episerver-7/

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})", 
            Field, 
            LuceneHelpers.EscapeParenthesis(Expression),
            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,

/Matt

#142218
Edited, Dec 04, 2015 16:07
Vote:
 

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.

#142323
Dec 09, 2015 9:17
Vote:
 

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");

categoryQuery.Items.Add(myCategory.ID.ToString());



#142338
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.