Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • David Armitage 510 posts 2081 karma points
    May 04, 2016 @ 11:49
    David Armitage
    0

    Umbraco Examine Search - Searching with Multiple NodeTypeAlias

    Hi,

    I am having a few troubles getting Examine to search by multiple NodeTypeAlias.

    I am basically wanting to search based on all vehicle types boat, car, plane etc.

    My query works with a single NodeTypeAlias Eg.

    criteria.NodeTypeAlias("car"); // 2 RESULTS
    

    When I try an or statement it just searches the first item in the or Eg.

    criteria.NodeTypeAlias("car").Or().NodeTypeAlias("boat"); // 2 RESULTS
    

    if I reverse I just returns the boats Eg.

    criteria.NodeTypeAlias("boat").Or().NodeTypeAlias("car"); // 7 RESULTS
    

    The total of boats and cars should be 10 Results but for some reason the search is ignoring the second half of the query.

    I have also tried the following code and got the exact same issues. Eg

    criteria.Field("nodeTypeAlias", "boat").Or().Field("nodeTypeAlias", "car");
    

    Does anyone know why the OR is not working for me?

    Here is the full code of the simple search. I'm going to add more search parameters later so this is why I'm using examine search over using the umbraco helpers to get the nodes.

    Code..

    public static List<Examine.SearchResult> GetSearchResults()
            {
                var searcher = Examine.ExamineManager.Instance.SearchProviderCollection["ExternalSearcher"];
                var criteria = searcher.CreateSearchCriteria();
                criteria.NodeTypeAlias("car").Or().NodeTypeAlias("boat");
                List<Examine.SearchResult> searchResults = searcher.Search(criteria).ToList();
                return searchResults;
            }
    

    Thanks for your help in advance

    Kind Regards

    David

  • David Armitage 510 posts 2081 karma points
    May 04, 2016 @ 12:00
    David Armitage
    1

    Hi,

    To help everyone out there experiencing this issue I found an answer on another thread. Thanks Tim for solving this for me. Click here for the other thread

    Basically I had to change

    var criteria = searcher.CreateSearchCriteria();
    

    To

    var criteria = searcher.CreateSearchCriteria(BooleanOperation.Or);
    

    For some reason the Or statements get ignored unless you set the default operator.

    Here is the full search code now working...

    public static List<Examine.SearchResult> GetSearchResults()
            {
                var searcher = Examine.ExamineManager.Instance.SearchProviderCollection["ExternalSearcher"];
                var criteria = searcher.CreateSearchCriteria(BooleanOperation.Or);
                criteria.NodeTypeAlias("car").Or().NodeTypeAlias("boat");
                List<Examine.SearchResult> searchResults = searcher.Search(criteria).ToList();
                return searchResults;
            }
    

    Hope this saves someone a bit of time solving the same problem.

    Thanks

    David

  • Veronica Burd 76 posts 201 karma points
    May 04, 2016 @ 12:08
    Veronica Burd
    103

    I have a similar search scenario and for that I use a IBooleanOperation with a GroupedOr as shown below.

    var searcher = ExamineManager.Instance.SearchProviderCollection["ExternalSearcher"];
    var criteria = searcher.CreateSearchCriteria(IndexTypes.Content, BooleanOperation.And);
    
    @* only look for documents of type CommitteeMember or DelegationMember *@
    IBooleanOperation query = criteria.GroupedOr(new string[] { "nodeTypeAlias" }, new string[] { "committeeMember", "delegationMember" });
    
    @* add filter to only find those linked to current MP*@
    query = query
        .And().Field("linkSearch", Model.Content.Id.ToString());
    
    List<Examine.SearchResult> searchResults = searcher.Search(query.Compile()).ToList();
    

    HTH

    Ver

  • David Armitage 510 posts 2081 karma points
    May 04, 2016 @ 12:15
    David Armitage
    0

    Thanks veronica I will give this code a go.

    I've been relying on guesswork when it comes to examine and learning as I go. This is pretty helpful.

    I wish we could rate peoples responses.

    Kind Regards

    David

  • Veronica Burd 76 posts 201 karma points
    May 04, 2016 @ 12:24
    Veronica Burd
    0

    Rule of thumb - any examine queries, look for posts by Ismail Mayat and Shannon Deminick (author of Examine). I've learned a lot from their posts on this forum and the series of blogs Ismail wrote on the Cogworks blog (start with Examiness hints and tips from the trenches part 1 - Umbraco Examine)

    Ver

  • David Armitage 510 posts 2081 karma points
    Jul 23, 2020 @ 06:37
    David Armitage
    0

    I also posted a blog article about this here. https://www.umbrajobs.com/blog/posts/2020/july/umbraco-8-examine-how-to-search-multiple-doctypes/

    This is the code you need.

     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");
    
  • Josef Henryson 24 posts 98 karma points
    Mar 24, 2023 @ 14:45
    Josef Henryson
    0

    Hi David. Found your blog post first and then this.

    If I copy your code it complains that NodeTypeAlias does not exist.

    Has it been removed in Umbraco 8 or do I need an extension?

    Cheers, Josef

  • Alan Mitchell 57 posts 281 karma points c-trib
    May 11, 2023 @ 11:36
    Alan Mitchell
    1

    Came here with a similar question, for Umbraco 11 which has subtly different syntax.

    This code works for me - based on the U11 examine quick start - but extended to exclude some node types from results:

    public IEnumerable<IPublishedContent> SearchSiteContent(string query)
        {
    
            var contentTypesToExclude = new[] {
                "siteSettings",
                "partner",
                "tag"
            };
    
            IEnumerable<string> ids = Array.Empty<string>();
            if (!string.IsNullOrEmpty(query) && _examineManager.TryGetIndex("ExternalIndex", out IIndex? index))
            {
                var searchQuery = index.Searcher.CreateQuery(IndexTypes.Content, BooleanOperation.And);
                ids = searchQuery
                    .ManagedQuery(query).And()
                    .GroupedNot(new string[] { "__NodeTypeAlias" }, contentTypesToExclude)
                    .Execute()
                    .Select(x => x.Id);
            }
    
            foreach (var id in ids)
            {
                yield return _umbracoHelper.Content(id);
            }
        }
    
Please Sign in or register to post replies

Write your reply to:

Draft