Here's my impl. Couldn't get a handle on an UmbracoHelper to lookup publishedcontent instead (UmbracoContext.Current is null at this time), but here's my code for anyone wanting to take or review ;)
public class AppStartupHandler : ApplicationEventHandler
{
protected override void ApplicationInitialized(UmbracoApplicationBase umbracoApplication, ApplicationContext applicationContext)
{
ExamineManager.Instance.IndexProviderCollection["PostsIndexer"].GatheringNodeData += ExamineEvents_GatheringNodeData;
}
public class ImageData
{
public string src { get; set; }
}
private void ExamineEvents_GatheringNodeData(object sender, IndexingNodeDataEventArgs e)
{
if (e.IndexType != IndexTypes.Content) return;
var fields = e.Fields;
var searchableFields = new Dictionary<string, string>();
var mediService = ApplicationContext.Current.Services.MediaService;
foreach (var field in fields)
{
switch (field.Key)
{
case "postImage":
var media = mediService.GetById(int.Parse(field.Value));
var imageUrl = media == null
? null
: JsonConvert.DeserializeObject<ImageData>(media.GetValue<string>("umbracoFile")).src;
searchableFields.Add("postImageUrl", imageUrl);
break;
}
}
foreach (var fld in searchableFields)
{
e.Fields.Add(fld.Key, fld.Value);
}
}
}
There is a way round that null issue on umbracohelper then you dont need media service and the extra db calls that will entail:
public void OnApplicationStarted(UmbracoApplicationBase umbracoApplication, ApplicationContext applicationContext)
{
var indexer = (UmbracoContentIndexer)ExamineManager.Instance.IndexProviderCollection["ExternalIndexer"];
var helper = new UmbracoHelper(UmbracoContext.Current);
ExamineManager.Instance.IndexProviderCollection["ExternalIndexer"].GatheringNodeData += (sender, e) => ExternalIndexerGatheringNodeData(sender,e,helper);
}
then make a modification to the method signature:
private void ExternalIndexerGatheringNodeData(object sender, IndexingNodeDataEventArgs e, UmbracoHelper helper)
{
if (e.IndexType == IndexTypes.Content)
{
// We can then search using languge root node
e.Fields.Add("SearchablePath", e.Fields["path"].Replace(",", " "));
// Make tags searchable too
if (e.Fields.ContainsKey("tags"))
{
e.Fields.Add("SearchableTags", e.Fields["tags"].Replace(",", " "));
}
ExamineHelper.AddAllFieldsToContentField(e,helper);
}
}
Examine Media umbracoFile - return image url
What is the best way to retrieve (index whatever) the umbracoFile property of a Media property on a document?
E.g. I have a property 'Post Image' on a document type 'Post'
I have an Examine index of posts and include the post image.
Searching posts is good, but for the postImage field in the results all I get is the id of the image. I want the umbracoFile.
What's the most efficient way then to get the image urls for my output (am using an S3 provider so want the full url)?
I can see there's a way to create a custom index with code but that seems real overkill and learning curve for my simple needs.
Alastair,
You can implement examine gathering node data event and during that you can get the id then the media url and inject that in.
Regards
Ismail
Great Ismail. Wonder-baa indeed.
Here's my impl. Couldn't get a handle on an UmbracoHelper to lookup publishedcontent instead (UmbracoContext.Current is null at this time), but here's my code for anyone wanting to take or review ;)
Index config:
Alastair,
There is a way round that null issue on umbracohelper then you dont need media service and the extra db calls that will entail:
then make a modification to the method signature:
Regards
Ismail
Great thank you. This is excellent technology.
is working on a reply...