简体   繁体   English

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

[英]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.

相关问题 如何使用NEST构造来自多个类型的字段且没有魔术字符串的ElasticSearch搜索 - How do I construct an ElasticSearch search using NEST with fields from multiple types without magic strings 如何在Elasticsearch NEST中序列化JToken或JObject类型的属性? - How do I serialize properties of type JToken or JObject in Elasticsearch NEST? 使用 NEST 使用 ElasticSearch 2.x 搜索多种类型时,如何获得混合结果? - How do you get mixed results when searching multiple types with ElasticSearch 2.x using NEST? Elasticsearch NEST 查询嵌套 object - Elasticsearch NEST query nested object 如何使用 NEST 库在嵌套的 object 中搜索 Elasticsearch 中同一字段的多个值? - How to search in nested object for multiple values on same field in Elasticsearch with NEST Library? NEST ElasticSearch C#如何在嵌套对象上进行过滤 - NEST ElasticSearch c# how to Filter on Nested Object ElasticSearch NEST搜索多个类型和所有字段 - ElasticSearch NEST Search Multiple Types & All Fields 跨多种类型的ElasticSearch NEST查询 - ElasticSearch NEST query across multiple types Nest-ElasticSearch.Net-如何分割搜索属性返回的FacetFilter命中计数 - Nest - ElasticSearch.Net - How do I split the FacetFilter hit count returned by a search property 在ElasticSearch上使用NEST - 如何使用部分填充的对象作为搜索条件 - Using NEST on ElasticSearch - How do i use a partially populated object as a search criteria
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM