3

マルチ マップ インデックスがあり、Reduce の結果で最大の日付を取得する必要があります。

私がしようとすると:

Reduce = results => from result in results
                    group result by result.Id into g
                    select new
                    {
                        Id = g.Key,
                        AccountId = g.Select(x => x.AccountId).Where(x => x != null).FirstOrDefault(),
                        Hostnames = g.SelectMany(x => x.Hostnames),
                        NextRenewalDate = g.Select(x => x.NextRenewalDate).Max(),
                        SubscriptionCount = g.Sum(x => x.SubscriptionCount)
                    };

NextRenewalDate常にヌルです。

ただし、次のことができます。

NextRenewalDate = g.Select(x => x.NextRenewalDate).FirstOrDefault,

そして、私は結果を得ます。

これは、RavenDB がインデックス内の日付を解釈する方法によるものでしょうか。私の Reduce Result クラスは以下のとおりです。

public class ReduceResult
{
    public string Id { get; set; }
    public string AccountId { get; set; }
    public string[] Hostnames { get; set; }
    public DateTime NextRenewalDate { get; set; }
    public int SubscriptionCount { get; set; }
}
アップデート

それ以来、インデックスを機能させることができましたがDateTime、ISO8601 文字列にキャストした場合のみです。私の完全なインデックスは以下のとおりです。

public class Account_Sites : AbstractMultiMapIndexCreationTask<Account_Sites.ReduceResult>
{
    public Account_Sites()
    {
        AddMap<CMS.Domain.Site>(sites => from site in sites
                                         from url in site.Urls
                                         select new
                                         {
                                             Id = site.Id,
                                             AccountId = (string)null,
                                             Hostnames = new[] { url.Hostname },
                                             SubscriptionDueDate = DateTime.MinValue,
                                             SubscriptionStartDate = DateTime.MinValue
                                         });

        AddMap<Domain.Account>(accounts => from account in accounts
                                           from siteId in account.Sites
                                           from subscription in account.Subscriptions
                                           where subscription != null
                                           select new
                                           {
                                               Id = siteId,
                                               AccountId = account.Id,
                                               Hostnames = new string[0],
                                               SubscriptionDueDate = subscription.NextRenewalDate,
                                               SubscriptionStartDate = subscription.StartDate
                                           });

        Reduce = results => from result in results
                            group result by result.Id into g
                            select new
                            {
                                Id = g.Key,
                                AccountId = g.Select(x => x.AccountId).Where(x => x != null).FirstOrDefault(),
                                Hostnames = g.SelectMany(x => x.Hostnames),
                                SubscriptionDueDate = g.Max(x => x.SubscriptionDueDate.ToString("o")), // ISO 8601 format e.g. 2012-10-22T12:51:03.0263843+00:00
                                SubscriptionStartDate = g.Max(x => x.SubscriptionStartDate.ToString("o"))
                            };
    }

    public class ReduceResult
    {
        public string Id { get; set; }
        public string AccountId { get; set; }
        public string[] Hostnames { get; set; }
        public DateTime SubscriptionDueDate { get; set; }
        public DateTime SubscriptionStartDate { get; set; }
    }
}

更新 2

以下に完全に失敗したテストを投稿しました。ダニエルのテストは機能しますが、以下は失敗します。ただし、TestSubscription.EndDateDateTime.Now(の代わりに)に変更すると機能します。UtcNowこれはビルド1.2.2103-Unstableを使用しています。

public class TestAccount
{
    public string Id { get; set; }
    public List<string> Sites { get; set; }
    public List<TestSubscription> Subscriptions { get; set; }

    public TestAccount()
    {
        Sites = new List<string>();
        Subscriptions = new List<TestSubscription>();
    }
}

public class TestSubscription
{
    public DateTime EndDate { get; set; }
}

public class TestSite
{
    public string Id { get; set; }
    public string Hostname { get; set; }
}

public class SitesWithSubscriptions : AbstractMultiMapIndexCreationTask<SitesWithSubscriptions.Result>
{
    public SitesWithSubscriptions()
    {
        AddMap<TestSite>(sites => from site in sites
                                  select new
                                  {
                                      SiteId = site.Id,
                                      Hostname = site.Hostname,
                                      SubscriptionEndDate = DateTime.MinValue
                                  });

        AddMap<TestAccount>(accounts => from account in accounts
                                        from siteId in account.Sites
                                        from subscription in account.Subscriptions
                                        select new
                                        {
                                            SiteId = siteId,
                                            Hostname = (string)null,
                                            SubscriptionEndDate = subscription.EndDate
                                        });

        Reduce = results => from result in results
                            group result by result.SiteId into g
                            select new
                            {
                                SiteId = g.Key,
                                Hostname = g.Select(x => x.Hostname).Where(x => x != null).FirstOrDefault(),
                                SubscriptionEndDate = g.Max(x => x.SubscriptionEndDate)
                            };
    }

    public class Result
    {
        public string SiteId { get; set; }
        public string Hostname { get; set; }
        public DateTime SubscriptionEndDate { get; set; }
    }
}

    public class SitesSpecs : RavenSpecs
{
    static IEnumerable<SitesWithSubscriptions.Result> results;

    Establish ctx = () =>
    {
        using (var session = Store.OpenSession())
        {
            var site = new TestSite { Hostname = "www.dev.com" };
            session.Store(site);

            var account = new TestAccount();
            account.Subscriptions.Add(new TestSubscription { EndDate = DateTime.UtcNow });
            account.Sites.Add(site.Id);
            session.Store(account);

            session.SaveChanges();
        }           
    };

    Because of = () =>
    {
        using (var session = Store.OpenSession())
        {
            results = session.Query<SitesWithSubscriptions.Result, SitesWithSubscriptions>()
                .Customize(x => x.WaitForNonStaleResults())
                .ToList();
        }
    };

    It Should_return_sites_with_subscription_end_dates = ()
        =>
    {
        results.Count().ShouldEqual(1);
        results.First().SubscriptionEndDate.Date.ShouldEqual(DateTime.UtcNow.Date);
    };
}
4

2 に答える 2

3

これを行う方法を示す簡単なサンプルを次に示します。

更新: RavenDB がリクエストどおりに DateTime を処理できることを確認するために、このコードを作成しました。ISO で文字列に変換する必要はありません... ビルド 960 で試しました。うまくいかない場合は、失敗したテストを提供し、使用しているビルドをお知らせください。

public class Student
{
    public string Id { get; set; }
    public string Classroom { get; set; }
    public DateTime Birthday { get; set; }
}

public class LatestBirthdayInClassroom : AbstractIndexCreationTask<Student, LatestBirthdayInClassroom.Result>
{
    public class Result
    {
        public string Classroom { get; set; }
        public DateTime LatestBirthday { get; set; }
    }

    public LatestBirthdayInClassroom()
    {
        Map = students => from student in students
                       select new
                       {
                           Classroom = student.Classroom,
                           LatestBirthday = student.Birthday
                       };

        Reduce = results => from result in results
                            group result by result.Classroom into g
                            select new
                            {
                                Classroom = g.Key,
                                LatestBirthday = g.Select(x => x.LatestBirthday).Max()
                            };
    }
}

class Program
{
    static void Main(string[] args)
    {
        using (var store = new EmbeddableDocumentStore
        {
            RunInMemory = true
        }.Initialize())
        {
            using (var session = store.OpenSession())
            {
                session.Store(new Student { Classroom = "A", Birthday = new DateTime(2012, 2, 1) });
                session.Store(new Student { Classroom = "A", Birthday = new DateTime(2012, 5, 1) });
                session.Store(new Student { Classroom = "A", Birthday = new DateTime(2012, 3, 1) });
                session.Store(new Student { Classroom = "B", Birthday = new DateTime(2012, 8, 1) });
                session.Store(new Student { Classroom = "B", Birthday = new DateTime(2012, 11, 1) });
                session.Store(new Student { Classroom = "B", Birthday = new DateTime(2012, 9, 1) });
                session.SaveChanges();
            }

            new LatestBirthdayInClassroom().Execute(store);

            // wait for indexing
            while (store.DatabaseCommands.GetStatistics().StaleIndexes.Length > 0)
            {
                Thread.Sleep(100);
            }

            using (var session = store.OpenSession())
            {
                var results = session.Query<LatestBirthdayInClassroom.Result, LatestBirthdayInClassroom>()
                    .ToList();
            }
        }
    }
}
于 2012-11-14T12:11:39.960 に答える
1

これはRavenDBのバグであることが判明し、ビルド2145で修正されました。詳細についてはhttp://issues.hibernatingrhinos.com/issue/RavenDB-718をご覧ください。

于 2012-11-29T13:36:38.773 に答える