[英]How do I map a single .NET type to multiple nested object types in ElasticSearch/NEST?
I'm using the NEST library to interact with ElasticSearch, and I'm trying to figure out a way to build index types/nested objects based on non-type data. 我正在使用NEST库与ElasticSearch进行交互,我正试图找出一种基于非类型数据构建索引类型/嵌套对象的方法。 The type has the following basic structure. 该类型具有以下基本结构。
public class Entity : DynamicObject
{
public string Id { get; set; }
// a bunch of other simple properties
public override IEnumerable<string> GetDynamicMemberNames()
{
return Data.Select(x => x.Name);
}
public override bool TryGetMember(GetMemberBinder binder, out object result)
{
var dictionary = Data.First(x => x.Name == binder.Name);
result = dictionary;
return true;
}
// each instance of one these should be a nested object type
public IList<NestedType> Data { get; set; }
public class NestedType
{
// How do I make Name be the nest type name?
public string Name { get; set; }
public IDictionary<string, string> Values { get; set; }
}
}
I want to create a nested object/type for each instance of NestedType. 我想为NestedType的每个实例创建一个嵌套对象/类型。 So if there are two instances of NestedType, there will then be two nested objects. 因此,如果有两个NestedType实例,那么将有两个嵌套对象。 I can inherit NestedType from DynamicObject to turn the dictionary into "real" properties that NEST then maps correctly (ie, turn each dictionary key into a property). 我可以从DynamicObject继承NestedType,将字典转换为NEST然后正确映射的“真实”属性(即,将每个字典键转换为属性)。 The problem is that I can't figure out how to set the name/type of the nested object. 问题是我无法弄清楚如何设置嵌套对象的名称/类型。
There are two ways to map names that I know of: ElasticType attribute and NestedObject fluent interface. 我知道有两种映射名称的方法:ElasticType属性和NestedObject流畅的接口。 The problem here is that there is a single type that represents multiple nested object types. 这里的问题是有一种类型代表多个嵌套对象类型。 I could do some runtime type building, but I'd rather not if I can avoid it. 我可以做一些运行时类型的构建,但是如果我可以避免它,我宁愿不做。
Is there a way to have a method or property be used as the nested object's name/type? 有没有办法让方法或属性用作嵌套对象的名称/类型? Or is there a better approach to mapping this type of data to ElasticSearch (hopefully via NEST)? 或者有更好的方法将这种类型的数据映射到ElasticSearch(希望通过NEST)?
Thanks! 谢谢! Erick 埃里克
EDIT 编辑
I updated the entity definition to reflect what I'm doing (using DynamicObject to get the JsonSerializer to do what I want). 我更新了实体定义以反映我正在做的事情(使用DynamicObject让JsonSerializer做我想做的事)。 What I want is the ability for the different dictionaries to have different mappings, (different stemming, analyzers, etc). 我想要的是不同字典具有不同映射的能力(不同的词干,分析器等)。 If there were proper types, I could use the NEST fluent syntax to set it up, but when using dynamic, there is no type for the fluent API to use. 如果有合适的类型,我可以使用NEST流畅的语法来设置它,但是当使用动态时,没有类型可供使用的流畅API。 Ultimately, I want to mix the fluent API with a string based on strings instead of types. 最后,我想将流畅的API与基于字符串而不是类型的字符串混合使用。 Does this make sense? 这有意义吗?
If I understand correctly your intention, Entity
object will have only nested objects in it, won't it? 如果我理解你的意图, Entity
对象将只有嵌套对象,不是吗?
You can try to use dynamic mapping functionality of elasticsearch for entity object. 您可以尝试使用elasticsearch的动态映射功能来实现实体对象。 I assume Entity is a root object. 我假设Entity是一个根对象。
curl -X POST localhost:9200/myindex/entity/_mapping
{"dynamic_templates": [
{"nested_data_template": {
"mapping": {
"type": "nested" },
"match_mapping_type": "object",
"path_match": "*" }}]}
path_match: *
and match_mapping_type: object
mean that for all field names with object as a value nested type mapping will be applied. path_match: *
和match_mapping_type: object
表示对于所有以对象作为值的字段名称,将应用嵌套类型映射。
Using NEST and Fluent API you can use the following API. 使用NEST和Fluent API,您可以使用以下API。 IntelliSense will guide you how to build mapping above. IntelliSense将指导您如何构建上面的映射。 ;) ;)
descriptor.DynamicTemplates(DynamicTemplatesDescriptor<Entity>)
Every time when a new property matching this template appears, elasticsearch will update mapping based on dynamic mapping. 每当出现与此模板匹配的新属性时,elasticsearch将根据动态映射更新映射。 After a while your mapping will look like: 一段时间后,您的映射将如下所示:
{
"entity": {
"mappings": {
"entity": {
"dynamic_templates": [
{
"nested_data_template": {
"mapping": {
"type": "nested"
},
"match_mapping_type": "object",
"path_match": "*"
}
}
],
"properties": {
"test": {
"type": "nested",
"properties": {
"test": {
"type": "string"
},
"another_property": {
"type": "string"
}
}
},
"test1": {
"type": "nested",
"properties": {
"test": {
"type": "string"
}
}
}
}
}
}
}
}
Hope this will help! 希望这会有所帮助!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.