我有一个RavenDB Linq查询,它使用结果转换器正常工作,我试图转换为Lucene语法,以便于创建动态查询。但是,我无法让Lucene查询正常工作,我正在寻找一些指导。
结果变形金刚:
public class CallRecordWithDisplayNameTransformer : AbstractTransformerCreationTask<CallRecord>
{
public CallRecordWithDisplayNameTransformer()
{
TransformResults = callRecords => from callRecord in callRecords
let displayNameRecord = LoadDocument<DisplayName>(callRecord.PhoneNumber)
let displayName = (displayNameRecord != null) ? displayNameRecord.Name : string.Empty
select new{
CallRecord = callRecord,
DisplayName = displayName
};
}
}
由变换器创建的结果类:
public class CallRecordWithDisplayName
{
public CallRecord CallRecord { get; set; }
public string DisplayName { get; set; }
}
以下是成功运行的Linq查询(执行此查询时,将返回完全填充的&amp;转换的'CallRecordWithDisplayName'对象中的25个):
session.Query<CallRecord>()
.Customize(x => x.WaitForNonStaleResults(TimeSpan.FromSeconds(5)))
.OrderByDescending(c => c.CallDateTime)
.TransformWith<CallRecordWithDisplayNameTransformer, CallRecordWithDisplayName>()
.Skip(0)
.Take(25)
.ToList();
这是Lucene查询无法正常工作。执行此查询时,它将返回25个 null 实例的“CallRecord”对象,而不是由变换器创建的“CallRecordWithDisplayName”对象。
session.Advanced.LuceneQuery<CallRecord>()
.OrderByDescending("CallDateTime")
.Skip(0)
.Take(25)
.SetResultTransformer(new CallRecordWithDisplayNameTransformer().TransformerName)
.ToList();
以下是显示正在为Linq(请求#584)和Lucene(请求#585)请求发送到服务器的查询的服务器日志:
Request # 584: GET - 7 ms - FiddleFlights - 200 - /indexes/dynamic/CallR
ecords?&pageSize=25&sort=-CallDateTime&resultsTransformer=CallRecordWithDisplayN
ameTransformer&operationHeadersHash=525986370
Query:
Time: 6 ms
Index: Auto/xz2MTg77IKTTb7MyHLD/4A==
Results: 25 returned out of 597 total.
Request # 585: GET - 7 ms - FiddleFlights - 200 - /indexes/dynamic/CallR
ecords?&pageSize=25&sort=-CallDateTime&resultsTransformer=CallRecordWithDisplayN
ameTransformer
Query:
Time: 5 ms
Index: Auto/xz2MTg77IKTTb7MyHLD/4A==
Results: 25 returned out of 597 total.
答案 0 :(得分:1)
尝试创建CallRecords的静态索引并像这样调用它:
session.Advanced.LuceneQuery<CustomerNameViewModel, Customers_ByName>()
.OrderByDescending("Name")
.Skip(0)
.Take(25)
.SetResultTransformer(new CustomerNameTransformer().TransformerName)
.ToList();
编辑:或者像这样使用自动索引:
session.Advanced.LuceneQuery<CustomerNameViewModel>("Auto/Customers/ByName")
在我的情况下,客户是您的CallRecord。
这适用于我在RavenDB 3.0.3599中,但是,如果你使用3.0,你应该使用DocumentQuery&lt;&gt;而不是LuceneQuery,因为后者已经过时(但它有效)。
但是,我不确定这是&#34;正确的&#34;这样做的方法,但是当与.SetResultTransformer(...)一起使用时,我找不到另一种定义Transformer返回类型的方法
希望这有帮助!
编辑:这是我的变换器,用奇怪的LoadDocument构建,只是为了测试。所以它没有做任何真实的&#34;感:
public class CustomerNameTransformer : AbstractTransformerCreationTask<Customer>
{
public CustomerNameTransformer()
{
TransformResults = results => from customer in results
let fixture = LoadDocument<Fixture>(customer.FixtureNumber)
select new CustomerNameViewModel
{
Id = customer.Id,
Name = customer.Name,
FixtureNumber = fixture.FixtureNumber,
Customer = customer
};
}
}