Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8935077
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T09:54:57+00:00 2026-06-15T09:54:57+00:00

Given a model like this: public class Location { public int Id { get;

  • 0

Given a model like this:

public class Location
{
    public int Id { get; set; }

    public string Name { get; set; }

    public Coordinates Geo { get; set; }

    public IList<Review> Reviews { get; set; }
}

public class Review
{
    public string Comment { get; set; }

    public int Rating { get; set; }
}

How can I write a query that will return matching locations sorted in descending order by their Rating?

I have an index that allows me to query for locations within a certain radius around a geopoint:

public class Locations_ByCoordinates : AbstractIndexCreationTask<Location>
{
    public Locations_ByCoordinates()
    {
        Map = locations => from l in locations
                           select new {_ = SpatialIndex.Generate(l.Geo.Lat, l.Geo.Lon)};
    }
}

This seems to work and I am able to sort the results like so:

RavenQueryStatistics stats;
var query = session.Advanced.LuceneQuery<Location>(Names.Indexes.LocationsByCoordinates)
                       .Statistics(out stats)
                       .WithinRadiusOf(radius, coordinates.Lat, coordinates.Lon);

switch (sort)
{
    case Sort.Distance:
        query = query.SortByDistance();
        break;
    case Sort.Rating:
        query = query.AddOrder(l => l.Reviews.Average(r => r.Rating), true); // TODO: This throws an exception, perhaps a modified index with an aggregate is required? Maybe Map/Reduce?
        break;
    case Sort.Alphabetical:
        query = query.OrderBy(l => l.Name);
        break;
    case Sort.Number_of_ratings:
        query = query.AddOrder(l => l.Reviews.Count, true);
        break;
    case Sort.Relevance: // This is the default ordering in RavenDB.
        break;
    default:
        throw new ArgumentOutOfRangeException("sort");
}

Every option works OK except sorting by average rating which throws an exception about unknown field. I feel this has to be a Map/Reduce index rather than a Map index. But I have no idea how to write it.

I am migrating this code from MongoDB, but I used to sort the documents by rating on the client side there, I thought I’d do this on the server side now.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T09:54:58+00:00Added an answer on June 15, 2026 at 9:54 am

    Since all reviews are in the location, and you want the average rating of the reviews for each location, you don’t need to reduce. Just create a new field in your map.

    Map = locations => from l in locations
                       select new {
                           _ = SpatialIndex.Generate(l.Geo.Lat, l.Geo.Lon),
                           AvgRating = l.Reviews.Average(r => r.Rating)
                       };
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Given the classes: public class Person { public string Name { get; set; }
My model is something like this: public class Line { public int CatalogNumber {get;
Given the following model ... public class Parent { public int Id { get;
Given I have a Model object like ... public class MyModel { public int
Let's say you have this Model: //model public class Stuff { public string Name
My model is a simple parent/child representation looking like this public class Parent {
Suppose I have this model: public class ViewModel { [Required] public string UserInput {
given the following model example: @Entity @Table(name=tierenforcers) public class TierEnforcer extends Model { public
I have a domain model that includes something like this: public class Customer :
This is just an example, but given the following model: class Foo(models.model): bar =

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.