Restrict dynamic template path_match to 1 level

Hey,

I'm trying to use a common mapping template to set a field to a nested type but to not set fields under it to also nested. The trouble I'm running into is the dynamic mapping template I'm using is setting the field, and all sub fields as nested. (Which makes sense based off how I'm doing it and the documentation)

The template I'm trying to use is:

POST _template/entity_record_template
{
  "index_patterns": [
    "entity_*"
  ],
  "mappings": {
    "_doc": {
      "dynamic_templates": [
        {
          "entity_contents": {
            "path_match": "entityContent.*",
            "mapping": {
              "type": "nested",
              "dynamic": "true"
            }
          }
        }
      ],
      "properties": {
        "entityDestination": {
          "type": "keyword"
        },
        "entityType": {
          "type": "keyword"
        },
        "entityContent": {
          "type": "object"
        }
      }
    }
  }
}

And then adding an object:

POST /entity_a/_doc
{
  "entityDestination": "all",
  "entityType": "a",
  "entityContent": {
    "entityA": [
      {
        "valOne": "A",
        "valTwo": "B"
      },
      {
        "valOne": "B",
        "valTwo": "A"
      }
    ]
  }
}

However it fails because:

{
  "error": {
    "root_cause": [
      {
        "type": "mapper_parsing_exception",
        "reason": "object mapping for [entityContent.entityA.valOne] tried to parse field [valOne] as object, but found a concrete value"
      }
    ],
    "type": "mapper_parsing_exception",
    "reason": "object mapping for [entityContent.entityA.valOne] tried to parse field [valOne] as object, but found a concrete value"
  },
  "status": 400
}

Which I understand makes sense so I could remove the dynamic_templates section however then when I'm trying to query like so:

POST /entity_a/_search
{
  "_source" : false,
  "query": {
    "nested": {
      "path": "entityContent.entityA",
      "query": {
        "bool": {
          "must": [
            { "match": { "entityContent.entityA.valOne": "A" }}
          ]
        }
      },
      "inner_hits" : {}
    }
  }
}

It then fails because the entityA isn't set to be nested, now I could add into the entityContent to implicity set entityA however I was trying to make this generic so if I get entityB then no update to the template would be required.

Is there a way to say for the path_match to be restricted to the first level? So entityContent.entityA is matched but entityContent.entityA.* is not? (The names also don't follow the pattern of entityX so I couldnt do entityContent.entity*)

Any suggestions, alternatives or recommendations would be most welcomed.

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.