我正在使用RavenDB 2.5,我想要做的是查询Group
(见下文),提供有效的Lucene搜索词并获取Member
个实例的集合(甚至是只是Ids)匹配。所以,课程定义:
public class Group {
public string Id { get; set; }
public IList<Member> Members { get; set; }
}
public class Member {
public string Name { get; set; }
public string Bio { get; set; }
}
它们以session.Store(groupInstance);
的形式存储在数据库中,正如您所期望的那样。我要做的是查询并返回与给定搜索字词匹配的Member
个实例。
所以,比如:
public class GroupMembers_BySearchTerm : AbstractIndexCreationTask {
public override IndexDefinition CreateIndexDefinition(){
return new IndexDefinition {
Map = "from g in docs.Groups select new { Content = new [] { g.Members.Select(m => m.Name), g.Members.Select(m => m.Bio) }, Id = g.Id }",
Indexes = { { "Id", FieldIndexing.Default }, { "Content", FieldIndexing.Analyzed } }
}
}
}
如果我使用类似的东西来打电话:
session.Advanced.LuceneQuery<Group, GroupMembers_BySearchTerm>().Where("Id: myId AND Content: search terms").ToList();
我显然得到了一个Group实例,但我怎样才能取回会员呢?
答案 0 :(得分:0)
这样的指数怎么样:
public class Members_BySearchTermAndGroup : AbstractIndexCreationTask {
public override IndexDefinition CreateIndexDefinition(){
return new IndexDefinition {
Map = "from g in docs.Groups
from member in g.Members
select new {
GroupdId = g.Id,
Name = member.Name,
Bio = member.Bio,
Content = new [] {member.Name, member.Bio },
}",
Indexes = {
{ "GroupId", FieldIndexing.Default },
{ "Content", FieldIndexing.Analyzed }
},
Stores = {
{ "Name", FieldStorage.Yes },
{ "Bio", FieldStorage.Yes }
}
}
}
}
如果你仔细看一下,你会发现我们正在为一个组内的每个成员创建一个新的lucene条目。因此,您将能够查询这些元素并检索它们。
最后,您可以像这样查询您的商店(有关searching的更多信息):
session.Query<Member, Members_BySearchTermAndGroup>()
.Search(x => x.Content, "search terms")
.ProjectFromIndexFieldsInto<Member>()
.ToList();
我现在无法检查,但我猜您需要使用ProjectFromIndexFieldsInto()来预测结果。有关this link中投影的更多信息。
或者,按照你的例子:
session.Advanced
.LuceneQuery<Member, Members_BySearchTermAndGroup>()
.Where("GroupId: myId AND Content: search terms")
.SelectFields<Member>()
.ToList();