Dynamic templates - Any way of accessing a field full path instead of its name?

Hi,

I am developping a search engine in which I rely quite heavily on dynamic templates, as the underlying data model is dynamic. So I'm using path_match with regular expressions.

Here's a sample example to get an idea:

{
  "mappings": {
    "doc": {
      "dynamic": true,
      "dynamic_templates": [
        {
          "fulltext_search_field_A": {
            "match_pattern": "regex",
            "path_match": "text.field_A\\.value",
            "mapping": {
              "type": "keyword",
              "ignore_above": "32766",
              "copy_to": "@fulltext.text.field_A",
              "fields": { ... }
            }
          }
        },
        ...
        {
          "fulltext_search_field_Z": {
            "match_pattern": "regex",
            "path_match": "text.field_Z\\.value",
            "mapping": {
              "type": "keyword",
              "ignore_above": "32766",
              "copy_to": "@fulltext.text.field_Z",
              "fields": { ... }
            }
          }
        }
      ]
    }
  }
}

I need to copy to distinct fields, so in my query I can choose a subset of fields to match, and also I can get a per-field highlighting. The copy_to target fields are handled by a separate template.

This method works but has issues: the number of fields can be high and this leads to a much bigger cluster state, which can lead to some nasty issues on a shared cluster. For this reason, and for the sake of clarity, I'd like to keep the definitions as succinct as possible.

Moreover, the mapping and fields definitions being the same for all fields, I'd very much like to factorize these into a single dynamic template, so I tried the following:

{
  "mappings": {
    "doc": {
      "dynamic": true,
      "dynamic_templates": [
        {
          "fulltext_search_fields": {
            "match_pattern": "regex",
            "path_match": "text.field_(?:A|B|...|Z).value",
              "mapping": {
              "type": "keyword",
              "ignore_above": "32766",
              "copy_to": "@fulltext.text.{name}",
              "fields": { ... }
            }
          }
        }
      ]
    }
  }
}

Unfortunatey that doesn't work as expected as all my fields get copied over to a single @fulltext.text.value field! What I'd need here is the field's full path instead of just its name!

The reference doc does not mention it, but Is there a means to achieve this? It would be so useful!

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