Painless: A New Scripting Language
A little over a year ago, I received a phone call one evening about an opportunity to come build an embedded programming language into Elasticsearch. Initially, I thought the idea was a crazy one. Why build a brand new language when there are already so many to choose from? But, as it turns out, it is very difficult to properly secure a language that can be executed remotely without that feature being thought of during the initial design phase. So, with that in mind, I’m pleased to introduce Painless, a new scripting language in Elasticsearch 5.0, designed from the ground up to be both secure and performant.
Painless is a dynamic scripting language with syntax similar to Groovy. Painless scripts are composed of optionally some number of static methods and a required single main section of code. Some highlights of Painless include the following:
- Both static and dynamic types
- List, map, and array initializers
- Shortcuts related to maps and lists
- Built-in regular expressions
- Lambda expressions
- Performance similar to Java
- Method and field level whitelisting providing a much more stringent execution model at runtime (to Painless, methods such as <class>.forName do not exist)
- Scoring scripts as simple as '_score + Math.log(doc.popularity)'
Painless can be used anywhere in Elasticsearch where scripts can normally be run by specifying the 'lang' parameter as 'painless'. Painless will also become the default language in Elasticsearch 5.0. Full documentation can be found here.
On a final note, I would like to thank both Robert Muir and Uwe Schindler for their significant contributions related the dynamic side of Painless, and also to Nik Everett for his significant contribution of regular expressions. Uwe even had several methods related to array length added directly to Java to eek out the best dynamic performance possible.
Jack Conradson is currently a developer at Elasticsearch where he continues to work on improvements to scripting. Prior to Elasticsearch, Jack worked at A9.com, where he developed the Expressions language and simple query parser for Apache Lucene.