I’m not saying you can’t get that to work, but here is an alternative I always use.
You can call the TransformingIndexValues event to add a new path field to the index that is searchable.
The problem with the default path is that it is indexed as a string “-1,1230,1242,1318,1319” so you cannot find based on just the ID you want.
If you instead index it as a multivalue field with a string array: [1,1230,1242,1318,1319] then each of those will be indexed and you could simply search for an id and all nodes with that in its path would be shown.
Here is an example of adding a new “searchablePath” field:
public class ExternalIndexTransformations : INotificationHandler<UmbracoApplicationStartedNotification>
{
private readonly IExamineManager _examineManager;
public ExternalIndexTransformations(IExamineManager examineManager)
{
_examineManager = examineManager;
}
public void Handle(UmbracoApplicationStartedNotification notification)
{
if (!_examineManager.TryGetIndex(Umbraco.Cms.Core.Constants.UmbracoIndexes.ExternalIndexName, out var index))
{
return;
}
index.TransformingIndexValues += IndexOnTransformingIndexValues;
}
private void IndexOnTransformingIndexValues(object? sender, IndexingItemEventArgs e)
{
if (e.ValueSet.Category != IndexTypes.Media)
{
return;
}
var valuesDictionary = e.ValueSet.Values.ToDictionary(x => x.Key, x => x.Value.ToList());
var path = valuesDictionary["path"].FirstOrDefault()?.ToString()?.Split(',');
var paths = path?.Cast<object>().ToList();
if (paths is not null && paths.Count != 0)
{
valuesDictionary.Add("searchablePath", paths);
}
e.SetValues(valuesDictionary.ToDictionary(x => x.Key, x => (IEnumerable<object>)x.Value));
}
}
Then you can just add it to your query:
var query = index
.Searcher.CreateQuery(IndexTypes.Media)
.Field("searchablePath", node.Id)
.And()
.GroupedOr(["__NodeTypeAlias"], "umbracoMediaArticle", "File");
Note: in my example I did it for media, but it works just the same for content, just have to change the IndexTypes.Media to IndexTypes.Content