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>
}
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");
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.
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:
Cheers.
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:
Hope that this will help.
Mila
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
is working on a reply...
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.