コード例 #1
0
        public void CanQueryForMetadataAndIndexEntriesOnly()
        {
            using (var store = GetDocumentStore())
            {
                using (var s = store.OpenSession())
                {
                    for (int i = 0; i < 5; i++)
                    {
                        s.Store(new User {
                            Name = "user" + i
                        });
                    }
                    s.SaveChanges();
                }

                store.Maintenance.Send(new PutIndexesOperation(new[] {
                    new IndexDefinition
                    {
                        Maps   = { "from doc in docs.Users select new { doc.Name }" },
                        Fields = new Dictionary <string, IndexFieldOptions>
                        {
                            { "Name", new IndexFieldOptions() }
                        },
                        Name = "test"
                    }
                }));

                Indexes.WaitForIndexing(store);

                using (var commands = store.Commands())
                {
                    var metadataOnly = commands.Query(new IndexQuery {
                        Query = "FROM INDEX 'test'"
                    }, metadataOnly: true).Results;

                    foreach (BlittableJsonReaderObject item in metadataOnly)
                    {
                        Assert.Equal(1, item.Count);
                        BlittableJsonReaderObject _;
                        Assert.True(item.TryGet(Constants.Documents.Metadata.Key, out _));
                    }

                    var entriesOnly = commands.Query(new IndexQuery {
                        Query = "FROM INDEX 'test' ORDER BY Name ASC"
                    }, indexEntriesOnly: true).Results;

                    for (int i = 0; i < 5; i++)
                    {
                        var item = (BlittableJsonReaderObject)entriesOnly[i];

                        Assert.Equal(2, item.Count);

                        string name;
                        Assert.True(item.TryGet("Name", out name));
                        Assert.Equal("user" + i, name);

                        string id;
                        Assert.True(item.TryGet(Constants.Documents.Indexing.Fields.DocumentIdFieldName, out id));
                        Assert.Equal("users/" + (i + 1) + "-a", id);
                    }
                }
            }
        }
コード例 #2
0
ファイル: Querying.cs プロジェクト: ikvm/ravendb
        public void CanGetFacets()
        {
            using (var store = GetDocumentStore())
            {
                var index = new CameraCost();
                index.Execute(store);

                using (var commands = store.Commands())
                {
                    for (var i = 0; i < 10; i++)
                    {
                        commands.Put(
                            "cameras/" + i,
                            null,
                            new Camera
                        {
                            Id           = "cameras/" + i,
                            Manufacturer = i % 2 == 0 ? "Manufacturer1" : "Manufacturer2",
                            Cost         = i * 100D,
                            Megapixels   = i * 1D
                        },
                            new Dictionary <string, object> {
                            { "@collection", "Cameras" }
                        });
                    }


                    Indexes.WaitForIndexing(store);

                    var facets = new List <Facet>
                    {
                        new Facet
                        {
                            FieldName = "Manufacturer"
                        }
                    };

                    var rangeFacets = new List <RangeFacet>
                    {
                        new RangeFacet
                        {
                            Ranges =
                            {
                                "Cost <= 200",
                                "Cost >= 300 and Cost <= 400",
                                "Cost >= 500 and Cost <= 600",
                                "Cost >= 700 and Cost <= 800",
                                "Cost >= 900"
                            }
                        },
                        new RangeFacet
                        {
                            Ranges =
                            {
                                "Megapixels <= 3",
                                "Megapixels >= 4 and Megapixels <= 7",
                                "Megapixels >= 8 and Megapixels <= 10",
                                "Megapixels >= 11",
                            }
                        }
                    };


                    commands.Put(
                        "facets/CameraFacets",
                        null,
                        new FacetSetup {
                        Id = "facets/CameraFacets", Facets = facets, RangeFacets = rangeFacets
                    },
                        null);

                    Indexes.WaitForIndexing(store);

                    using (var session = store.OpenSession())
                    {
                        var facetResults = session
                                           .Query <Camera, CameraCost>()
                                           .AggregateUsing("facets/CameraFacets")
                                           .Execute();

                        Assert.Equal(3, facetResults.Count);

                        Assert.Equal(2, facetResults["Manufacturer"].Values.Count);
                        Assert.Equal("manufacturer1", facetResults["Manufacturer"].Values[0].Range);
                        Assert.Equal(5, facetResults["Manufacturer"].Values[0].Count);
                        Assert.Equal("manufacturer2", facetResults["Manufacturer"].Values[1].Range);
                        Assert.Equal(5, facetResults["Manufacturer"].Values[1].Count);

                        Assert.Equal(5, facetResults["Cost"].Values.Count);
                        Assert.Equal("Cost <= 200", facetResults["Cost"].Values[0].Range);
                        Assert.Equal(3, facetResults["Cost"].Values[0].Count);
                        Assert.Equal("Cost >= 300 and Cost <= 400", facetResults["Cost"].Values[1].Range);
                        Assert.Equal(2, facetResults["Cost"].Values[1].Count);
                        Assert.Equal("Cost >= 500 and Cost <= 600", facetResults["Cost"].Values[2].Range);
                        Assert.Equal(2, facetResults["Cost"].Values[2].Count);
                        Assert.Equal("Cost >= 700 and Cost <= 800", facetResults["Cost"].Values[3].Range);
                        Assert.Equal(2, facetResults["Cost"].Values[3].Count);
                        Assert.Equal("Cost >= 900", facetResults["Cost"].Values[4].Range);
                        Assert.Equal(1, facetResults["Cost"].Values[4].Count);

                        Assert.Equal(4, facetResults["Megapixels"].Values.Count);
                        Assert.Equal("Megapixels <= 3", facetResults["Megapixels"].Values[0].Range);
                        Assert.Equal(4, facetResults["Megapixels"].Values[0].Count);
                        Assert.Equal("Megapixels >= 4 and Megapixels <= 7", facetResults["Megapixels"].Values[1].Range);
                        Assert.Equal(4, facetResults["Megapixels"].Values[1].Count);
                        Assert.Equal("Megapixels >= 8 and Megapixels <= 10", facetResults["Megapixels"].Values[2].Range);
                        Assert.Equal(2, facetResults["Megapixels"].Values[2].Count);
                        Assert.Equal("Megapixels >= 11", facetResults["Megapixels"].Values[3].Range);
                        Assert.Equal(0, facetResults["Megapixels"].Values[3].Count);
                    }

                    using (var session = store.OpenSession())
                    {
                        var r1 = session.Query <Camera, CameraCost>()
                                 .Where(x => x.Cost < 200)
                                 .AggregateUsing("facets/CameraFacets")
                                 .Execute();

                        var r2 = session.Query <Camera, CameraCost>()
                                 .Where(x => x.Megapixels < 3)
                                 .AggregateUsing("facets/CameraFacets")
                                 .Execute();

                        var multiFacetResults = new[] { r1, r2 };

                        Assert.Equal(3, multiFacetResults[0].Count);

                        Assert.Equal(2, multiFacetResults[0]["Manufacturer"].Values.Count);
                        Assert.Equal("manufacturer1", multiFacetResults[0]["Manufacturer"].Values[0].Range);
                        Assert.Equal(1, multiFacetResults[0]["Manufacturer"].Values[0].Count);
                        Assert.Equal("manufacturer2", multiFacetResults[0]["Manufacturer"].Values[1].Range);
                        Assert.Equal(1, multiFacetResults[0]["Manufacturer"].Values[1].Count);

                        Assert.Equal(5, multiFacetResults[0]["Cost"].Values.Count);
                        Assert.Equal("Cost <= 200", multiFacetResults[0]["Cost"].Values[0].Range);
                        Assert.Equal(2, multiFacetResults[0]["Cost"].Values[0].Count);
                        Assert.Equal("Cost >= 300 and Cost <= 400", multiFacetResults[0]["Cost"].Values[1].Range);
                        Assert.Equal(0, multiFacetResults[0]["Cost"].Values[1].Count);
                        Assert.Equal("Cost >= 500 and Cost <= 600", multiFacetResults[0]["Cost"].Values[2].Range);
                        Assert.Equal(0, multiFacetResults[0]["Cost"].Values[2].Count);
                        Assert.Equal("Cost >= 700 and Cost <= 800", multiFacetResults[0]["Cost"].Values[3].Range);
                        Assert.Equal(0, multiFacetResults[0]["Cost"].Values[3].Count);
                        Assert.Equal("Cost >= 900", multiFacetResults[0]["Cost"].Values[4].Range);
                        Assert.Equal(0, multiFacetResults[0]["Cost"].Values[4].Count);

                        Assert.Equal(4, multiFacetResults[0]["Megapixels"].Values.Count);
                        Assert.Equal("Megapixels <= 3", multiFacetResults[0]["Megapixels"].Values[0].Range);
                        Assert.Equal(2, multiFacetResults[0]["Megapixels"].Values[0].Count);
                        Assert.Equal("Megapixels >= 4 and Megapixels <= 7", multiFacetResults[0]["Megapixels"].Values[1].Range);
                        Assert.Equal(0, multiFacetResults[0]["Megapixels"].Values[1].Count);
                        Assert.Equal("Megapixels >= 8 and Megapixels <= 10", multiFacetResults[0]["Megapixels"].Values[2].Range);
                        Assert.Equal(0, multiFacetResults[0]["Megapixels"].Values[2].Count);
                        Assert.Equal("Megapixels >= 11", multiFacetResults[0]["Megapixels"].Values[3].Range);
                        Assert.Equal(0, multiFacetResults[0]["Megapixels"].Values[3].Count);


                        Assert.Equal(3, multiFacetResults[1].Count);

                        Assert.Equal(2, multiFacetResults[1]["Manufacturer"].Values.Count);
                        Assert.Equal("manufacturer1", multiFacetResults[1]["Manufacturer"].Values[0].Range);
                        Assert.Equal(2, multiFacetResults[1]["Manufacturer"].Values[0].Count);
                        Assert.Equal("manufacturer2", multiFacetResults[1]["Manufacturer"].Values[1].Range);
                        Assert.Equal(1, multiFacetResults[1]["Manufacturer"].Values[1].Count);

                        Assert.Equal(5, multiFacetResults[1]["Cost"].Values.Count);
                        Assert.Equal("Cost <= 200", multiFacetResults[1]["Cost"].Values[0].Range);
                        Assert.Equal(3, multiFacetResults[1]["Cost"].Values[0].Count);
                        Assert.Equal("Cost >= 300 and Cost <= 400", multiFacetResults[1]["Cost"].Values[1].Range);
                        Assert.Equal(0, multiFacetResults[1]["Cost"].Values[1].Count);
                        Assert.Equal("Cost >= 500 and Cost <= 600", multiFacetResults[1]["Cost"].Values[2].Range);
                        Assert.Equal(0, multiFacetResults[1]["Cost"].Values[2].Count);
                        Assert.Equal("Cost >= 700 and Cost <= 800", multiFacetResults[1]["Cost"].Values[3].Range);
                        Assert.Equal(0, multiFacetResults[1]["Cost"].Values[3].Count);
                        Assert.Equal("Cost >= 900", multiFacetResults[1]["Cost"].Values[4].Range);
                        Assert.Equal(0, multiFacetResults[1]["Cost"].Values[4].Count);

                        Assert.Equal(4, multiFacetResults[1]["Megapixels"].Values.Count);
                        Assert.Equal("Megapixels <= 3", multiFacetResults[1]["Megapixels"].Values[0].Range);
                        Assert.Equal(3, multiFacetResults[1]["Megapixels"].Values[0].Count);
                        Assert.Equal("Megapixels >= 4 and Megapixels <= 7", multiFacetResults[1]["Megapixels"].Values[1].Range);
                        Assert.Equal(0, multiFacetResults[1]["Megapixels"].Values[1].Count);
                        Assert.Equal("Megapixels >= 8 and Megapixels <= 10", multiFacetResults[1]["Megapixels"].Values[2].Range);
                        Assert.Equal(0, multiFacetResults[1]["Megapixels"].Values[2].Count);
                        Assert.Equal("Megapixels >= 11", multiFacetResults[1]["Megapixels"].Values[3].Range);
                        Assert.Equal(0, multiFacetResults[1]["Megapixels"].Values[3].Count);
                    }
                }
            }
        }