Runtime Fields

Hey, I wanted to know what is the difference between defining runtime_field in mapping v/s and doing so in the query itself.
For this, I tried running the following requests to observe if there occurs any changes in internal calculation by observing the profile section of the output, but seems like I am unlucky. The profile section does not tell anything specific to the calculation of the runtime field and seems like the sample data is not large enough to produce any significant difference in the timings of the search results over the indexes.

POST _reindex
{
  "source": {"index": "kibana_sample_data_ecommerce"},
  "dest": {"index": "sample"}
}
GET /kibana_sample_data_ecommerce/_search
{
  "profile": true,
  "explain": true
}
GET /kibana_sample_data_ecommerce/_search
{
  "_source": false,
  "fields": [
    "*"
  ], 
  "profile": true,
  "explain": true
}
PUT /sample/_mapping
{
  "runtime": {
    "taxful_total_price": {
      "type": "long"
    },
    "taxless_total_price": {
      "type": "long"
    },
    "tax_value": {
      "type": "long",
      "script": {
        "source": """
          emit(doc['taxful_total_price'].value - doc['taxless_total_price'].value);
          """
      }
    }
  }
}
GET /sample/_search
{
  "_source": false, 
  "profile": true, 
  "explain": true
}
GET /sample/_search
{
  "profile": true, 
  "explain": true
}
GET /sample/_search
{
  "fields": [
    "*"
  ], 
  "_source": false, 
  "profile": true, 
  "explain": true
}
GET /kibana_sample_data_ecommerce/_search
{
  "profile": true,
  
  "query": {
    "bool": {
      "filter": [
        {
          "range": {
            "order_date": {
              "gte": "2023-09-04T23:45:36.000Z"
            }
          }  
        }
      ]
    }
  },
  
  "runtime_mappings": {
    "taxful_total_price": {
      "type": "long"
    },
    "taxless_total_price": {
      "type": "long"
    },
    "tax_value": {
      "type": "long",
      "script": {
        "source": """
          emit(doc['taxful_total_price'].value - doc['taxless_total_price'].value);
          """
      }
    }
  },
  
  "fields": [
    "*"
  ],
  
  "_source": false
}

Can anyone tell if there is any difference between the two methods in terms of performance and also the what is the best-practice when creating these run-time fields.

Hi @MOHIT_SHARMA4,

There is a different in terms of when they are executed. There is a good thread from 2021 here that gives a good explanation of the difference. The documentation also gives some benefits of runtime fields here as well.

To determine which one you use I would recommend having a look at the compromises in the documentation for their impact on searching to get an idea of some useful best practices and tradeoffs.

Hope that helps!

Hi @carly.richmond , thanks for your help.
But in the above search request where I have written:

"runtime_mappings": {
    "taxful_total_price": {
      "type": "long"
    },
    "taxless_total_price": {
      "type": "long"
    },
    "tax_value": {
      "type": "long",
      "script": {
        "source": """
          emit(doc['taxful_total_price'].value - doc['taxless_total_price'].value);
          """
      }
    }
  }

The above is not a script field to the best of my knowledge(correct me in case I am wrong). This is just another way to use the run-time field instead of mentioning it in the mapping itself (I can still use it in the query filters unlike script fields).

I was not comparing script fields with run-time fields. But instead, comparing the two methods to use run-time fields, the first one to directly mention them in the mapping, and the second to mention them in the search request. I wanted to know if there is any difference between the two methods in terms of performance.

@MOHIT_SHARMA4, so to confirm your question is not about scripted versus runtime fields but whether there is a performance impact on defining the runtime fields in the mapping compared to the search request?

@carly.richmond, Yes you got it correct.

I'm not aware of a performance impact in terms of their execution as in both cases they are evaluated at query time. I think it's more down to the level of flexibility you need when defining your runtime fields that determines where to specify your runtime fields.

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