How to filter docs by text field length?

Using scripted fields is quite the performance killer in this example. The most performant solution to this would be to use an ingest pipeline with a script processor that stores the length of the string in a dedicated field.

The reason for the different values is that some data gets retrieved from the inverted index (where it already might have been shortened when there are long fields) where as the first one uses the JSON source.