Copied to clipboard

Flag this post as spam?

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


  • minesh shah 4 posts 84 karma points
    Dec 13, 2018 @ 17:04
    minesh shah
    0

    Examine Search, Filter collection of Pages

    I have implemented Search using Examine and was wondering if I passed in a collection of NodeId how I can filter these out from the results ?

    I have been trying to replicate the logic used for "umbracoNodeHide" although not had any success

            public Examine.ISearchResults SearchUsingExamine(string[] documentTypes, List<SearchGroup> searchGroups, List<string> PagesToExclude = null)
        {
            ISearchCriteria searchCriteria = ExamineManager.Instance.CreateSearchCriteria(BooleanOperation.And);
            IBooleanOperation queryNodes = null;
    
            //only shows results for visible documents.
            queryNodes = searchCriteria.GroupedNot(new string[] {"umbracoNaviHide"}, "1");
    
            //ToDo:Filter out pages in the PagesToExclude list. 
    
            if (documentTypes != null && documentTypes.Length > 0)
            {
                //only get results for documents of a certain type
                queryNodes = queryNodes.And().GroupedOr(new string[] { _docTypeAliasFieldName }, documentTypes);
            }
    
            if (searchGroups != null && searchGroups.Any())
            {
                //in each search group it looks for a match where the specified fields contain any of the specified search terms
                //usually would only have 1 search group, unless you want to filter out further, i.e. using categories as well as search terms
                foreach (SearchGroup searchGroup in searchGroups)
                {
                    queryNodes = queryNodes.And().GroupedOr(searchGroup.FieldsToSearchIn, searchGroup.SearchTerms);
                }
            }
    
            //return the results of the search
    
  • minesh shah 4 posts 84 karma points
    Dec 17, 2018 @ 22:39
    minesh shah
    0

    Right, I have managed to achieve this by adding the following

                //if (pagesToExclude != null && pagesToExclude.Any())
            //{
            queryNodes = queryNodes.Not().GroupedOr(new [] {"__NodeId"}, new []{"7196", "10241" });    //.Not().Field("__NodeId", "7196");
            //}
    

    I'm fairly sure their are more efficient ways of doing this, if any one could advise,

  • Matt Barlow | jacker.io 164 posts 740 karma points c-trib
    Dec 18, 2018 @ 18:04
    Matt Barlow | jacker.io
    100

    You can declare queryNodes and searchCriteria implictly with "var".

    Do you need the GroupedNot on the umbracoNaviHide check? Since you are only comparing one field to "1".

      var searchCriteria = ExamineManager.Instance.CreateSearchCriteria(BooleanOperation.Not);
                var queryNodes = searchCriteria.Field("umbracoNaviHide", "1");
    

    But yes, that works for filtering by nodeId, am sure that is efficient.

    Other things I would consider doing is putting your Search Fields as an Enum, so they become strongly typed.

Please Sign in or register to post replies

Write your reply to:

Draft