Sorting results by "group by" like function

Vote:
 

I'm trying to sort some results from EPiServer Find using the count of a grouping like function. However, I can't see how I would achieve this with the Find API's. At the moment I'm having to get documents back from Find (filtered by a certain value) and then do the grouping and sorting in memory with Linq to objects. Below is a summary of my indexed type and code:

public class TopicIndex 
{
        public int CreatorUserId { get; set; }
        public int ItemCategory { get; set; }
}

var docs = _searchClient.Search()
                .Filter(x => x.ItemCategory.Match(1))
                .GetResult();

var sortedDocs = docs.GroupBy(d => d.CreatorUserId)
                .Select(d => new { UserId = d.First().CreatorUserId, Count = d.Count() })
                .OrderByDescending(g => g.Count);

Any ideas how I could achieve this just using Find?

Thanks.

#143286
Jan 19, 2016 14:35
Vote:
 

Why don't you make the CreatorUserId a facet? Then you'll get the results back faceted on that, with the counts. You can still combine with a pre and/or post filter.

#143371
Jan 21, 2016 15:29
Vote:
 

I look into that Dan. Cheers

#143372
Jan 21, 2016 15:35
Vote:
 

Dan's suggestion works a treat. I had to make my property a string but that wasn't an issue. Here's the solution:

public class TopicIndex 
{
        public string CreatorUserId { get; set; }
        public int ItemCategory { get; set; }
}

var docs = _searchClient.Search<TopicIndex>()
                .Filter(x => x.ItemCategory.Match(1))
                .TermsFacetFor(x => x.CreatorUserId)
                .Take(0)
                .GetResult();

var topAuthors = docs.TermsFacetFor(x => x.CreatorUserId)
                .Terms
                .OrderByDescending(t => t.Count)
                .Select(t => new 
            {
                UserId =  t.Term,
                Count = t.Count
            });
#143376
Jan 21, 2016 16:06
Vote:
 

Glad you came right :)

#143383
Jan 21, 2016 17:07
This topic was created over six months ago and has been resolved. If you have a similar question, please create a new topic and refer to this one.
* 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.