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>
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)
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).
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 successSearch.cshtml
ExternalServer
Hi Mike
We use an index for a blog and the following snippet sorts the results by a custom field:
So, not having tested the theory, x.Fields[""] should be the bit I think you are asking about.
Hope this helps.
Cheers Nigel
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:
Thank you for your help and partification Nigel :o)
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.
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
And that right there is the golden answer! Thanks for the help Nigel, you deserve a virtual highfive!
is working on a reply...