How to search multiple doc types in Umbraco using Examine

Have you ever needed to restrict Examine in Umbraco to search a restricted list of doc types? Then this is for you.

By default when searching Umbraco content using Examine you can only provide 1 document type alias to limit which content types are searched which can be limiting if you have lots of different types of content and can be time-consuming constructing a query to add all the different content types to the search query. Below is an example of how you can use built-in features to pass a list of doc types to the search query in one method.

When using the following tutorial as a starter, you can adjust the code to the below to search more than one Umbraco doc type (key areas are in bold):

    
    IEnumerable ids = Array.Empty();
    
    var docTypes = new string[]{
        HomePage.ModelTypeAlias,
        ContentPage.ModelTypeAlias,
        "YOUR_DOC_TYPE_ALIASES_HERE"
    };
    
    if (_examineManager.TryGetIndex(Umbraco.Cms.Core.Constants.UmbracoIndexes.ExternalIndexName, out IIndex? index))
    {
    ids = index
        .Searcher
        .CreateQuery("content")
        .GroupedOr(new string[] { ExamineFieldNames.ItemTypeFieldName }, docTypes)
        .And()
        .Field("nodeName", query)
        .Execute()
        .Select(x => x.Id);
}

foreach (var id in ids)
{
    yield return _umbracoHelper.Content(id);
}