3

So I am trying to bring back a list of Bucket Items and map them using Glass Mapper.

A couple of issues I am running into. I tried as you can see using the News glass model, but I get back several thousand records instead of just the one I have now.

The records returned are empty. So that is probably incorrect. If you look at the count variable with that I get back two records one being the News item I expect and the other is the standard values of the News item. I believe I am on the right track as Bucket Items get put in the index, but not sure how to map this correctly to Glass Mapper. Any ideas?

using (var context = ContentSearchManager.GetIndex("sitecore_web_index").CreateSearchContext())
{
    var newsitems = context.GetQueryable<Models.sitecore.templates.Feature.News.News>().ToList();
    var count = context.GetQueryable<SearchResultItem>().Count(item => item.TemplateId == INewsConstants.TemplateId)
}

enter image description here

2
  • How is the map for your News template setup?
    – Richard Seal
    Sep 22, 2017 at 20:20
  • It is the generated map from glass. Should I set it up as a index type model?
    – RReilley
    Sep 22, 2017 at 20:26

1 Answer 1

3

This is happening because in your first query, you are not filtering the results by anything:

var newsitems = context.GetQueryable<Models.sitecore.templates.Feature.News.News>().ToList();

Your query is returning all the Sitecore items in the index. But if your News template is setup to enforce the template on the Glass model, it will not be able to cast the Sitecore Item to your Glass model in the results that don't match the template.

Glass doesn't automatically apply a query to filter the search results by template Id.

You could add this in a couple of ways:

  1. Just add the filter to the query:

    var newsitems = context.GetQueryable<Models.sitecore.templates.Feature.News.News>()
        .Where(item => item.TemplateId == INewsConstants.TemplateId)
        .ToList();
    
  2. Add a predefined query to your model class:

    [PredefinedQuery("__templatename", ComparisonType.Equal, "news"]
    public class News 
    {
        // fields here
    }
    

This would then automatically add the template query anytime you used that class to return results from the search API.

See this post for more details on predefined queries/

4
  • Thanks. I didn't get anything back with 1 for some reason. I do see the record in the index so not exactly sure why. I am going to try and do the PredefinedQuery if I can't get 1 to work. I did have to modify 1 to be Where(x => x.TemplateId == INewsConstants.TemplateId.Guid), but that should not make a difference. I will keep you posted.
    – RReilley
    Sep 22, 2017 at 21:11
  • Looks like 2 is not working either. It returns what I got before with the SearchResultItem when I weeded out the template. The item and t the standard value. I pasted it up on top.
    – RReilley
    Sep 22, 2017 at 22:14
  • Update on this. I was able to get the predefined query to work and also exclude the standard values. The fields I need are null, but I am guessing the bucket template only gets added to the index and not the fields.
    – RReilley
    Sep 23, 2017 at 1:30
  • Marking this as the correct answer. It worked for retrieving the items. I will just need to put the bucket item in a custom index if I want the field values.
    – RReilley
    Sep 25, 2017 at 20:55

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.