Runtime Fields & Tile serve

Hello guys, I wanted to have a confirmation before going in with Elasticsearch.

I would like to display roads on a Map (Mapbox). Let's say these roads have a maxSpeed, length and a width properties. I would like to compute on the fly a "dangerScore" based on these values (static) AND some input weight (Dynamic) on each of them. The dangerScore then would be used to apply a color gradiant on roads.

These are my questions :

  • Should I use the tile serve API and in the Body use something like script_field ?
  • Would it be slow ?
  • Any Idea on How much it could cost me at least ?
  • Are features in the basic plan ?

Thanks in advance guys !

Hello @YB_Coding,

welcome to our community., please find below the answer to your questions:

  • Should I use the tile serve API and in the Body use something like script_field ?

Vector tiles search API does not support scripted fields, you should use runtime fields instead.

  • Would it be slow ?

I expect that the overhead of adding a computed field to be minimal, so it should be pretty fast. Two tips to get best performance:

  1. Consider overriding the default sorting: The documents of the hits layer are sorted by the length of the bounding box diagonal of the geometries to make sure the biggest shapes are included in the final tile. This is done using a painless script so it not the most efficient way for sorting. For example if your documents contain the length of the road, you might better use that for sorting as it should be more efficient.
  2. Prefer WKT over Geojson: This is an area we are working to to improve but at the moment is better to encode your geometries using WKT instead of Geojson. The reason is that in order to read the geometries from source, we build an object model of the document in memory. This means for GeoJson we will be creating many small objects which is not GC friendly. On the other hand WKT will only create one (probably big) string object which is (in most cases) more GC friendly.
  • Any Idea on How much it could cost me at least ?

I don't know sorry.

  • Are features in the basic plan ?

Assuming that you will be storing your roads on a geo_shape field, as far as you don't generate the aggregation layer (grid_precision = 0), then all the features are in basic plan.

I expect you final vector tile search query to look something like:

POST {road_index}/_mvt/{road_field}/{z}/{x}/{y}?grid_precision=0
{
  "runtime_mappings": {
    "danger_score": {
      "type": "double",
      "script": {
        "source": "{YOUR SCRIPT}"
      }
    }
  },
  "fields": [
    "danger_score"
  ],
  "sort": [
    {
      "length": {
        "order": "desc"
      }
    }
  ]
}

Hope this helps

1 Like

Thanks for the detailed answer. It helped me a lot. I'm doing some test locally for now :grin:

1 Like

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