在 RavenDB 4.2 中,我想创建一个基于动态对象的索引/映射。更好的说法是,在编译时不知道的动态属性。
这是我正在摄取的原始 JSON 的示例:
{
"id": "A",
"detections":
[
{
"steps": [
{
"object": {
"id": "A1",
"target": {
"domain_name": "foobar.com"
}
},
"object": {
"id": "A2",
"target": {
"ipv4": "127.0.0.1"
}
}
}
]
}
]
}
上面的示例是从第 3 方获取的,并存储在 RavenDB 集合中。粗略翻译,以下模型具有挑战:
public class Step
{
public string Id { get; set; }
public DateTime When {get; set;}
public dynamic Object { get; set; } // aware that it's not handy naming
}
这里的问题在于 object.target.X 属性名称是动态的。它们不能是强类型的,可以是很多东西,例如:domain_name、ipv4、ipv6、dns、shoe_size、hair_colour 等。这就是为什么整个 steps.object
被摄取并存储为System.Object
或 dynamic
。
我的目标是基本上对每个 object.target
执行 SelectMany() 并提取属性名称(键)和值。这将使我的 RavenDB 索引变成这样:
public class StepsIndex : AbstractIndexCreationTask<Models.Step, StepsIndex.Result>
{
public class Result
{
public DateTime When { get; set; }
public string TargetKey { get; set; }
public string TargetValue { get; set; }
// ... removed other properties for brevity
}
public StepsIndex()
{
Map = steps =>
from block in blocks
from detection in blocks.Detections
from step in detection.Steps
select new Result
{
// extract property name (key), like 'domain_name'
TargetKey = step.Object.target.GetType().GetProperties()[0].Name,
// extract property value, like 'foobar.com'
TargetValue = step.Object.target.GetType().GetProperty(s.Object.target.GetType().GetProperties()[0].Name).GetValue(s.Object.target, null)
};
}
}
不幸的是,由于 step.Object
是动态的并在编译时导致以下错误,因此这不起作用:
Error [CS1963] An expression tree may not contain a dynamic operation
我尝试过的第二个选项是将其转换为表达式中的 JSON,这也失败了,因为 Raven 的投影在运行时不知道 Newtonsoft.Json:
// Error CS0103: The name 'JObject' does not exist in the current context
// Error CS0103: The name 'JsonConvert' does not exist in the current context
TargetKey = JObject.Parse(JsonConvert.SerializeObject(ass.Object))["target"][0].Value<string>(),
我想到的第三个选项可能是将 dynamic Object
更改为 System.Object Object
,但还没有找到提取属性键/值的巧妙方法在不知道属性(property)的情况下。
问题:如何提取这些动态属性键和值并将它们映射到 RavenDB 索引?
最佳答案
RavenDB 允许索引动态字段: 见:
关于c# - 基于动态属性创建 RavenDB 索引,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57409422/