Include additional fields in result.Items objects

Vote:
 
I added extension method to include CategoryName and included that as a field in conventions.

The field is being indexed properly:

"CategoryName$$string": "MyCategoryName",
The filtering on that field works fine:

var result = SearchClient.Instance
                                 .Search<ProductContent>()
                                 .Filter(x => x.CategoryName().MatchCaseInsensitive(categoryName))
                                 // ...
                                 .GetContentResult();

The above works correctly.

However, I'd like to access the CategoryName indexed property in each of result.Items object. Is there any way to do that?

#285357
Edited, Aug 11, 2022 12:44
Vote:
 

When you call GetContentResult(), you are actually loading the matching content from database (or cache, if cached). if you want specific field (not limited to your CategoryName), you would have to use GetResult() as a projection of the document 

#285358
Aug 11, 2022 13:05
Vote:
 

Thanks for the answer, Quan.

Hm when I call the GetResult for the ProductContent, I get the exception:

EPiServer.Core.EPiServerException: Please use GetContentResult extension method for getting results with content types

How can I get the product content with GetResult?

#285362
Aug 11, 2022 13:46
Vote:
 

How does your code look like? Did you just change the last line to GetResult? you would also need to remove the Search<ProductContent>(). instead: Projections (optimizely.com)

it might not be trivial change 

#285363
Aug 11, 2022 13:57
Vote:
 

For now, the full search is as following:

var result = SearchClient.Instance
                         .Search<ProductContent>()
                         .Filter(x => x.CategoryRouteSegment().MatchCaseInsensitive(categoryRouteSegment.ToString()))
                         .FilterOnCurrentMarket()
                         .FilterOnLanguages(new[] { currentLanguageCode })
                         .FilterForVisitor()
                         .OrderBy(x => x.SortOrder())
                         .Skip(paging.Skip())
                         .Take(paging.PageSize)
                         .GetContentResult();

So, if I would remove Search<ProductContent>() then - should I match type property with Product Content manualy and have something like ForQuery("*")?

#285364
Aug 11, 2022 14:01
Vote:
 

You need to be using something like this

var result = client.Search<BlogPost>() .Select(x => new SearchResult { Title = x.Title, Author = x.Author.Name }) .GetResult();

i.e. a Select query for projection. it might not provide all you need, so you might be forced to load the content yourself (tip: using GetItems is a good way to ensure performance) 

#285403
Aug 12, 2022 5:56
Vote:
 

Would that be possible for the Product content? Because I got the exception when using GetResult on that:

EPiServer.Core.EPiServerException: Please use GetContentResult extension method for getting results with content types

#285404
Aug 12, 2022 6:52
Vote:
 

Yes it should. But the Select part is important because you are getting a projection. Your query should look like this (untested by me obviously)

var result = SearchClient.Instance
                         .Search<ProductContent>()
                         .Filter(x => x.CategoryRouteSegment().MatchCaseInsensitive(categoryRouteSegment.ToString()))
                         .FilterOnCurrentMarket()
                         .FilterOnLanguages(new[] { currentLanguageCode })
                         .FilterForVisitor()
                         .OrderBy(x => x.SortOrder())
                         .Skip(paging.Skip())
                         .Take(paging.PageSize)
                         .Select(x=> new SearchResult {Name = x.Name, ContentLink = x.ContentLink})
                         .GetResult();
#285405
Aug 12, 2022 7:04
* You are NOT allowed to include any hyperlinks in the post because your account hasn't associated to your company. User profile should be updated.