Skip to main content

Querying: Intersection

  • To allow users to Intersect queries on the server-side and return only documents that match all the provided sub-queries, we introduced the query intersection feature.

  • In this page:

Intersection

Let's consider a case where we have a T-Shirt class:

public class TShirt
{
public string Id { get; set; }

public int ReleaseYear { get; set; }

public string Manufacturer { get; set; }

public List<TShirtType> Types { get; set; }
}

public class TShirtType
{
public string Color { get; set; }

public string Size { get; set; }
}

We will fill our database with a few records:

session.Store(new TShirt
{
Id = "tshirts/1",
Manufacturer = "Raven",
ReleaseYear = 2010,
Types = new List<TShirtType>
{
new TShirtType {Color = "Blue", Size = "Small"},
new TShirtType {Color = "Black", Size = "Small"},
new TShirtType {Color = "Black", Size = "Medium"},
new TShirtType {Color = "Gray", Size = "Large"}
}
});

session.Store(new TShirt
{
Id = "tshirts/2",
Manufacturer = "Wolf",
ReleaseYear = 2011,
Types = new List<TShirtType>
{
new TShirtType { Color = "Blue", Size = "Small" },
new TShirtType { Color = "Black", Size = "Large" },
new TShirtType { Color = "Gray", Size = "Medium" }
}
});

session.Store(new TShirt
{
Id = "tshirts/3",
Manufacturer = "Raven",
ReleaseYear = 2011,
Types = new List<TShirtType>
{
new TShirtType { Color = "Yellow", Size = "Small" },
new TShirtType { Color = "Gray", Size = "Large" }
}
});

session.Store(new TShirt
{
Id = "tshirts/4",
Manufacturer = "Raven",
ReleaseYear = 2012,
Types = new List<TShirtType>
{
new TShirtType { Color = "Blue", Size = "Small" },
new TShirtType { Color = "Gray", Size = "Large" }
}
});

Now we want to return all the T-shirts that are manufactured by Raven and contain both Small Blue and Large Gray types.

To do this, we need to do the following:

  • add the Raven.Client.Documents namespace to usings
  • use the Intersect query extension:
IList<TShirt> results = session.Query<TShirts_ByManufacturerColorSizeAndReleaseYear.Result, TShirts_ByManufacturerColorSizeAndReleaseYear>()
.Where(x => x.Manufacturer == "Raven")
.Intersect()
.Where(x => x.Color == "Blue" && x.Size == "Small")
.Intersect()
.Where(x => x.Color == "Gray" && x.Size == "Large")
.OfType<TShirt>()
.ToList();

The above query will return tshirts/1 and tshirts/4, that match all sub-queries.
tshirts/2 will not be included in the results because it is not manufactured by Raven, and tshirts/3 will not be included because it is not available in Small Blue.