How to query multiple nodeTypes with Examine?
# help-with-umbraco
a
Hi! I'm having an issue where I only get results from the first nodeTypeAlias I have specified, but not any others. If I switch them around I still only get results from the first nodetype. Any suggestions on what I'm doing wrong? Here is my current code: if (!string.IsNullOrEmpty(query) && _examineManager.TryGetIndex("ExternalIndex", out IIndex? index)) { var searchQuery = index.Searcher.CreateQuery("content", Examine.Search.BooleanOperation.Or) .Field("umbracoNaviHide", "0") .And() .Field("nodeName", query) .And() .NodeTypeAlias("page") .Or() .NodeTypeAlias("article"); var searchResults = searchQuery.Execute(); } The above code only returns "page" nodetypes in the results. If I switch them around, I only get "article" nodetypes in the results 🤔
j
Try with groupedOr, this definitely works for me on a site:
Copy code
csharp
.And()
.GroupedOr(new[] { "__NodeTypeAlias" }, ArticlePicker.ModelTypeAlias, ContentPage.ModelTypeAlias, LandingPage.ModelTypeAlias, RecipePicker.ModelTypeAlias)
a
Worked like a charm! Awesome 😄 Do I need to do something similar if I wish to query against multiple fields? Currently I am just querying against "nodeName"
j
Examine has a ManagedQuery method fot that, so something like this fx will search on the query term in the two specified fields
Copy code
csharp
var pages = index.Searcher.CreateQuery("content")
    .ManagedQuery(query, new[] { $"FullTextContent_{culture.ToLowerInvariant()}", $"nodeName_{culture.ToLowerInvariant()}" })
    .And()
    .GroupedOr(new[] { "__NodeTypeAlias" }, ArticlePicker.ModelTypeAlias, ContentPage.ModelTypeAlias, LandingPage.ModelTypeAlias, RecipePicker.ModelTypeAlias)   
    .Execute(QueryOptions.SkipTake(0, limit));
a
Awesome..thanks alot for your help 😍
9 Views