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 6730437
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T10:24:31+00:00 2026-05-26T10:24:31+00:00

In RavenDB I understand when you require to count properties matching a certain criteria,

  • 0

In RavenDB I understand when you require to count properties matching a certain criteria, it can be achieved

  1. by creating facets against those property names you wish to group,
  2. and then creating an index over the above properties and the properties required in the where clause.
  3. and finally issuing a query by using the above index. and materializing the query using the ToFacets extension.

But what happens when your where clause happens to contain a predicate against a property that is a collection of values on the document? Because if I add the nested property from the collection to the index against the parent document, my facet counts on properties on the parent document will not be accurate?

for e.g.

public class Camera { 
  string Make { get;set; }
  string Model { get;set; } 
  double Price { get;set; }
  IEnumerable<string> Showrooms { get;set; }
}

My query would look like

(from camera in session.Query<Camera, Camera_Facets>() 
 where camera.Price < 100 && camera.ShowRooms.Any(s => s.In("VIC", "ACT", "QLD")) 
 select new { 
       camera.Make, 
       camera.Model, 
       camera.Price}
 ).ToFacets("facets/CameraFacets");

Update:
Here is the failing test

[TestFixture]
public class FacetedSearchWhenQueryingNestedCollections
{
    public class Car
    {
        public Car()
        {
            Locations = new List<string>();
        }

        public string Id { get; set; }
        public string Make { get; set; }
        public string Model { get; set; }
        public double Price { get; set; }
        public IEnumerable<string> Locations { get; set; }
    }

    public class Car_Facets : AbstractIndexCreationTask<Car>
    {
        public Car_Facets()
        {
            Map = cars => from car in cars
                          select new {car.Make, car.Model, car.Price};
        }
    }

    private static IDocumentSession OpenSession
    {
        get { return new EmbeddableDocumentStore {RunInMemory = true}.Initialize().OpenSession(); }
    }

    [Test]
    public void CanGetFacetsWhenQueryingNesetedCollectionValues()
    {
        var cars = Builder<Car>.CreateListOfSize(50)
            .Section(0, 10)
                .With(x => x.Model = "Camry")
                .With(x => x.Make = "Toyota")
                .With(x => x.Price = 2000)
                .With(x => x.Locations = new[] {"VIC", "ACT"})
            .Section(11, 20)
                .With(x => x.Model = "Corolla")
                .With(x => x.Make = "Toyota")
                .With(x => x.Price = 1000)
                .With(x => x.Locations = new[] { "NSW", "ACT" })
            .Section(21, 30)
                .With(x => x.Model = "Rx8")
                .With(x => x.Make = "Mazda")
                .With(x => x.Price = 5000)
                .With(x => x.Locations = new[] { "ACT", "SA", "TAS" })
            .Section(31, 49)
                .With(x => x.Model = "Civic")
                .With(x => x.Make = "Honda")
                .With(x => x.Price = 1500)
                .With(x => x.Locations = new[] { "QLD", "SA", "TAS" })
            .Build();

        IDictionary<string, IEnumerable<FacetValue>> facets;

        using(var s = OpenSession)
        {
            s.Store(new FacetSetup { Id = "facets/CarFacets", Facets = new List<Facet> { new Facet { Name = "Model" }, new Facet { Name = "Make" }, new Facet { Name = "Price" } } });
            s.SaveChanges();

            IndexCreation.CreateIndexes(typeof(Car_Facets).Assembly, s.Advanced.DocumentStore);

            foreach (var car in cars)
                s.Store(car);

            s.SaveChanges();

            s.Query<Car, Car_Facets>().Customize(x => x.WaitForNonStaleResults()).ToList();

            facets = s.Query<Car, Car_Facets>()
                .Where(x => x.Price < 3000)
                .Where(x => x.Locations.Any(l => l.In("QLD", "VIC")))
                .ToFacets("facets/CarFacets");
        }

        Assert.IsNotNull(facets);
        Assert.IsTrue(facets.All(f => f.Value.Count() > 0));
        Assert.IsTrue(facets.All(f => f.Value.All(x => x.Count > 0)));
    }

    [TearDown]
    public void ClearData()
    {
        using(var s = OpenSession)
        {
            foreach (var car in s.Query<Car>().ToList())
                s.Delete(car);

            s.SaveChanges();
        }
    }
}

But if I change my query to be. (not querying the nested collection any more)

    facets = s.Query<Car, Car_Facets>()
                .Where(x => x.Price < 3000)
                .ToFacets("facets/CarFacets");

Now I get 3 Enumerations in the dictionary, all with values.

  • 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-05-26T10:24:31+00:00Added an answer on May 26, 2026 at 10:24 am

    if I change my index to be

        public class Car_Facets : AbstractIndexCreationTask<Car>
        {
            public Car_Facets()
            {
                Map = cars => from car in cars
                              from location in car.Locations
                              select new {car.Make, car.Model, car.Price, Location = location};
            }
        }
    

    Create a class for the projection

        public class CarOnLocation
        {
            public string Make { get; set; }
            public string Model { get; set; }
            public double Price { get; set; }
            public string Location { get; set; }
        }
    

    And then query as

        facets = s.Query<Car, Car_Facets>().AsProjection<CarOnLocation>()
                    .Where(x => x.Price < 3000)
                    .Where(x => x.Location.In("QLD", "VIC"))
                    .ToFacets("facets/CarFacets");
    

    it works.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have some behavior that I don't understand. I'm using RavenDB, and I'm using
There are about half a million RavenDB documents of Customers. One of the properties
I have been playing around with RavenDB (build 531) and I can't seem to
With RavenDB, creating an IDocumentSession upon app start-up (and never closing it until the
In the RavenDB Studio, I can see 69 CustomVariableGroup documents. My query only returns
I was wondering if someone could help me understand RavenDB transformations as I cant
Added RavenDB add-on to AppHarbor app. It seems that creating databases is disabled in
For every RavenDb Index field I can specify Storage, Sorting, Indexer and Analyzer. In
In this RavenDB post , Ayende uses a .As call to push the data
I'm playing with RavenDb and wondering if I'm missing something obvious. Thing is, that

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.