如何在ElasticSearch / NEST中将单个.NET类型映射到多个嵌套对象类型?

时间:2015-04-17 16:56:06

标签: 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; } 
        }
}

我想为NestedType的每个实例创建一个嵌套对象/类型。因此,如果有两个NestedType实例,那么将有两个嵌套对象。我可以从DynamicObject继承NestedType,将字典转换成NEST然后正确映射的“真实”属性(即,将每个字典键转换为属性)。问题是我无法弄清楚如何设置嵌套对象的名称/类型。

我知道有两种映射名称的方法:ElasticType属性和NestedObject流畅接口。这里的问题是有一种类型代表多个嵌套对象类型。我可以做一些运行时类型构建,但是如果我能避免它,我宁愿不做。

有没有办法让方法或属性用作嵌套对象的名称/类型?或者有没有更好的方法将这种类型的数据映射到ElasticSearch(希望通过NEST)?

谢谢! 埃里克

修改

我更新了实体定义以反映我正在做的事情(使用DynamicObject让JsonSerializer做我想做的事)。我想要的是不同字典具有不同映射的能力(不同的词干,分析器等)。如果有合适的类型,我可以使用NEST流畅的语法来设置它,但是当使用动态时,没有类型可供使用的流畅API。最后,我想将流畅的API与基于字符串而不是类型的字符串混合使用。这有意义吗?

1 个答案:

答案 0 :(得分:6)

如果我理解你的意图,Entity对象将只包含嵌套对象,赢了吗?

您可以尝试对实体对象使用elasticsearch的{​​{3}}功能。我假设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: *match_mapping_type: object表示对于所有使用object作为值的字段名称,将应用嵌套类型映射。

使用NEST和Fluent API,您可以使用以下API。 IntelliSense将指导您如何构建上面的映射。 ;)

descriptor.DynamicTemplates(DynamicTemplatesDescriptor<Entity>)

每当出现与此模板匹配的新属性时,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"
              }
            }
          }
        }
      }
    }
  }
}

希望这会有所帮助!