问题描述
我正在使用NEST库与ElasticSearch进行交互,并且我试图找出一种基于非类型数据构建索引类型/嵌套对象的方法.该类型具有以下基本结构.
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. 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; }
}
}
我想为NestedType的每个实例创建一个嵌套的对象/类型.因此,如果有两个NestedType实例,则将有两个嵌套对象.我可以从DynamicObject继承NestedType,以将字典转换为NEST,然后正确映射的真实"属性(即,将每个字典键转换为属性).问题是我不知道如何设置嵌套对象的名称/类型.
I want to create a nested object/type for each instance of NestedType. So if there are two instances of NestedType, there will then be two nested objects. I can inherit NestedType from DynamicObject to turn the dictionary into "real" properties that NEST then maps correctly (i.e., turn each dictionary key into a property). The problem is that I can't figure out how to set the name/type of the nested object.
我知道有两种方法来映射名称:ElasticType属性和NestedObject流畅接口.这里的问题是,有一个单一类型代表多个嵌套对象类型.我可以进行一些运行时类型构建,但是如果可以避免的话,我宁愿不这样做.
There are two ways to map names that I know of: ElasticType attribute and NestedObject fluent interface. 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.
是否可以使用方法或属性作为嵌套对象的名称/类型?还是有更好的方法(希望通过NEST)将这种类型的数据映射到ElasticSearch?
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)?
谢谢!埃里克
编辑
我更新了实体定义以反映我在做什么(使用DynamicObject获取JsonSerializer来执行我想要的操作).我想要的是不同词典具有不同映射(不同词干,分析器等)的能力.如果有适当的类型,我可以使用NEST fluent语法进行设置,但使用动态时,没有类型可供fluent API使用.最终,我想将流畅的API与基于字符串而不是类型的字符串混合使用.这有道理吗?
I updated the entity definition to reflect what I'm doing (using DynamicObject to get the JsonSerializer to do what I want). 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. Ultimately, I want to mix the fluent API with a string based on strings instead of types. Does this make sense?
推荐答案
如果我正确理解了您的意图,Entity
对象将仅包含嵌套对象,不是吗?
If I understand correctly your intention, Entity
object will have only nested objects in it, won't it?
您可以尝试将Elasticsearch的动态映射功能用于实体对象.我认为Entity是根对象.
You can try to use dynamic mapping functionality of elasticsearch for entity object. I assume Entity is a root object.
curl -X POST localhost:9200/myindex/entity/_mapping
{"dynamic_templates": [
{"nested_data_template": {
"mapping": {
"type": "nested" },
"match_mapping_type": "object",
"path_match": "*" }}]}
path_match: *
和match_mapping_type: object
表示对于所有以object为值的字段名称,将应用嵌套类型映射.
path_match: *
and match_mapping_type: object
mean that for all field names with object as a value nested type mapping will be applied.
使用NEST和Fluent API,您可以使用以下API. IntelliSense将指导您如何构建上面的映射. ;)
Using NEST and Fluent API you can use the following API. IntelliSense will guide you how to build mapping above. ;)
descriptor.DynamicTemplates(DynamicTemplatesDescriptor<Entity>)
每次出现与该模板匹配的新属性时,elasticsearch都会基于动态映射更新映射.一段时间后,您的映射将如下所示:
Every time when a new property matching this template appears, elasticsearch will update mapping based on dynamic mapping. 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"
}
}
}
}
}
}
}
}
希望这会有所帮助!
这篇关于如何在ElasticSearch/NEST中将单个.NET类型映射到多个嵌套对象类型?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!