Пример #1
0
        public void Can_create_index_using_linq_from_client_using_map_reduce()
        {
            documentStore.DatabaseCommands.PutIndex("UsersCountByLocation", new IndexDefinitionBuilder <LinqIndexesFromClient.User, LinqIndexesFromClient.LocationCount>
            {
                Map = users => from user in users
                      where user.Location == "Tel Aviv"
                      select new { user.Location, Count = 1 },
                Reduce = results => from loc in results
                         group loc by loc.Location
                         into g
                         select new { Location = g.Key, Count = g.Sum(x => x.Count) },
                Indexes = { { x => x.Location, FieldIndexing.NotAnalyzed } }
            });

            using (var session = documentStore.OpenSession())
            {
                session.Store(new LinqIndexesFromClient.User
                {
                    Location = "Tel Aviv",
                    Name     = "Yael"
                });

                session.SaveChanges();

                LinqIndexesFromClient.LocationCount single = session.Advanced.LuceneQuery <LinqIndexesFromClient.LocationCount>("UsersCountByLocation")
                                                             .Where("Location:\"Tel Aviv\"")
                                                             .WaitForNonStaleResults()
                                                             .Single();

                Assert.Equal("Tel Aviv", single.Location);
                Assert.Equal(1, single.Count);
            }
        }
        public void Can_create_index_using_linq_from_client_using_map_reduce()
        {
            using (var server = GetNewServer(port, path))
            {
                var documentStore = new DocumentStore {
                    Url = "http://localhost:" + port
                };
                documentStore.Initialize();
                documentStore.DatabaseCommands.PutIndex("UsersCountByLocation", new IndexDefinition <LinqIndexesFromClient.User, LinqIndexesFromClient.LocationCount>
                {
                    Map = users => from user in users
                          where user.Location == "Tel Aviv"
                          select new { user.Location, Count = 1 },
                    Reduce = results => from loc in results
                             group loc by loc.Location into g
                             select new { Location = g.Key, Count = g.Sum(x => x.Count) },
                });

                using (var session = documentStore.OpenSession())
                {
                    session.Store(new LinqIndexesFromClient.User
                    {
                        Location = "Tel Aviv",
                        Name     = "Yael"
                    });

                    session.SaveChanges();

                    LinqIndexesFromClient.LocationCount single = session.LuceneQuery <LinqIndexesFromClient.LocationCount>("UsersCountByLocation")
                                                                 .Where("Location:Tel Aviv")
                                                                 .WaitForNonStaleResults()
                                                                 .Single();

                    Assert.Equal("Tel Aviv", single.Location);
                    Assert.Equal(1, single.Count);
                }
            }
        }