Skip to the content

Umbraco 8 Examine How to Search Multiple DocTypes

Hi Guys,

I had a little trouble with this so thought I should post here how to Search Multiple DocTypes using Examine in Umbraco 8.

Here is the basics you need in place.

var searcher = index.GetSearcher();
var criteria = searcher.CreateQuery(IndexTypes.Content, BooleanOperation.Or);
var examineQuery = criteria.NodeTypeAlias("contentPage").Or().NodeTypeAlias("blogDetailsPage").Or().NodeTypeAlias("eventDetailsPage").Or().NodeTypeAlias("officeDetailsPage").Or().NodeTypeAlias("sectorDetailsPage").Or().NodeTypeAlias("serviceDetailsPage").Or().NodeTypeAlias("staffDetailsPage");

Hint* Remember to use BooleanOperation.Or. Before I used And and it was producing some strange results.

Here is the complete code.

public SearchPageSearch Search(SearchPageSearch searchPageSearch)
{
try
{
if (!_examineManager.TryGetIndex(Constants.UmbracoIndexes.ExternalIndexName, out IIndex index))
throw new InvalidOperationException($"No index found by name {Constants.UmbracoIndexes.ExternalIndexName}");var searcher = index.GetSearcher();
var criteria = searcher.CreateQuery(IndexTypes.Content, BooleanOperation.Or);
var examineQuery = criteria.NodeTypeAlias("contentPage").Or().NodeTypeAlias("blogDetailsPage").Or().NodeTypeAlias("eventDetailsPage").Or().NodeTypeAlias("officeDetailsPage").Or().NodeTypeAlias("sectorDetailsPage").Or().NodeTypeAlias("serviceDetailsPage").Or().NodeTypeAlias("staffDetailsPage");if (!string.IsNullOrEmpty(searchPageSearch.Keywords))
{
if (searchPageSearch.Keywords.Contains(" "))
{
string[] terms = searchPageSearch.Keywords.Split(' ');
examineQuery.And().GroupedOr(new[] { "contents" }, terms);
}
else
{
examineQuery.And().Field("contents", searchPageSearch.Keywords.MultipleCharacterWildcard());
}
}examineQuery.OrderByDescending(new SortableField[] { new SortableField("sortableDate") });int pageIndex = searchPageSearch.CurrentPage - 1;
int pageSize = searchPageSearch.ItemsPerPage;
ISearchResults searchResult = examineQuery.Execute(maxResults: pageSize * (pageIndex + 1));
IEnumerable<ISearchResult> pagedResults = searchResult.Skip(pageIndex * pageSize);
int totalResults = Convert.ToInt32(searchResult.TotalItemCount);
searchPageSearch.TotalItems = totalResults;
searchPageSearch.TotalPages = (totalResults + searchPageSearch.ItemsPerPage - 1) / searchPageSearch.ItemsPerPage;if (pagedResults != null && pagedResults.Count() > 0)
searchPageSearch.SearchPageResults = GetSearchPageResultFromSearch(pagedResults);}
catch (Exception e)
{
_logger.Error<SiteHelper>("Search | Exception: {0} | Message: {1}", e.InnerException != null ? e.InnerException.ToString() : "", e.Message != null ? e.Message.ToString() : "");
}
return searchPageSearch;
}

About the author

David Armitage

Umbraco Community Builder | .Net MVC Developer | Umbraco Certified Master | Recruitment Professional

Hey Peeps,

I'm an entrepreneur and web developer with a passion for coding. I absolutely love working with Umbraco CMS and appreciate the Umbraco community even more.

I've got 10 years+ .Net experience and 7 years+ experience working in the recruitment industry, both coding and marketing job websites. I wanted to use my skills to help give something back to this awesome community and so I decided to build UmbraJobs.com.

My vision & fundamentals

I want this website to be a place where Umbraco Professionals and Umbraco Partners can advertise their services but at the same time I want to filter out all the under-qualified freelancers & agencies that take up the biggest part of other job & freelancer websites. We've all worked hard to gain our Umbraco certifications, partnership status, and help build the Umbraco community to what it is today. I want to see everyone get rewarded for their efforts.

Follow me on social media

If you're an Umbraco professional, partner, employer, recruiter or a passionate Umbraco community builder then I'm more than happy to hear from you. Follow me on my social media channels and please reach out if you have any needs, want help or consultation with anything Umbraco related or just want a general chat.

comments powered by Disqus

Blog Filter


How we can help?

Need help with an Umbraco project?

Need help with a project but not too sure who to approach? Send us your project brief and budget. We will provide a free consultation and can help you gather quotes from the best and most suitable freelancers or agencies.

Looking to hire an Umbraco Professional?

Have you got job vacancy and want to hire an Umbraco professional? Post a job on our website for free. Alternatively let us know your requirements and we will send suitable prospects your way.

Claim your free profile!

Are you an Umbraco Freelance Developer or Umbraco partner that wants to advertise on our site? If you work with Umbraco or registered as an Umbraco partner then you can create a profile for free.

Let's build the Umbraco Community

We're big on building the Umbraco community and we think you guys are awesome! If there's anyway at all we can help then please reach out.