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

后端 未结 1 892
太阳男子
太阳男子 2021-01-08 01:15

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

相关标签:
1条回答
  • 2021-01-08 01:25

    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!

    0 讨论(0)
提交回复
热议问题