Using Intersect
To return only documents that match all provided sub-queries, use the Intersect
extension which enables RavenDB to perform server-side intersection queries.
Syntax
IRavenQueryable<T> Intersect<T>();
Example
// return all T-shirts that are manufactured by 'Raven'
// and contain both 'Small Blue' and 'Large Gray' types
IList<TShirt> tshirts = 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();
// return all T-shirts that are manufactured by 'Raven'
// and contain both 'Small Blue' and 'Large Gray' types
IList<TShirt> tshirts = await asyncSession.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>()
.ToListAsync();
public class TShirts_ByManufacturerColorSizeAndReleaseYear : AbstractIndexCreationTask<TShirt>
{
public class Result
{
public string Manufacturer { get; set; }
public string Color { get; set; }
public string Size { get; set; }
public int ReleaseYear { get; set; }
}
public TShirts_ByManufacturerColorSizeAndReleaseYear()
{
Map = tshirts => from tshirt in tshirts
from type in tshirt.Types
select new
{
Manufacturer = tshirt.Manufacturer,
Color = type.Color,
Size = type.Size,
ReleaseYear = tshirt.ReleaseYear
};
}
}
from index 'TShirts/ByManufacturerColorSizeAndReleaseYear'
where intersect(Manufacturer = 'Raven', Color = 'Blue' and Size = 'Small', Color = 'Gray' and Size = 'Large')