I use a custom index, here called NordmanniIndex, which i then add all the fields available to it, so i can search them.
public class ExamineComponent : IComponent
{
private readonly IExamineManager _examineManager;
private readonly ILogger _logger;
private readonly CustomIndexCreator customIndexCreator;
public ExamineComponent(IExamineManager examineManager, ILogger logger, CustomIndexCreator customIndexCreator)
{
_examineManager = examineManager ?? throw new
ArgumentNullException(nameof(examineManager));
_logger = logger;
this.customIndexCreator = customIndexCreator;
}
public void Initialize()
{
foreach (var dex in customIndexCreator.Create())
{
_examineManager.AddIndex(dex);
}
if (_examineManager.TryGetIndex("NordmanniIndex", out IIndex customNordmanniIndex))
{
if (customNordmanniIndex is BaseIndexProvider indexProviderNordmanni)
{
indexProviderNordmanni.TransformingIndexValues += IndexProviderTransformingIndexValues;
}
}
}
private void IndexProviderTransformingIndexValues(object sender, IndexingItemEventArgs e)
{
if (e.ValueSet.Category == IndexTypes.Content)
{
try
{
var combinedFields = new StringBuilder();
foreach (var fieldValues in e.ValueSet.Values)
{
foreach (var value in fieldValues.Value)
{
if (value != null)
{
combinedFields.AppendLine(value.ToString());
}
}
}
e.ValueSet.Add("contents", combinedFields.ToString());
}
catch (Exception ex)
{
_logger.Error<ExamineComponent>(ex, "Error combining fields for {ValueSetId}", e.ValueSet.Id);
}
}
}
public void Terminate()
{
}
}
Then i have a SearchHelper with a GetSearchResult method.
Here i search my fields, and exclude certain pages from the result, since i don’t want them to appear in searches. And if a page has the UmbracoNavihide, that should be excluded as well.
public ISearchResults GetSearchResult(string searchTerm = "")
{
searchTerm = searchTerm.MakeSearchQuerySafe();
IExamineManager examineManager = ExamineManager.Instance;
if (!examineManager.TryGetIndex("NordmanniIndex", out IIndex index))
{
throw new InvalidOperationException($"No index found by name NordmanniIndex");
}
var searcher = index.GetSearcher();
var fieldToSearch = "contents";
var hideFromNavigation = "umbracoNaviHide";
var criteria = searcher.CreateQuery(IndexTypes.Content, BooleanOperation.Or);
var examineQuery = criteria.Field(fieldToSearch, searchTerm);
new ContentValueSetValidator(true, true, publicAccessService, null, null, null);
if (searchTerm.Contains(" "))
{
var terms = searchTerm.Split(' ').Select(x => x.MultipleCharacterWildcard()).ToArray();
examineQuery.And().GroupedOr(new[] { fieldToSearch }, terms);
}
else
{
examineQuery.And().Field(fieldToSearch, searchTerm.MultipleCharacterWildcard());
}
// if hidden from navigation, should also not be shown in search.
examineQuery.Not().Field(hideFromNavigation, 1.ToString());
//exclude these Document types from searches:
examineQuery.Not().NodeTypeAlias(NotFound.ModelTypeAlias);
examineQuery.Not().NodeTypeAlias(Global.ModelTypeAlias);
examineQuery.Not().NodeTypeAlias(AdminSection.ModelTypeAlias);
var results = examineQuery.Execute();
if (results.Any())
{
return results;
}
else
{
return null;
}
}