我正在使用以下结果转换器来尝试从混合文档集中检索更少的数据。所有文档都继承自基类型,但我想从派生类型中撤回一些字段(如果存在)。
这是我的尝试(为简洁起见):
public DocumentAsSearchResultTransformer()
{
TransformResults = docs => from doc in docs
// need several casts to get the properties of the lower cast variables
let subDoc = doc as SubDocumentType
let subDoc2 = doc as SubDocumentType2
select new SearchResult
{
EntityId = doc.EntityId,
Name = doc.Name
// start pulling out document type specific fields
Categories = subDoc != null ? subDocCategories : null,
UserTags = subDoc2 != null ? subDoc2.UserTags : null
};
}
但是,当我尝试将此结果转换器放到服务器上时,我收到以下异常:
System.ServiceModel.ServiceActivationException: The service '/MyService/MyService.svc' cannot be activated due to an exception during compilation. The exception message is: Exception has been thrown by the target of an invocation..
---> System.Reflection.TargetInvocationException: Exception has been thrown by the target of an invocation.
---> Raven.Abstractions.Exceptions.BadRequestException: Could not understand query: Could not understand query:
[DomRegion FileName =,Begin =(3,18),End =( - 1,-1)]:错误 - 意外符号As'
[DomRegion FileName=, Begin=(6, 27), End=(-1, -1)]: Error - Unexpected symbol
As'
[DomRegion FileName =,Begin =(9,33),End =( - 1,-1)]:错误 - 意外符号As' ---> System.Net.WebException: The remote server returned an error: (400) Bad Request.
at System.Net.HttpWebRequest.GetResponse()
at Raven.Client.Connection.HttpJsonRequest.ReadJsonInternal(Func
1 getResponse)
---内部异常堆栈跟踪结束---
在Raven.Client.Connection.HttpJsonRequest.HandleErrors(WebException e)
在Raven.Client.Connection.HttpJsonRequest.ReadJsonInternal(Func 1 getResponse)
at Raven.Client.Connection.HttpJsonRequest.ReadResponseJson()
at Raven.Client.Connection.ServerClient.DirectPutTransformer(String name, OperationMetadata operationMetadata, TransformerDefinition definition)
at Raven.Client.Connection.ReplicationInformer.TryOperation[T](Func
2操作,OperationMetadata operationMetadata,OperationMetadata primaryOperationMetadata,Boolean avoidThrowing,T& result,Boolean& wasTimeout)
在Raven.Client.Connection.ReplicationInformer.ExecuteWithReplication [T](String方法,String primaryUrl,OperationCredentials primaryCredentials,Int32 currentRequest,Int32 currentReadStripingBase,Func 2 operation)
at Raven.Client.Connection.ServerClient.ExecuteWithReplication[T](String method, Func
2操作)
在Raven.Client.Indexes.AbstractTransformerCreationTask.Execute(IDatabaseCommands databaseCommands,DocumentConvention documentConvention)
在Raven.Client.Indexes.IndexCreation.CreateIndexes(ExportProvider catalogToGetnIndexingTasksFrom,IDocumentStore documentStore)
at myService.MyService..ctor()in c:\ Users \ me \ repos \ Admin \ MyService \ MyService.svc.cs:第51行
---内部异常堆栈跟踪结束---
//更多堆栈跟踪(可能不需要)
所以,看起来我错了。获取这些派生类属性的最佳方法是什么?或者我是否尝试了不可能的事情?
任何帮助表示感谢。
答案 0 :(得分:1)
您正在尝试使用服务器上不存在的类型。
您可以通过在客户端的实例上调用AsDocument(doc)
来获取动态行为。
答案 1 :(得分:1)
我不明白尝试使用AsDocument是什么意思,而我尝试使用它会导致丑陋的代码无法正常工作,所以我放弃了它,并尝试提出另一个想法。
事实证明< T>在AbstractTransformerCreationTask中,不必与索引中的文档类型对齐,它只是让您更容易理解智能感知。
因此,为了解决这个问题,我让我的变换器类使用了SearchResult类型作为< T>,以及我返回的类型。
dfA=data.frame(C1=sample(1:92047), C2=sample(1:92047))
listB=list(sample(1:1829))
dfAinB=dfA[which(dfA$C1 %in% unlist(listB)),]
str(dfAinB)
然后我可以正常地从文档中分配属性。
public class DocumentAsSearchResultTransformer : AbstractTransformerCreationTask<SearchResult>
{
...
}
哪个效果很好!
然后我遇到了问题,虽然我不想要一些更大的属性回来,但我想从它们计算一些值。谢天谢地,这很容易解决。如果您在搜索结果模型中包含不希望从搜索中返回的属性,则可以在变换中使用它们,即使您没有从变换中发回它们。
TransformResults = docs => (from doc in docs
select new SearchResult
{
Name = doc.Name,
Categories = doc.Categories, // this is from a derived document type really, but if the doc doesn't have it it'll be null
..
}
由于您已将所选SearchResult的Votes属性保留为null,因此它不会从查询中返回。但是你的计算属性会。
看看它在服务器端进行的转换,这也是一种非常简洁的转换方式。
希望这有助于处理类似情况的其他人。