Copied to clipboard

Flag this post as spam?

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


  • Derek Gray 2 posts 82 karma points
    Feb 26, 2020 @ 15:26
    Derek Gray
    0

    How to preview matched text on search.

    I'm trying to implement search to our site, I have managed to match and get the Url and Name, however part of requirements for the search is that on the search results, there is a preview of the content that matched. Does anyone know how to go about this? Or could point me in the location of some relevant documentation?

    So far I just have used a basic example for search results:

     @{
            var searchQuery = Request.QueryString["query"];
        }
    
        @if (!string.IsNullOrWhiteSpace(searchQuery))
        {
            var results = Umbraco.ContentQuery.Search(searchQuery, string.Empty);
            long resultCount = results != null && results.Any() ? results.Count() : 0;
            <div class="searchresults">
                <p>We found <strong>@resultCount</strong> result@(resultCount != 1 ? "s" : "") when searching for <strong>@searchQuery</strong></p>
                @if (resultCount > 0)
                {
                    <ul>
                        @foreach (var result in results)
                        {
                        <li>
                            <h3><a href="@result.Content.Url">@result.Content.Name</a></h3>
                            <p>[Need the matched content preview here]</p>
                        </li>
                        }
                    </ul>
                }
            </div>
        }
    

    Cheers.

  • Mila Pandurska 75 posts 350 karma points
    Feb 27, 2020 @ 11:34
    Mila Pandurska
    0

    Hi Derek,

    What you need to show in your preview? Some properties? The search result item has Id which is the Umbraco node ID. You can get Umbraco node by this ID and them show the needed properties. Something like:

    var node = Umbraco.Content(Id);
    node.GetPropertyValue("description");
    

    Hope that this will help.

    Mila

  • Ismail Mayat 4511 posts 10059 karma points MVP 2x admin c-trib
    Feb 27, 2020 @ 14:11
    Ismail Mayat
    100

    Derek,

    You do this Highlighter.net which is a lucene contrib plugin however it does require you to write some code.

    See https://our.umbraco.com/forum/developers/extending-umbraco/13571-Umbraco-Examine-Search-Results-Highlighting its for lucene 2.9 but its should work for 3.0.3 which is what umbraco v8 uses. There used to be a wiki doc page on how todo it but it seems to have gone.

    Main thing is passing in the lucene query you used to do the search and the field you want to highlight. In your case you are using Umbraco search which searches over all the fields and also I do not think there is a way of getting the lucene query that Umbraco.ContentQuery.Search uses.

    I have only ever used examine directly for searching and when i need to highlight i create a content field in the index that has all the content munged into for all the fields for a given document I then search and highlight on that.

    Regards

    Ismail

  • This forum is in read-only mode while we transition to the new forum.

    You can continue this topic on the new forum by tapping the "Continue discussion" link below.

Please Sign in or register to post replies