- ETL Module
The ETL module is responsible for inserting preprocessed and embedded movie .json files into Elasticsearch.
- Database (NoSQL - Elasticsearch)
In load.py, we use the elasticsearch Python client to:
- Connect to the Elasticsearch server (with retry logic)
- Create an index (if it doesn't exist)
- Insert the
.jsondocuments from the data folder - Each document includes:
movie_idgenrestype(short, summary, or long)chunk_idtextvector(768-dim dense vector)
Indexing uses
cosine similarityto support semantic search.
- Docker Setup
The ETL service is containerized and configured to run a run_etl.sh script which:
- Waits for a
.zipfile of JSON chunks to appear in thedata/jsons/folder.
app/
├── etl/
│ └── data/
│ │ └── jsons/
│ │ ├── move_data.zip
│ └── ...
└──
- Unzips and flattens the directory structure.
- Executes
load.pyto push the content into Elasticsearch. - Cleans and archives the
.zipfile after successful upload.
- Dockerfile
FROM python:3.10-slim-bullseye
# Install system dependencies
RUN apt-get update && apt-get install -y --no-install-recommends \
unzip \
build-essential libpq-dev libfreetype6-dev libpng-dev libjpeg-dev \
libblas-dev liblapack-dev gfortran \
&& apt-get clean && rm -rf /var/lib/apt/lists/*
# Set working directory
WORKDIR /etl
# Copy requirements file and install dependencies
COPY requirements.txt .
RUN pip3 install --upgrade pip && pip3 install --no-cache-dir -r requirements.txt
# Copy the rest of the code
COPY . .
# Make the run script executable
RUN chmod +x run_etl.sh
# Run the ETL process
CMD ["sh", "run_etl.sh"]
get_elasticsearch()
Creates and returns an Elasticsearch client instance.
Returns:
| Name | Type | Description |
|---|---|---|
Elasticsearch |
An instance of the Elasticsearch client connected to the URL specified in the environment variable |
Source code in app/etl/config.py
22 23 24 25 26 27 28 29 | |
connect_to_elasticsearch(retries=10, delay=2)
Attempts to connect to Elasticsearch with retry logic.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
retries |
int
|
Number of retry attempts. |
10
|
delay |
int
|
Delay in seconds between attempts. |
2
|
Returns:
| Name | Type | Description |
|---|---|---|
Elasticsearch |
An instance of the Elasticsearch client. |
Raises:
| Type | Description |
|---|---|
ConnectionError
|
If connection fails after all attempts. |
Source code in app/etl/load.py
10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 | |
create_index(es)
Creates an Elasticsearch index with the required mappings if it doesn't exist.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
es |
Elasticsearch
|
Elasticsearch client. |
required |
Source code in app/etl/load.py
36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 | |
upload_documents(es)
Uploads JSON documents from the TO_INSERT_DIR to Elasticsearch.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
es |
Elasticsearch
|
Elasticsearch client. |
required |
Source code in app/etl/load.py
67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 | |