Indexing RavenDb静态索引:对子集合对象的查询
我的文档结构如下所示:Indexing RavenDb静态索引:对子集合对象的查询,indexing,ravendb,Indexing,Ravendb,我的文档结构如下所示: 雇主=>职位=>所需技能 雇主拥有职位集合 职位拥有所需技能的集合。 所需技能包括技能(字符串)和熟练程度(枚举) 如果我使用动态索引,它似乎可以返回公司,但是我希望使用索引填充MVC视图模型,以返回到UI 我对瑞文来说真的是个新手,所以我为做了任何愚蠢/不必要的事道歉 我得到了以下映射: public class PositionSearch : AbstractIndexCreationTask<Employer> { public
雇主=>职位=>所需技能
雇主拥有职位集合职位拥有所需技能的集合。
所需技能包括技能(字符串)和熟练程度(枚举) 如果我使用动态索引,它似乎可以返回公司,但是我希望使用索引填充MVC视图模型,以返回到UI 我对瑞文来说真的是个新手,所以我为做了任何愚蠢/不必要的事道歉 我得到了以下映射:
public class PositionSearch : AbstractIndexCreationTask<Employer>
{
public PositionSearch()
{
Map = employers =>
from employer in employers
from position in employer.Positions
select new
{
EmployerId = employer.Id,
EmployerName = employer.Name,
PositionId = position.Id,
PositionTitle = position.Title,
position.Location,
position.Description,
RequiredSkills = position.RequiredSkills
};
StoreAllFields(FieldStorage.Yes);
Index("RequiredSkills_Skill", FieldIndexing.Analyzed);
}
}
有谁能看出我做错了什么,或者为我建议另一种方法吗
谢谢
詹姆斯
更新我的视图模型-谢谢:
public class PositionSearchResultModel
{
public PositionSearchResultModel()
{
RequiredSkills = new HashSet<SkillProficiency>();
}
public string EmployerId { get; set; }
public string EmployerName { get; set; }
public string PositionId { get; set; }
public string PositionTitle { get; set; }
public string Location { get; set; }
public string Description { get; set; }
public ICollection<SkillProficiency> RequiredSkills { get; set; }
}
公共类位置搜索结果模型
{
公共位置SearchResultModel()
{
RequiredSkills=新HashSet();
}
公共字符串EmployerId{get;set;}
公共字符串EmployerName{get;set;}
公共字符串PositionId{get;set;}
公共字符串PositionTitle{get;set;}
公共字符串位置{get;set;}
公共字符串说明{get;set;}
公共ICollection RequiredSkills{get;set;}
}
因为要对技能名称进行分析搜索,所以需要将其作为单独的索引项进行隔离
public class PositionSearch
: AbstractIndexCreationTask<Employer, PositionSearchResultModel>
{
public PositionSearch()
{
Map = employers =>
from employer in employers
from position in employer.Positions
select new
{
EmployerId = employer.Id,
EmployerName = employer.Name,
PositionId = position.Id,
PositionTitle = position.Title,
position.Location,
position.Description,
position.RequiredSkills,
// Isolate the search property into it's own value
SkillsSearch = position.RequiredSkills.Select(x => x.Skill)
};
// you could store all fields if you wanted, but the search field
// doesn't need to be stored so that would be wasteful.
Store(x => x.PositionId, FieldStorage.Yes);
Store(x => x.PositionTitle, FieldStorage.Yes);
Store(x => x.Location, FieldStorage.Yes);
Store(x => x.Description, FieldStorage.Yes);
Store(x => x.RequiredSkills, FieldStorage.Yes);
// Any field you are going to use .Search() on should be analyzed.
Index(x => x.SkillsSearch, FieldIndexing.Analyzed);
}
}
它是什么类型真的不重要。字符串数组或集合就可以了。您也可以只使用字符串或对象,因为它只是相关的名称
查询此索引时,请使用.Search()
方法,如下所示:
var results = session.Query<PositionSearchResultModel, PositionSearch>()
.Customize(x => x.WaitForNonStaleResults()) // only use this in testing
.Search(x=> x.SkillsSearch, "SkillName")
.ProjectFromIndexFieldsInto<PositionSearchResultModel>() // AsProjection would also work
.ToList();
var results=session.Query()
.Customize(x=>x.WaitForNonSaleResults())//仅在测试中使用此选项
.Search(x=>x.SkillsSearch,“SkillName”)
.ProjectFromIndexFieldsInto()//项目也可以工作
.ToList();
请注意,您必须存储这么多字段的唯一原因是您希望投影它们。如果您将这些位置分离到它们自己的文档中,您将拥有更小的索引和更少的项目。请记住,在进行项目时,原始文档中的所有字段都已经存在,并且直接来自文档存储,而不必复制到索引中。因此,如果您的原始文档与您期望的结果更接近,那么所做的工作就会更少。您将索引项与索引结果混淆了。请提供您的
PositionSearchResultModel
课程,我将给出完整的解决方案。谢谢。我看到你想把这个字段标记为已分析的。你想在技能名称上精确匹配吗?或者你想做一个分析搜索吗?我想这需要是一个分析搜索,因为我可能不总是有完整的技能名称。我需要做类似的事情,但我不需要查看结果,只需检查是否存在技能。这是否意味着我不需要存储这些字段?@DanyW-如果您只是在where子句中使用这些字段,则不需要存储它们。只有当您要将它们投影回结果集时,才需要存储它们。
public class PositionSearch
: AbstractIndexCreationTask<Employer, PositionSearchResultModel>
{
public PositionSearch()
{
Map = employers =>
from employer in employers
from position in employer.Positions
select new
{
EmployerId = employer.Id,
EmployerName = employer.Name,
PositionId = position.Id,
PositionTitle = position.Title,
position.Location,
position.Description,
position.RequiredSkills,
// Isolate the search property into it's own value
SkillsSearch = position.RequiredSkills.Select(x => x.Skill)
};
// you could store all fields if you wanted, but the search field
// doesn't need to be stored so that would be wasteful.
Store(x => x.PositionId, FieldStorage.Yes);
Store(x => x.PositionTitle, FieldStorage.Yes);
Store(x => x.Location, FieldStorage.Yes);
Store(x => x.Description, FieldStorage.Yes);
Store(x => x.RequiredSkills, FieldStorage.Yes);
// Any field you are going to use .Search() on should be analyzed.
Index(x => x.SkillsSearch, FieldIndexing.Analyzed);
}
}
public string[] SkillsSearch { get; set; }
var results = session.Query<PositionSearchResultModel, PositionSearch>()
.Customize(x => x.WaitForNonStaleResults()) // only use this in testing
.Search(x=> x.SkillsSearch, "SkillName")
.ProjectFromIndexFieldsInto<PositionSearchResultModel>() // AsProjection would also work
.ToList();