How do I map a single .NET type to multiple nested object types in ElasticSearch/NEST?

…衆ロ難τιáo~ 提交于 2019-12-01 02:28:14

If I understand correctly your intention, Entity object will have only nested objects in it, won't it?

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: * and match_mapping_type: object mean that for all field names with object as a value nested type mapping will be applied.

Using NEST and Fluent API you can use the following API. IntelliSense will guide you how to build mapping above. ;)

descriptor.DynamicTemplates(DynamicTemplatesDescriptor<Entity>)

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"
              }
            }
          }
        }
      }
    }
  }
}

Hope this will help!

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!