Search code examples
asp.netsearchumbracoumbraco5

Make a optional search in umbraco


I made new document type in umbraco.then made some node by this document type in content. i will set up a new search index so found this code that Setting up a new search index.

@* Get the search term from query string *@
@{var searchTerm = Request.QueryString["search"];}
@{var results = ExamineManager.Instance.Search(searchTerm, true); }

but i do not know how to limited this code that can search only in my document type.


Solution

  • There are couple of steps for this.

    You will have to:

    • Create a ISearchCriteria object;
    • Create an search expression from the criteria object, including the docType alias as a field to be searched;
    • Search using the expression.

    This can be coded as:

    var criteria = ExamineManager.Instance.CreateSearchCriteria();
    
    var expression = criteria.Field("nodeTypeAlias", "yourDocTypeAlias")
                             .And()
                             .Field("nodeName", searchTerm);
    
    var results = ExamineManager.Instance.Search(expression.Compile());
    

    This is possible because when Umbraco publishes a node, it saves the node's docType alias to the search index.

    There is more Examine documentation here. I would also recommend downloading Luke which is a standalone tool that will allow you to look inside an index so you can see what is actually stored by Umbraco.