Indexing RavenDB:如何从MultiMapIndex中正确查询/筛选嵌套值?
我的应用程序要求能够通过相关Indexing RavenDB:如何从MultiMapIndex中正确查询/筛选嵌套值?,indexing,ravendb,Indexing,Ravendb,我的应用程序要求能够通过相关联系人的编号过滤/搜索对 对始终存储了对联系人的引用,但该联系人的号码不会也不会存储在引用中。因此,我尝试为此创建一个自定义索引,因为对和联系人存储在不同的集合中 索引的简化示例如下所示 public class Pairs_Search : AbstractMultiMapIndexCreationTask<Pairs_Search.Result> { public class Result { public string
联系人的编号
过滤/搜索对
对
始终存储了对联系人的引用
,但该联系人的号码不会也不会存储在引用中。因此,我尝试为此创建一个自定义索引,因为对
和联系人
存储在不同的集合中
索引的简化示例如下所示
public class Pairs_Search : AbstractMultiMapIndexCreationTask<Pairs_Search.Result>
{
public class Result
{
public string Id { get; set; }
public string Workspace { get; set; }
public ContactResult Contact { get; set; }
public bool HasContactDetails { get; set; }
}
public class ContactResult
{
public string Id { get; set; }
public string Name { get; set; }
public int Number { get; set; }
}
public Pairs_Search()
{
AddMap<Pair>(pairs => pairs
.Select(p => new
{
p.Id,
p.Workspace,
Contact = new
{
p.Contact.Id,
p.Contact.Name,
Number = 0
},
// Mark this items as WITHOUT contact details.
HasContactDetails = false,
}
)
);
AddMap<Contact>(contacts => contacts
.Select(c => new
{
Id = (string) null,
Workspace = (string) null,
Contact = new
{
c.Id,
Name = c.DisplayName,
c.Number
},
// Mark this items as WITH contact details.
HasContactDetails = true,
}
)
);
Reduce = results => results
// First group by the contact ID. This will
// create a group with 2 or more items. One with the contact
// details, and one or more with pair details.
// They are all marked by a boolean flag 'HasContactDetails'.
.GroupBy(x => x.Contact.Id)
// We are going to enrich each item in the current group, that is
// marked as 'HasContactDetails = false', with the contact number.
// We need that so that we can filter on it later.
.Select(group =>
group
.Select(i => new
{
i.Id,
i.Workspace,
Contact = new
{
i.Contact.Id,
i.Contact.Name,
// Does the current item have the contact details?
Number = i.HasContactDetails
// Yes, in this case we use the previously set contact number.
? i.Contact.Number
// No, find the item with the contact details and grab the number.
: group.Single(x => x.HasContactDetails).Contact.Number
},
// Pass on the flag that indicates wheter or not
// this item has the contact details. We are going
// to need it later.
i.HasContactDetails
}
)
// We don't need the items with the contact details
// anymore, so filter them out.
.Where(x => !x.HasContactDetails)
)
// Flatten all the small lists to one big list.
.SelectMany(x => x);
// Mark the following fields of the result as searchable.
Index(x => x.Contact.Number, FieldIndexing.Search);
}
}
对非嵌套值进行过滤也有效(在完整示例中也可用)。正如您所看到的,它过滤掉了具有不同工作空间的一个
private static async Task ThisOneWithWorkspaceFilterWorks()
{
using (var session = Store.OpenAsyncSession())
{
var results = await session
.Query<Pairs_Search.Result, Pairs_Search>()
.Where(x => x.Workspace == "hogwarts")
.ToListAsync();
LogResults("ThisOneWithWorkspaceFilterWorks()", results);
}
// Output:
// ThisOneWithWorkspaceFilterWorks(): Pair 'Harry Potter' with number '70'
// ThisOneWithWorkspaceFilterWorks(): Pair 'Harry Potter' with number '70'
// ThisOneWithWorkspaceFilterWorks(): Pair 'Hermione Granger' with number '71'
}
有人能告诉我我做错了什么吗?任何帮助都将不胜感激 方法是将ContactResult存储在不同的集合中,
这就是所谓的相关文档在这种情况下,
当您创建索引时,您就为相关文档编制了索引
从中的演示示例学习:
该示例适用于基本地图索引,但多地图索引的原理相同
从索引类中删除公共类ContactResult
并使用以下内容定义索引:
select new Result
{
....
Number = LoadDocument<Contact>(Pair.Contact).Number
....
}
选择新结果
{
....
编号=LoadDocument(Pair.Contact)。编号
....
}
谢谢!我试过了,它似乎奏效了!有关更新的示例,请参见。这是一个简单得多的方法。但这并不能解释为什么我的例子不起作用!
private static async Task ThisOneWithWorkspaceAndNumberFilterDoesntWork()
{
using (var session = Store.OpenAsyncSession())
{
var results = await session
.Query<Pairs_Search.Result, Pairs_Search>()
.Where(x => x.Workspace == "hogwarts")
.Where(x => x.Contact.Number == 70)
.ToListAsync();
LogResults("ThisOneWithWorkspaceAndNumberFilterDoesntWork()", results);
}
// Output:
// ThisOneWithWorkspaceAndNumberFilterDoesntWork(): EMPTY RESULTS!
}
select new Result
{
....
Number = LoadDocument<Contact>(Pair.Contact).Number
....
}