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

**URL:** https://discuss.elastic.co/t/cannot-use-keyword-multifield-that-is-child-of-text-field-in-script/347416
**Category:** Elasticsearch
**Tags:** runtime-fields
**Created:** [November 17, 2023, 3:34pm UTC](https://discuss.elastic.co/t/cannot-use-keyword-multifield-that-is-child-of-text-field-in-script/347416 "2023-11-17T15:34:50Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![Ian\_Simpson](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/ian_simpson/32/105655_2.png) [@Ian\_Simpson](https://discuss.elastic.co/u/Ian_Simpson)
#### Post date: [November 17, 2023, 3:34pm UTC](https://discuss.elastic.co/t/cannot-use-keyword-multifield-that-is-child-of-text-field-in-script/347416/1 "2023-11-17T15:34:50Z")

</div>

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](https://github.com/elastic/elasticsearch/issues/68819#issuecomment-895161263).

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

```auto
"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:

```auto
{
  "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:

```auto
"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.

---

<div class="post-metadata">

### Author: ![Ian\_Simpson](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/ian_simpson/32/105655_2.png) [@Ian\_Simpson](https://discuss.elastic.co/u/Ian_Simpson)
#### Post date: [November 17, 2023, 6:52pm UTC](https://discuss.elastic.co/t/cannot-use-keyword-multifield-that-is-child-of-text-field-in-script/347416/2 "2023-11-17T18:52:13Z")

</div>

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

```auto
        "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
        },

```

---

<div class="post-metadata">

### Author: ![system](https://us1.discourse-cdn.com/elastic/original/3X/1/a/1ac57faf039f6b580b3f104ef42a2a89e41014de.png) [@system](https://discuss.elastic.co/u/system)
#### Post date: [December 15, 2023, 6:52pm UTC](https://discuss.elastic.co/t/cannot-use-keyword-multifield-that-is-child-of-text-field-in-script/347416/3 "2023-12-15T18:52:32Z")

</div>

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