Use runtime fields

Use runtime fields

Use the runtime_mappings parameter to extract and create runtime fields, or columns, from existing ones during a search.

The following search creates a release_day_of_week runtime field from release_date and returns it in the response.

  1. POST _sql?format=txt
  2. {
  3. "runtime_mappings": {
  4. "release_day_of_week": {
  5. "type": "keyword",
  6. "script": """
  7. emit(doc['release_date'].value.dayOfWeekEnum.toString())
  8. """
  9. }
  10. },
  11. "query": """
  12. SELECT * FROM library WHERE page_count > 300 AND author = 'Frank Herbert'
  13. """
  14. }

The API returns:

  1. author | name | page_count | release_date |release_day_of_week
  2. ---------------+---------------+---------------+------------------------+-------------------
  3. Frank Herbert |Dune |604 |1965-06-01T00:00:00.000Z|TUESDAY