Run time field generates error when trying tutorial

I'm trying to learn how to create run time fields by following the instructions on this page:

I tried my own variation with these queries:

PUT rfield

POST rfield/_doc
{
  "Favourite Food": "My fave food is"
}

GET rfield/_search
{
  "runtime_mappings": {
    "new_field": {
      "type": "keyword",
      "script": {
        "source": "emit(doc['Favourite Food'].value + ' Pizza')"
      }
    }
  },
    "aggs":{
    "new_field": {
      "terms": {
      "field": "new_field"
      }
    }
  }
}

But my search query causes this error:

{
  "error": {
    "root_cause": [
      {
        "type": "script_exception",
        "reason": "runtime error",
        "script_stack": [
          "org.elasticsearch.server@8.7.0/org.elasticsearch.index.mapper.TextFieldMapper$TextFieldType.fielddataBuilder(TextFieldMapper.java:969)",
          "org.elasticsearch.server@8.7.0/org.elasticsearch.index.fielddata.IndexFieldDataService.getForField(IndexFieldDataService.java:102)",
          "org.elasticsearch.server@8.7.0/org.elasticsearch.index.query.SearchExecutionContext.lambda$setSourceProvider$3(SearchExecutionContext.java:513)",
          "org.elasticsearch.server@8.7.0/org.elasticsearch.search.lookup.SearchLookup.getForField(SearchLookup.java:115)",
          "org.elasticsearch.server@8.7.0/org.elasticsearch.search.lookup.LeafDocLookup$2.run(LeafDocLookup.java:153)",
          "org.elasticsearch.server@8.7.0/org.elasticsearch.search.lookup.LeafDocLookup$2.run(LeafDocLookup.java:132)",
          "java.base/java.security.AccessController.doPrivileged(AccessController.java:318)",
          "org.elasticsearch.server@8.7.0/org.elasticsearch.search.lookup.LeafDocLookup.getFactoryForDoc(LeafDocLookup.java:132)",
          "org.elasticsearch.server@8.7.0/org.elasticsearch.search.lookup.LeafDocLookup.get(LeafDocLookup.java:170)",
          "org.elasticsearch.server@8.7.0/org.elasticsearch.search.lookup.LeafDocLookup.get(LeafDocLookup.java:32)",
          "emit(doc['Favourite Food'].value + ' Pizza')",
          "         ^---- HERE"
        ],
        "script": "emit(doc['Favourite Food'].value + ' Pizza')",
        "lang": "painless",
        "position": {
          "offset": 9,
          "start": 0,
          "end": 44
        }
      }
    ],
    "type": "search_phase_execution_exception",
    "reason": "all shards failed",
    "phase": "query",
    "grouped": true,
    "failed_shards": [
      {
        "shard": 0,
        "index": "rfield",
        "node": "aIxuZp6uT8SfhHqfRZTJFg",
        "reason": {
          "type": "script_exception",
          "reason": "runtime error",
          "script_stack": [
            "org.elasticsearch.server@8.7.0/org.elasticsearch.index.mapper.TextFieldMapper$TextFieldType.fielddataBuilder(TextFieldMapper.java:969)",
            "org.elasticsearch.server@8.7.0/org.elasticsearch.index.fielddata.IndexFieldDataService.getForField(IndexFieldDataService.java:102)",
            "org.elasticsearch.server@8.7.0/org.elasticsearch.index.query.SearchExecutionContext.lambda$setSourceProvider$3(SearchExecutionContext.java:513)",
            "org.elasticsearch.server@8.7.0/org.elasticsearch.search.lookup.SearchLookup.getForField(SearchLookup.java:115)",
            "org.elasticsearch.server@8.7.0/org.elasticsearch.search.lookup.LeafDocLookup$2.run(LeafDocLookup.java:153)",
            "org.elasticsearch.server@8.7.0/org.elasticsearch.search.lookup.LeafDocLookup$2.run(LeafDocLookup.java:132)",
            "java.base/java.security.AccessController.doPrivileged(AccessController.java:318)",
            "org.elasticsearch.server@8.7.0/org.elasticsearch.search.lookup.LeafDocLookup.getFactoryForDoc(LeafDocLookup.java:132)",
            "org.elasticsearch.server@8.7.0/org.elasticsearch.search.lookup.LeafDocLookup.get(LeafDocLookup.java:170)",
            "org.elasticsearch.server@8.7.0/org.elasticsearch.search.lookup.LeafDocLookup.get(LeafDocLookup.java:32)",
            "emit(doc['Favourite Food'].value + ' Pizza')",
            "         ^---- HERE"
          ],
          "script": "emit(doc['Favourite Food'].value + ' Pizza')",
          "lang": "painless",
          "position": {
            "offset": 9,
            "start": 0,
            "end": 44
          },
          "caused_by": {
            "type": "illegal_argument_exception",
            "reason": "Fielddata is disabled on [Favourite Food] in [rfield]. 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 [Favourite Food] in order to load field data by uninverting the inverted index. Note that this can use significant memory."
          }
        }
      }
    ]
  },
  "status": 400
}

What did I do wrong?

1 Like

I solved the problem making sure I define a mapping first before I post the first record:

PUT rfield/_mapping
{
  "properties": {
    "Favourite Food": {"type":"keyword"}
  }
}
2 Likes

Yep, I was just about to write that but one other thing it's not a great idea or best practice to have spaces in your field names underscores are much better

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