Copied to clipboard

Flag this post as spam?

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


  • Heather 13 posts 125 karma points
    Jul 31, 2020 @ 18:11
    Heather
    0

    Umbraco Search Umbraco.ContentQuery.Search(searchQuery, string.Empty);

    I am following Paul Seals tutorial on creating a search page. I was wondering if there was any way to exclude search items based on the umbraco navi hide element?

    var results = Umbraco.ContentQuery.Search(searchQuery, string.Empty);

  • Heather 13 posts 125 karma points
    Jul 31, 2020 @ 19:14
    Heather
    0

    I got a response from the amazing Paul Seal on FB.

    Hi Heather. When I’m querying using examine I do a .Not().Field(“umbracoNaviHide”, “1”) When using Linq I do this .Where(x => x.IsVisible());

  • Paul Seal 524 posts 2889 karma points MVP 7x c-trib
    Jul 31, 2020 @ 22:33
    Paul Seal
    100

    I also shared this post which probably has the answer in it.

    https://shazwazza.com/post/searching-with-ipublishedcontentquery-in-umbraco/

  • David Armitage 510 posts 2081 karma points
    Aug 01, 2020 @ 02:51
    David Armitage
    1

    Hi Heather,

    Yes this is possible. I do something similar with a lot of other bool flags.

    I always create an ExamineHelper that adds some extra search fields into Examine.

    The steps are....

    1. Create a component / composer that will handle add your extra search data into Examine.

    2. I usually convert the bool data to a string so I can easily search it later.

    3. Run your project and go and re-publish the content nodes in Umbraco. This will add the extra search data into the Examine search index.

    4. Then add the extra search stuff into your query.

    Here is an example of some code I put together for you.

    This is the component you will need to create.

    using Examine;
    using System;
    using Umbraco.Core.Composing;
    using Umbraco.Core;
    using Examine.Providers;
    using Umbraco.Core.Logging;
    using Umbraco.Web;
    using Umbraco.Core;
    using Umbraco.Core.Composing;
    
    
    namespace Website.Core.Events
    {
        [RuntimeLevel(MinLevel = RuntimeLevel.Run)]
        public class ExamineComposerPublic : IUserComposer
        {
            public void Compose(Composition composition)
            {
                composition.Components().Append<ExamineComponents>();
            }
        }
    
        public class ExamineComponents : IComponent
        {
            private readonly IUmbracoContextFactory _umbracoContextFactory;
            private readonly IExamineManager _examineManager;
            private readonly ILogger _logger;
    
            public ExamineComponents(IUmbracoContextFactory umbracoContextFactory, IExamineManager examineManager, ILogger logger)
            {
                _umbracoContextFactory = umbracoContextFactory;
                _examineManager = examineManager ?? throw new ArgumentNullException(nameof(examineManager));
                _logger = logger ?? throw new ArgumentNullException(nameof(logger));
            }
    
            public void Initialize()
            {
                if (!_examineManager.TryGetIndex(Constants.UmbracoIndexes.ExternalIndexName, out IIndex index))
                    throw new InvalidOperationException($"No index found by name {Constants.UmbracoIndexes.ExternalIndexName}");
    
                if (!(index is BaseIndexProvider indexProvider))
                    throw new InvalidOperationException("Could not cast");
    
                indexProvider.TransformingIndexValues += IndexProviderTransformingIndexValues;
            }
    
            private void IndexProviderTransformingIndexValues(object sender, IndexingItemEventArgs e)
            {
                if (int.TryParse(e.ValueSet.Id, out var pageId))
                {
                    if(e.ValueSet.ItemType == "yourDocTypeAliasHere")
                    {
                        using (UmbracoContextReference umbracoContextReference = _umbracoContextFactory.EnsureUmbracoContext())
                        {
                            var contentNode = umbracoContextReference.UmbracoContext.Content.GetById(pageId);
                            if (contentNode != null)
                            {
                                if (contentNode.Value("umbracoNaviHide") is bool umbracoNaviHide)
                                    e.ValueSet.Set("umbracoNaviHide", umbracoNaviHide.ToString());
                            }
                        }
                    }
                }
            }
    
            public void Terminate() { }
        }
    }
    

    And here is some search code.

     public void Search()
            {
                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.And);
                var examineQuery = criteria.NodeTypeAlias("yourDocTypeAliasHere");
                bool umbracoNaviHide = false;
                examineQuery.And().Field("umbracoNaviHide", umbracoNaviHide.ToString());
                ISearchResults searchResult = examineQuery.Execute(maxResults: 100);
            }
    

    You will need to tweak it to your solutions but you should get a good idea from this.

    Regards

    David

Please Sign in or register to post replies

Write your reply to:

Draft