Eri*_*k T 16 c# elasticsearch nest
我正在使用NEST库与ElasticSearch进行交互,我正试图找出一种基于非类型数据构建索引类型/嵌套对象的方法.该类型具有以下基本结构.
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; }
}
}
Run Code Online (Sandbox Code Playgroud)
我想为NestedType的每个实例创建一个嵌套对象/类型.因此,如果有两个NestedType实例,那么将有两个嵌套对象.我可以从DynamicObject继承NestedType,将字典转换为NEST然后正确映射的"真实"属性(即,将每个字典键转换为属性).问题是我无法弄清楚如何设置嵌套对象的名称/类型.
我知道有两种映射名称的方法:ElasticType属性和NestedObject流畅的接口.这里的问题是有一种类型代表多个嵌套对象类型.我可以做一些运行时类型的构建,但是如果我可以避免它,我宁愿不做.
有没有办法让方法或属性用作嵌套对象的名称/类型?或者有更好的方法将这种类型的数据映射到ElasticSearch(希望通过NEST)?
谢谢!埃里克
编辑
我更新了实体定义以反映我正在做的事情(使用DynamicObject让JsonSerializer做我想做的事).我想要的是不同字典具有不同映射的能力(不同的词干,分析器等).如果有适当的类型,我可以使用NEST流畅的语法来设置它,但是当使用动态时,没有类型可供使用的流畅API.最后,我想将流畅的API与基于字符串而不是类型的字符串混合使用.这有意义吗?
如果我理解你的意图,Entity对象将只有嵌套对象,不是吗?
您可以尝试使用elasticsearch的动态映射功能来实现实体对象.我假设Entity是一个根对象.
curl -X POST localhost:9200/myindex/entity/_mapping
{"dynamic_templates": [
{"nested_data_template": {
"mapping": {
"type": "nested" },
"match_mapping_type": "object",
"path_match": "*" }}]}
Run Code Online (Sandbox Code Playgroud)
path_match: *并且match_mapping_type: object表示对于具有对象作为值的所有字段名称,将应用嵌套类型映射.
使用NEST和Fluent API,您可以使用以下API.IntelliSense将指导您如何构建上面的映射.;)
descriptor.DynamicTemplates(DynamicTemplatesDescriptor<Entity>)
Run Code Online (Sandbox Code Playgroud)
每当出现与此模板匹配的新属性时,elasticsearch将根据动态映射更新映射.一段时间后,您的映射将如下所示:
{
"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"
}
}
}
}
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
希望这会有所帮助!