Making Custom Search Facets


You can also add facets to your search. The QueryBuilder class has a method called AddFacet with the following signature:

public static void AddFacet(string field, IEnumerable<string> values, 

         Lucene.Net.Search.Occur occur, ref BooleanQuery query)

The last parameter is a reference to the query object returned from GetQuery(). Let's say that you wanted to add a facet to your search to filter out items by department, you could call the method like this:

AddFacet("Department", new string[] { "HR", "Sales" }, Occur.SHOULD, ref query);

Of course, you would need to track this new facet in your view/controller like the examples above show for category or type.

Passing a value of MUST means that each of the items passed into the values parameter must mach on the field. By passing SHOULD you are instructing lucene that any can match. So if you know that your items will only have one value for department (like items can only have one type) then you should pass SHOULD. If your items can have multiple departments (like items can have multiple categories) then you could pass MUST.