我有下一个索引:
public class TestIndex : AbstractIndexCreationTask<Resource>
{
public class Result
{
public string Caption { get; set; }
public string TestVal{ get; set; }
}
public TestIndex()
{
Map = resources => from r in resources
select new
{
Caption = r.Caption,
TestVal = r.Caption
};
}
}
我就是这样查询的:
var data = session.Query<Resource, TestIndex>()
.Customize(x => x.WaitForNonStaleResults())
.AsProjection<TestIndex.Result>()
.ToList();
问题是,当Caption填充期望值时,查询后每个对象的TestVal属性都为null。
如果您想从索引进行投影,您需要存储值
我也遇到过类似的问题,对索引的查询仍然检索到空值。事实证明,我做了快速测试——在每个程序运行后,索引也在运行,它没有足够的时间进行编译。在这种情况下,解决方案是使用自定义。等待非过期结果():
Query<ResultType,IndexType>()
.Customize(customization => customization.WaitForNonStaleResultsAsOfNow()) //this is imprtant when you using quick tests, not in production server
.Where(...).AsProjection<ResultType>
您还需要记住将数据从索引存储到数据库,并在索引类中使用
StoreAllFields(FieldStorage.Yes);