How can I set Examine to search under a specific node?
I'm trying to create a simple search results page for my blog. The structure of my site looks like:
Home
Blog
Blog Post 1
Blog Post 1
Blog Post 1
...
I have some test code to get some search only the node title:
var searcher = ExamineManager.Instance.SearchProviderCollection["ExternalSearcher"];
var searchCriteria = Searcher.CreateSearchCriteria(BooleanOperation.Or);
var query = searchCriteria.Field("nodeName", "blog post 3").Compile();
var searchResults = Searcher.Search(query);
This works, but it is searching across the entire site, instead of just in my blog.
Is there a way to have Examine search only under a specific node? Maybe a way to add parentNodeId as a IndexAttributeFields in ExamineIndex.config and have a criteria for that?
What we've done for these cases is to index a searchablePath field, which lets us then search only nodes with a particular parent somewhere in their path.
Since we delimit each path segment with a pipe, we can then search like so:
var query = searchCriteria.Field("searchablePath", "|1045|").Compile();
To find all descendant nodes of 1045.
Try this:
Create an event handler for the ExamineGatheringNodeData action
Override the ApplicationStarted function, to register your custom code for a particular index
In that custom code, grab the path
Add a new field to the index with the path formatted as a searchable string
Something like the below:
public class ExamineGatheringNodeDataAction : ApplicationEventHandler {
protected override void ApplicationStarted(UmbracoApplicationBase umbracoApplication, ApplicationContext applicationContext) {
var myIndexer = ExamineManager.Instance.IndexProviderCollection[myIndexerAlias];
myIndexer.GatheringNodeData += IndexerOnGatheringNodeData;
}
private void IndexerOnGatheringNodeData(object sender, IndexingNodeDataEventArgs e) {
var path = e.Fields["path"];
var searchablePath = String.Join(" ", path.Split(',').Select(x => String.Format("{1}{0}{1}", x.ToLower(), '|')));
if (e.Fields.ContainsKey("searchablePath"))
{
e.Fields["searchablePath"] = searchablePath;
}
else
{
e.Fields.Add("searchablePath", searchablePath);
}
}
}
How can I set Examine to search under a specific node?
I'm trying to create a simple search results page for my blog. The structure of my site looks like:
I have some test code to get some search only the node title:
This works, but it is searching across the entire site, instead of just in my blog.
Is there a way to have Examine search only under a specific node? Maybe a way to add
parentNodeId
as a IndexAttributeFields in ExamineIndex.config and have a criteria for that?Hi Steven
What we've done for these cases is to index a searchablePath field, which lets us then search only nodes with a particular parent somewhere in their path.
Since we delimit each path segment with a pipe, we can then search like so:
To find all descendant nodes of 1045.
Try this:
Something like the below:
is working on a reply...