Elasticsearch SQL & Watsonx: A Comprehensive End-to-End Guide
Walks through setting up Elasticsearch SQL with Watsonx to translate natural language questions into Elasticsearch SQL queries and execute them.
What this file does
Walks through setting up Elasticsearch SQL with Watsonx to translate natural language questions into Elasticsearch SQL queries and execute them.
When to use it
- You want non-technical users to query Elasticsearch with plain English
- You need to combine Elasticsearch SQL with an LLM for automated query generation
- You are building a natural language interface over Elasticsearch data
Assumes this stack
Elasticsearch SQL & Watsonx: A Comprehensive End-to-End Guide
Ever wondered how to tap into the power of Elasticsearch without wrestling with its entire Query DSL? Or how to let non-technical teammates ask questions like, “Which employees left this year?” and automatically run the right Elasticsearch SQL? In this comprehensive guide, you’ll learn exactly how to do both—combining the simplicity of SQL and the intelligence of Watsonx to handle everything from basic lookups to advanced text searches.
Prerequisites
clone the repo
git clone https://github.com/IBM/NL2ESQL-with-watsonx.git
Elastic Search Setup
Use Watsonx Discovery
- follow this guide to setup watsonx discovery to get the DB and kibana.
- we need following credentials in our
.env
ELASTIC_URL=""
ELASTIC_USERNAME=""
ELASTIC_PASSWORD=""
Connecting to watsonx.ai for Natural Language to Elastic SQL Generation
-
Connect with watsonx.ai with following the steps here
-
Learn more about using watsonx.ai about here
-
we need following credentials in our
.env
IBM_CLOUD_API_KEY=""
WATSONX_ENDPOINT=""
WATSONX_PROJECT_ID=""
Download Data
For this walkthrough, we use the Kaggle Employee Dataset.
Setup python envinorment
-
Create a virtual environment:
python3.11 -m venv venv source venv/bin/activate # On Windows: venv\Scripts\activate -
Install dependencies:
pip install -r requirements.txt
Before we begine let's Learn about Elasticsearch SQL
Introduction to Elasticsearch SQL
What is Elasticsearch SQL?
Elasticsearch SQL provides an SQL-based interface to query Elasticsearch data. It allows querying Elasticsearch indices as if they were traditional database tables, enabling users familiar with SQL to leverage Elasticsearch without needing to master its native Query DSL syntax (Elasticsearch SQL Documentation).
Key Capabilities:
- SQL Compatibility: Supports standard SQL operations like
SELECT,WHERE,GROUP BY, and aggregation functions. - Indexing and Metadata: Elasticsearch indexes serve as tables, while documents act as rows. Metadata dictionaries (
SHOW TABLES,DESCRIBE) allow schema exploration (Metadata Commands). - Date Handling: Date fields support various functions (
YEAR(),MONTH(),DATE_TRUNC(), etc.), providing flexible date manipulation (Date Functions). - Full-text Search: Special functions like
MATCH()enable powerful text search, integrating Elasticsearch's full-text capabilities within SQL queries (Match Queries).
Real-world Use Cases:
- Search Applications: Quickly implement search functionality using SQL syntax, allowing fast and intuitive development.
- Analytics and Reporting: Easily aggregate, analyze, and visualize large datasets by integrating Elasticsearch SQL with Business Intelligence (BI) tools (Analytics with Elasticsearch SQL).
ES Vs Traditional SQL
Traditional SQL queries are designed primarily for structured, relational data and rely on fixed schemas and exact-match logic (or basic pattern matching with LIKE). In contrast, Elasticsearch queries harness advanced text analysis, tokenization, and relevance scoring, making them far more flexible and efficient when handling unstructured or semi-structured data. With features like fuzzy matching, proximity and span queries, and dynamic query templating, Elasticsearch provides a richer, more nuanced search experience—enabling rapid, distributed searches across massive datasets that traditional SQL simply cannot match. This combination of power, flexibility, and scalability makes Elasticsearch queries significantly better suited for modern search and analytics use cases.
Why ElasticSearch SQL and not ElasticSearch Query DSL
We are relying on LLMs to generate the queries from Natural Language, SQL syntax is readily available in the training dataset of these models, we can expect higher accuracy for a wide range of queries.
Steps
Step 1. Indexing Employee Data in Elasticsearch
In the Indexing code
- Load environment variables (e.g., Elasticsearch credentials).
- Create an Elasticsearch client.
- Read the employee_data.csv, format date columns, and convert them to ISO 8601.
- Save the formatted data to both CSV and JSON.
- Define an index mapping for our
employee_dataindex in Elasticsearch. - Create the index if it doesn’t already exist.
- Bulk-load the data from the JSON file into Elasticsearch.
Step 2: Watsonx Wrapper Class for LLM Inference
We have added Python class wrapper (WatsonxWrapper) that handles Watsonx LLM calls, loading environment variables for the credentials, and abstracting away the text generation and streaming logic. It includes synchronous inference generate_text and streaming inference generate_text_stream.
watsonx wrapper class.
This class will be our main interface to query the LLM for natural language to ES SQL conversion, as well as for automated metadata generation.
Step 3. Automating Metadata Creation for Elasticsearch Fields
To make the our Natural Language to elastic SQL, we need create a metadata dictionary of out index, so that the LLM can generate elastic SQL. We’ll use Watsonx to generate natural language descriptions of each field, along with sample values, data types, etc. This is helpful for:
- Documenting our data automatically.
- Building data catalogs or self-service analytics tools. Creating Metdata Dictionary
Step 4. Natural Language to Elasticsearch SQL with Watsonx
After indexing, we can now harness Watsonx to interpret natural language queries and convert them into ES SQL. We then execute the generated SQL queries against Elasticsearch, returning the results in a tabular form (using pandas DataFrames).
code: nl2eql
- Load environment variables and set up the
Elasticsearchclient. - Import the WatsonxWrapper, custom prompts, and parameters.
- Define a list of questions.
- Generate ES SQL using the Watsonx model.
- Execute the SQL and display the results.
Checkout the prompts used here
Step 5. run the demo
We have created a simple streamlit app where you can test the results
streamlit run streamlit_app.py
Yohooo!!!!, Celebrate your journey upto this point.
Summary
So, to summarise, following this guide you will be able to do Advance Retreival Augmeneted Generated with the power of watsonx and Elastic SQL.
To Dive deeper more into elastic search sql features, check out elasticsearch_queries
What's inside
5 steps with code examples, prerequisites, and a summary; includes 3 Python scripts and a Streamlit app
Change this for your project
- Replace
IBM/NL2ESQL-with-watsonxwith your own repository URL - Replace
employee_data.csvwith your own dataset filename - Replace
employee_dataindex name with your own Elasticsearch index
Where it goes
A standard operating procedure. Keep where the team or agent running the process will find it.
Worth borrowing
- Using an LLM to generate metadata descriptions for Elasticsearch fields
- Separating prompts into a dedicated file for maintainability
Related Documents
Comprehensive AI Assistant Tools Reference
Lists 80+ tools with MCP server associations, bulk support, parallel capability, resource impact, and execution type for AI agent workflows.
iOS Deployment Guide
Walks through setting up an iOS development environment, building a Tauri app for iOS, and publishing to the App Store or alternative channels.
How to Add Resources to Your FastMCP Server
Teaches how to add static and dynamic MCP resources to a FastMCP server, with six ready-to-copy examples for a GitHub crawler.
PCI Compliance Guide
Explains PCI DSS compliance via tokenization, lists forbidden and allowed data, and provides code examples for BigCommerce and Stripe.