Multi Map Ravendb根据另一个集合中的文档查找文档

时间:2015-03-05 18:28:04

标签: c# ravendb

我在Ravendb有2个收藏。 IdentifiedDupEmployees 包含重复的员工信息以及重复文档的数量。 员工包含整个员工文档。我基本上需要根据 IdentifiedDupEmployees 中的firstname,lastname,DateofBirth,address1找到员工集合中的员工文档。

我写了一个多地图索引。请帮我调整一下。由于目前它正在返回Employee集合中的所有内容,因此我只想要从员工集合中返回的那些文档,其员工详细信息存在于IdentifiedDupEmployees集合中

public class IdentifiedDupemployees {
            public string FirstName { get; set; }
            public string LastName { get; set; }
            public string DateOfBirth { get; set; }
            public string address1 { get; set; }
            public int count {get; set;}
}

Public class employee 
{
           public string Ids {get; set;}
            public string FirstName { get; set; }
            public string LastName { get; set; }
            public string DateOfBirth { get; set; }
            public string address1 { get; set; }
}


namespace DuplicatePatient.Storage.Indexes
{
    class DupFindPatientId: AbstractMultiMapIndexCreationTask<DupFindPatientId.Result>
    {

        public class Result
        {

            public string Ids { get; set; }
            public string FirstName { get; set; }
            public string LastName { get; set; }
            public string DateOfBirth { get; set; }

            public string address1 { get; set; }
        }
        public DupFindPatientId()
        {

            AddMap<IdentifiedDupPatients>(entities => from a in entities

                                                      select new
                                                      {
                                                          Ids = (string)null,
                                                          FirstName = a.FirstName,
                                                          LastName = a.LastName,
                                                          DateOfBirth = a.DOB,
                                                          address1 = a.address1
                                                      });

            AddMap<Patient>(entities => from b in entities
                                        //from c in b.FirstName
                                        select new
                                        {
                                            Ids = b.Id,
                                            FirstName = b.FirstName,
                                            LastName = b.LastName,
                                            DateOfBirth = b.DateOfBirth,
                                            address1 = b.Address1
                                        });

            Reduce = results => from result in results
                                group result by new
                                {
                                    result.FirstName,
                                    result.LastName,
                                    result.DateOfBirth,
                                    result.address1

                                   }
                                    into g   

                                    select new
                                    {
                                        Ids = g.Select(x => x.Ids).Where(x => x != null).First(),
                                        FirstName = g.Key.FirstName,
                                        LastName = g.Key.LastName,
                                        DateOfBirth = g.Key.DateOfBirth,
                                        address1 = g.Key.address1

                                    };
        }

1 个答案:

答案 0 :(得分:0)

我更改了索引以包含重复标记,您可以在查询期间对其进行过滤

public class IdentifiedDupemployees {
            public string FirstName { get; set; }
            public string LastName { get; set; }
            public string DateOfBirth { get; set; }
            public string address1 { get; set; }
            public int count {get; set;}
}

Public class employee 
{
           public string Ids {get; set;}
            public string FirstName { get; set; }
            public string LastName { get; set; }
            public string DateOfBirth { get; set; }
            public string address1 { get; set; }
}


namespace DuplicatePatient.Storage.Indexes
{
    class DupFindPatientId: AbstractMultiMapIndexCreationTask<DupFindPatientId.Result>
    {

        public class Result
        {

            public string Ids { get; set; }
            public string FirstName { get; set; }
            public string LastName { get; set; }
            public string DateOfBirth { get; set; }

            public string address1 { get; set; }
        }
        public DupFindPatientId()
        {

            AddMap<IdentifiedDupPatients>(entities => from a in entities

                                                      select new
                                                      {
                                                          Ids = (string)null,
                                                          FirstName = a.FirstName,
                                                          LastName = a.LastName,
                                                          DateOfBirth = a.DOB,
                                                          address1 = a.address1,
                                                          Duplicate = true
                                                      });

            AddMap<Patient>(entities => from b in entities
                                        //from c in b.FirstName
                                        select new
                                        {
                                            Ids = b.Id,
                                            FirstName = b.FirstName,
                                            LastName = b.LastName,
                                            DateOfBirth = b.DateOfBirth,
                                            address1 = b.Address1,
                                            Duplicate = false
                                        });

            Reduce = results => from result in results
                                group result by new
                                {
                                    result.FirstName,
                                    result.LastName,
                                    result.DateOfBirth,
                                    result.address1

                                   }
                                    into g   

                                    select new
                                    {
                                        Ids = g.Select(x => x.Ids).Where(x => x != null).First(),
                                        FirstName = g.Key.FirstName,
                                        LastName = g.Key.LastName,
                                        DateOfBirth = g.Key.DateOfBirth,
                                        address1 = g.Key.address1,
                                        Duplicate = g.Any(x=>Duplicate)
                                    };
        }