Cannot Use Keyword Multifield that is Child of Text Field in Script

I am trying to accomplish case-insensitive aggregation filters on fields with multiple values as via scripting as shown here: Support case_insensitive for 'Include' of Terms Aggregation · Issue #68819 · elastic/elasticsearch · GitHub.

The way I have set up my mappings is such that each text field has multi-fields for sorting and aggregating. For instance:

"Contacts": {
    "type": "text",
    "fields": {
        "agg": {
            "type": "keyword",
            "ignore_above": 256,
            "normalizer": "trimNormalizer"
        },
        "sort": {
            "type": "keyword",
            "ignore_above": 256,
            "normalizer": "sortNormalizer"
        }
    },
    "analyzer": "searchAnalyzer"
}

If I try to use a script to create a case-insensitive aggregation filter on the field Contacts.agg I receive an error, which I believe is a bug. This is my query:

{
  "track_total_hits": true,
  "size": 0,
  "runtime_mappings": {
    "insensitive_matches": {
      "type": "keyword",
      "script": {
        "source": "for (String contact : doc.Contacts.agg) { if (contact.toLowerCase(Locale.ROOT).contains('amelia')) { emit(contact); } }"
      }
    }
  },
  "aggs": {
    "fieldAgg": {
      "terms": {
        "field": "insensitive_matches",
        "size": 200
      }
    }
  }
}

And this is the error I receive:

"for (String contact : doc.Contacts.agg) { ",
"                         ^---- HERE"

"Fielddata is disabled on [Contacts] in [orgs]. Text fields are not optimised for operations that require per-document field data like aggregations and sorting, so these operations are disabled by default. Please use a keyword field instead. Alternatively, set fielddata=true on [Contacts] in order to load field data by uninverting the inverted index. Note that this can use significant memory."

This is with version 8.11.0.

Of course this does work if you don't use multi-fields and change the mapping to use "copy_to":

        "Contacts": {
            "type": "text",
            "analyzer": "searchAnalyzer",
            "copy_to":[
                "Contacts_Sort",
                "Contacts_Agg"
            ]
        },
        "Contacts_Sort": {
            "type": "keyword",
            "normalizer": "sortNormalizer",
            "ignore_above": 256
        },
        "Contacts_Agg": {
            "type": "keyword",
            "normalizer": "trimNormalizer",
            "ignore_above": 256
        },

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