Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • Alastair Todd 44 posts 142 karma points
    Jun 08, 2016 @ 08:16
    Alastair Todd
    0

    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.

     <IndexUserFields>
          <add Name="postImage" />
    

    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.

  • Ismail Mayat 4511 posts 10092 karma points MVP 2x admin c-trib
    Jun 08, 2016 @ 08:47
    Ismail Mayat
    100

    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

  • Alastair Todd 44 posts 142 karma points
    Jun 08, 2016 @ 09:56
    Alastair Todd
    0

    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 ;)

    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);
            }
        }
    }
    

    Index config:

     <IndexSet SetName="PostsIndexSet" IndexPath="~/App_Data/TEMP/ExamineIndexes/{machinename}/Posts/">
        <IndexAttributeFields>
          <add Name="id" />
          <add Name="nodeName" />
          <add Name="writerName" />
          <add Name="parentID" />
        </IndexAttributeFields>
        <IndexUserFields>
          <add Name="postImage" />
          <add Name="postImageUrl" />
          <add Name="lowResImageUrl" />
          <add Name="postDate" Type="DateTime" />
          <add Name="title" />
          <add Name="categories" />
          <add Name="html" />
          <add Name="videoHtml" />
          <add Name="youtubeId" />
        </IndexUserFields>
        <IncludeNodeTypes>
          <add Name="post"/>
        </IncludeNodeTypes>
      </IndexSet>
    
  • Ismail Mayat 4511 posts 10092 karma points MVP 2x admin c-trib
    Jun 08, 2016 @ 10:31
    Ismail Mayat
    1

    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:

    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);
            }
        }
    

    Regards

    Ismail

  • Alastair Todd 44 posts 142 karma points
    Jun 08, 2016 @ 11:23
    Alastair Todd
    0

    Great thank you. This is excellent technology.

Please Sign in or register to post replies

Write your reply to:

Draft