Creating an Advanced Search

Orchard, Indexing, Search, Advanced Search


Use this guide to help you navigate some aspects of customizing the search in Orchard. Afterwards you will be able to index fields of a part in the example I will be using price and indexing a quick mock of products.

Download package

Using the code

The key parts are:

  • indexing (if you are including custom fields) To include the data in the index catalog it will need a ContentHandler that has a bit of code like:
    • OnIndexing<TestPricePart>((context, part) =>
    • context.DocumentIndex.Add("price", part.Record.Price)
    • .RemoveTags()
    • .Analyze()
    • );
  • Create a custom search service implementing the priorities of your search and fields to search on

Steps - Creating your own search

  1. Create the parts and records needed for the content type to search.
  2. Create an index for the search. This can be done in-code using the IIndexManager or using the admin menu (index under settings).
  3. Add indexing tasks for the fields that need indexing for the part
  4. Create a service to define how you want the search to act. This can be based on ISearchService or by starting from scratch.
  5. Create a controller to take in options and serve the new results of the service.
    • Ive taken to suppressing the default controller to support different display types for the basic search.
  6. Create or extend the main index action or specialize an advanced query route to separate the complexity or different search modes.

Points of Interest

AdvancedSearchFormPartDriver - Provides an additional shape for the SearchFormPart. This is to provide the advanced features of the search and to display on the search form widget.

IAdvancedSearchService (AdvancedSearchService) - shows how to search a particular field. There are a few options to begin. Here is a example of searching for products withing a price range 'only'.

  • if (advancedSearch.Between.PriceFrom.HasValue &&
  • !advancedSearch.Between.PriceTo.HasValue)
  • searchBuilder
  • .WithinRange(SearchDefinitions.Price, advancedSearch.Between.PriceFrom.GetValueOrDefault(), double.MaxValue)
  • .Mandatory();

AdvancedSearchController - Overrides the default controller for Search as well as provides the views and shapes for the advanced search. Additionally it serves up results by the content type found in the custom settings rather than the default summary, providing a little more flexibility.


Version 1 - 25th June 2013

On Jul 4 2013 3:35 PMBy admin