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);
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....
Create a component / composer that will handle add your extra search data into Examine.
I usually convert the bool data to a string so I can easily search it later.
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.
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.
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);
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());
I also shared this post which probably has the answer in it.
https://shazwazza.com/post/searching-with-ipublishedcontentquery-in-umbraco/
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....
Create a component / composer that will handle add your extra search data into Examine.
I usually convert the bool data to a string so I can easily search it later.
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.
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.
And here is some search code.
You will need to tweak it to your solutions but you should get a good idea from this.
Regards
David
is working on a reply...