Permalink
Browse files

RDoc-80. Document Intersection Queries

  • Loading branch information...
ppekrol committed Jan 7, 2013
1 parent 0ab87e8 commit 4af1e35ea8d162329ede968b262231fa353384fe
@@ -0,0 +1,132 @@
+using System.Collections.Generic;
+using System.Linq;
+using Raven.Client;
+using Raven.Client.Indexes;
+using Raven.Client.Linq;
+
+namespace RavenCodeSamples.ClientApi.Querying
+{
+ #region intersection_1
+ 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; }
+ }
+
+ #endregion
+
+ #region intersection_2
+ public class TShirtIndex : AbstractIndexCreationTask<TShirt>
+ {
+ public TShirtIndex()
+ {
+ this.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
+ };
+ }
+ }
+
+ #endregion
+
+ public class Intersection : CodeSampleBase
+ {
+ public void Sample()
+ {
+ using (var store = NewDocumentStore())
+ {
+ using (var session = store.OpenSession())
+ {
+ #region intersection_3
+ 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" }
+ }
+ });
+
+ #endregion
+
+ #region intersection_4
+ session.Query<TShirt>("TShirtIndex")
+ .Where(x => x.Manufacturer == "Raven")
+ .Intersect()
+ .Where(x => x.Types.Any(t => t.Color == "Blue" && t.Size == "Small"))
+ .Intersect()
+ .Where(x => x.Types.Any(t => t.Color == "Gray" && t.Size == "Large"))
+ .ToList();
+
+ #endregion
+
+ #region intersection_5
+ session.Advanced.LuceneQuery<TShirt>("TShirtIndex")
+ .Where("Manufacturer:Raven INTERSECT Color:Blue AND Size:Small INTERSECT Color:Gray AND Size:Large")
+ .ToList();
+
+ #endregion
+ }
+ }
+ }
+ }
+}
@@ -105,6 +105,7 @@
<Compile Include="ClientApi\ConnectionToRavenDbDataStore.cs" />
<Compile Include="ClientApi\FacetedSearch.cs" />
<Compile Include="ClientApi\Querying\HandlingDocumentRelationships.cs" />
+ <Compile Include="ClientApi\Querying\Intersection.cs" />
<Compile Include="ClientApi\Querying\LazyOperations.cs" />
<Compile Include="ClientApi\Querying\LinqExtensions\OfType.cs" />
<Compile Include="ClientApi\Querying\LinqExtensions\ProjectFromIndexFieldsInto.cs" />
@@ -78,6 +78,7 @@
<None Include="client-api\index.markdown" />
<None Include="client-api\partial-document-updates.markdown" />
<None Include="client-api\querying\.docslist" />
+ <None Include="client-api\querying\intersection.markdown" />
<None Include="client-api\querying\handling-document-relationships.markdown" />
<None Include="client-api\querying\index.markdown" />
<None Include="client-api\querying\lazy-operations.markdown" />
@@ -6,3 +6,4 @@ handling-document-relationships.markdown Handling document relationships
polymorphism.markdown Polymorphism
/linq-extensions Linq extensions
lazy-operations.markdown Lazy Operations
+intersection.markdown Intersection
@@ -0,0 +1,25 @@
+# Intersection
+
+To allow users to `Intersect` queries on the server-side and return only documents that match **all** the provided sub-queries we have introduced the query intersection feature.
+
+Lets consider a case, where we have a T-Shirt class:
+
+{CODE intersection_1@ClientApi\Querying\Intersection.cs /}
+
+Index:
+
+{CODE intersection_2@ClientApi\Querying\Intersection.cs /}
+
+Few records:
+
+{CODE intersection_3@ClientApi\Querying\Intersection.cs /}
+
+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 just need to use `Intersect` query extension from `Raven.Client` namespace
+
+{CODE intersection_4@ClientApi\Querying\Intersection.cs /}
+
+or its equivalent in Lucene syntax
+
+{CODE intersection_5@ClientApi\Querying\Intersection.cs /}
+
+Above query will return `tshirts/1` and `tshirts/4` as a result. Document `tshirts/2` will not be included, because it is not manufactured by `Raven` and `tshirts/3` is not available in `Small` `Blue` so it does not match **all** the sub-queries.

0 comments on commit 4af1e35

Please sign in to comment.