0
0
Elasticsearchquery~10 mins

Runtime fields in Elasticsearch - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to define a runtime field that calculates the length of the "message" field.

Elasticsearch
{
  "runtime_mappings": {
    "message_length": {
      "type": "long",
      "script": {
        "source": "emit(doc['message'].value.[1])"
      }
    }
  }
}
Drag options to blanks, or click blank then click option'
Acount()
Bsize()
Clength()
Dlen()
Attempts:
3 left
💡 Hint
Common Mistakes
Using Python's len() function which is not valid in painless scripts.
Using size() which is for collections, not strings.
2fill in blank
medium

Complete the code to create a runtime field that extracts the first 3 characters of the "user" field.

Elasticsearch
{
  "runtime_mappings": {
    "user_prefix": {
      "type": "keyword",
      "script": {
        "source": "emit(doc['user'].value.[1](0, 3))"
      }
    }
  }
}
Drag options to blanks, or click blank then click option'
Asubstr
Bsubstring
Cslice
Dcut
Attempts:
3 left
💡 Hint
Common Mistakes
Using substr which is not supported in painless.
Using slice which is a JavaScript method, not painless.
3fill in blank
hard

Fix the error in the runtime field script to correctly convert the "age" field to a string.

Elasticsearch
{
  "runtime_mappings": {
    "age_str": {
      "type": "keyword",
      "script": {
        "source": "emit(doc['age'].value.[1]())"
      }
    }
  }
}
Drag options to blanks, or click blank then click option'
AtoString
Bstr
Cstringify
Dto_str
Attempts:
3 left
💡 Hint
Common Mistakes
Using Python's str() function which is invalid here.
Using stringify which is a JavaScript function, not painless.
4fill in blank
hard

Fill both blanks to create a runtime field that checks if the "status" field equals "active" and emits 1 if true, else 0.

Elasticsearch
{
  "runtime_mappings": {
    "is_active": {
      "type": "long",
      "script": {
        "source": "if (doc['status'].value [1] '[2]') { emit(1) } else { emit(0) }"
      }
    }
  }
}
Drag options to blanks, or click blank then click option'
A==
B!=
Cactive
Dinactive
Attempts:
3 left
💡 Hint
Common Mistakes
Using != which checks inequality.
Using the wrong string like 'inactive'.
5fill in blank
hard

Fill all three blanks to create a runtime field that emits the uppercase version of the "city" field only if its length is greater than 5.

Elasticsearch
{
  "runtime_mappings": {
    "city_upper": {
      "type": "keyword",
      "script": {
        "source": "if (doc['city'].value.[1]() [2] 5) { emit(doc['city'].value.[3]()) }"
      }
    }
  }
}
Drag options to blanks, or click blank then click option'
Alength
B>
CtoUpperCase
Dsize
Attempts:
3 left
💡 Hint
Common Mistakes
Using size() instead of length() for string length.
Using < instead of > for comparison.
Using toUpper instead of toUpperCase.