Using Stored Scripts in Elasticsearch Ingestion Pipelines for Base64 and Hex Decoding
The article explains how Elasticsearch stored scripts can be created and invoked within ingestion pipelines to decode Base64 and hexadecimal data, showing reusable Painless scripts, pipeline configuration, and the advantages of modular, maintainable transformations without inline code duplication.
This article demonstrates how to use stored scripts in Elasticsearch to implement non-standard data processing workflows. Stored scripts allow you to save scripts independently in Elasticsearch and invoke them on-demand within ingestion pipelines, enabling code reuse without duplication.
The first part introduces the concept of stored scripts in Elasticsearch, explaining how they provide a modular and manageable approach to data processing pipelines. The second part details the implementation of a Base64 decoding stored script named "decodebase64", which accepts a field name parameter, checks for null values, and decodes the Base64 content to a target field using the Painless scripting language.
The script is created using the PUT /_scripts/decodebase64 API with the following source code:
PUT /_scripts/decodebase64
{
"script": {
"description": "Decode base64",
"lang": "painless",
"source": "def src=ctx[params['field']]; if (src == null) { return; } def target=params['target_field']; ctx[target]=src.decodeBase64();"
}
}Then, an ingestion pipeline is created to use this stored script with configurable parameters for field and target_field, allowing flexible reuse across different use cases. The pipeline is demonstrated with a bulk indexing operation that decodes Base64-encoded fruit names.
The third part shows a similar implementation for hexadecimal decoding using a stored script named "decodehex", which manually converts hex strings to text by iterating through pairs of characters and converting them using Integer.parseInt().
PUT /_scripts/decodehex
{
"script": {
"description": "Decode HEX",
"lang": "painless",
"source": "def src=ctx[params['field']]; if (src == null) { return; } def target=params['target_field']; StringBuilder sb = new StringBuilder(); for (int i = 0; i < src.length(); i += 2) { String byteStr = src.substring(i, i + 2); char byteChar = (char) Integer.parseInt(byteStr, 16); sb.append(byteChar) } ctx[target] = sb.toString();"
}
}The article also demonstrates combining multiple stored scripts in a single pipeline for processing different encoding formats simultaneously. The final section summarizes the benefits of using stored scripts: creating reusable scripts for common data transformation tasks, saving development time while maintaining code consistency, and improving pipeline readability and maintainability by avoiding inline script clutter.
Sohu Tech Products
A knowledge-sharing platform for Sohu's technology products. As a leading Chinese internet brand with media, video, search, and gaming services and over 700 million users, Sohu continuously drives tech innovation and practice. We’ll share practical insights and tech news here.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.