Copied to clipboard

Flag this post as spam?

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


  • MB 273 posts 936 karma points
    Mar 12, 2017 @ 17:18
    MB
    0

    Examine - how to get results with images etc.

    I used this example to create a search page: Examine example

    The search results are visible and works fine. But I'm wondering how I can grap custom fields like the first image in the image uploader and other fields.

    I tried adding year in both ExamineIndex.config and the query variable in Search.cshtml along with @result.Fields["year"] but without success

    Search.cshtml

         var searcher = ExamineManager.Instance.SearchProviderCollection["ExternalSearcher"];
            var searchCriteria = searcher.CreateSearchCriteria(BooleanOperation.Or);
    
            var query = searchCriteria.GroupedOr(new string[] { "nodeName", "bodyText" }, searchTerm).Compile();
            var searchResults = searcher.Search(query);
            var noResults = searchResults.Count();
    
            <h1 class="section-headline">
                <span>Søgning</span>
            </h1>
    
            <div class="container">
                <section>
                    <p>Du søgte efter @searchTerm, vi fandt @noResults results</p>
    
                        @foreach (var result in searchResults)
                        {
                            <div>
    <img src="ShowProductImageHere" />
                                <a href="@umbraco.library.NiceUrl(result.Id)">@result.Fields["nodeName"]</a>
                            </div>
                        }
    
                </section>
    </div>
    

    ExternalServer

      <IndexSet SetName="ExternalIndexSet" IndexPath="~/App_Data/ExamineIndexes/External/">
    <IndexAttributeFields>
      <add Name="id" />
      <add Name="nodeName"/>
      <add Name="updateDate" />
      <add Name="writerName" />
      <add Name="nodeTypeAlias" />
    </IndexAttributeFields>
    <IndexUserFields>
      <add Name="bodyText"/>
      <add Name="siteName"/>
      <add Name="year"/>
    </IndexUserFields>
    <IncludeNodeTypes>
      <add Name="productPage" />
    </IncludeNodeTypes>
    

  • Nigel Wilson 945 posts 2077 karma points
    Mar 12, 2017 @ 18:53
    Nigel Wilson
    1

    Hi Mike

    We use an index for a blog and the following snippet sorts the results by a custom field:

    List<Examine.SearchResult> searchResults = searcher.Search(search, useWildcards: true).ToList();
    searchResults = searchResults.OrderByDescending(x => Convert.ToDateTime(x.Fields["displayeddatepublished"])).ToList();
    

    So, not having tested the theory, x.Fields[""] should be the bit I think you are asking about.

    Hope this helps.

    Cheers Nigel

  • MB 273 posts 936 karma points
    Mar 12, 2017 @ 19:13
    MB
    0

    Hey Nigel,

    Thank you for helping me out!

    First of all, that sorting feature is a really nice addition I didn't have before now, so thank you for that :o)

    What I originally wanted to achieve was a way to grap the informations from my document type productPage.

    Eg. in my results list I'd like to recieve the productimage, I'm just not sure how to grap it.

    EDIT SOLUTION:

    I found a solution to display the images.

    1) Go to ExamineIndex.config (in config folder) and add the alias to the IndexUserFields eg.: <add Name="productImages"/>

    2) I had to go to Umbraco backoffice -> Development -> Examine Management -> (your search provider, mine is ExternalIndexer) -> Index info & tool -> Reindex

    3) In my search.cshtml I added:

     var imageIds = result.Fields["productImages"].Split(new string[] { "," }, StringSplitOptions.RemoveEmptyEntries);
                    var images = Umbraco.Media(imageIds);
                    var firstImage = images.FirstOrDefault();
                    var firstImageUrl = firstImage.Url;
                    var firstImageCropUrl = firstImage.GetResponsiveImageUrl(433, 0);
    
    <img src="@firstImageCropUrl" />
    

    Thank you for your help and partification Nigel :o)

  • Nigel Wilson 945 posts 2077 karma points
    Mar 12, 2017 @ 19:52
    Nigel Wilson
    100

    Hi Mike.

    Sorry, I misread your initial post.

    In the search index you should add the custom field for the image property - this will typically stored the ID of the media item.

    <IndexUserFields>
    

    Then in your code you should check if the value exists and if so output the image path by using Umbraco.Media(x.Fields["productImage"]).Url (untested).

    Cheers, Nigel

  • MB 273 posts 936 karma points
    Mar 12, 2017 @ 20:03
    MB
    0

    And that right there is the golden answer! Thanks for the help Nigel, you deserve a virtual highfive!

Please Sign in or register to post replies

Write your reply to:

Draft