This project provides a high-performance RESTful API for indexing and searching multilingual documents using Elasticsearch as the backend. The service is built with Python and FastAPI and is fully containerized using Docker for easy setup and deployment.
The key feature is its ability to handle documents with text in various languages dynamically, without needing to pre-define the languages in the schema.
- Fast & Modern API: Built on FastAPI for high performance and automatic interactive API documentation.
- Powerful Search: Leverages Elasticsearch for complex, multilingual text search and analysis.
- Containerized: Uses Docker and Docker Compose for easy setup, deployment, and scalability.
- Scalable: Designed as a microservice that can be easily integrated into a larger system.
- Backend: Python 3.9
- API Framework: FastAPI
- ASGI Server: Uvicorn
- Database/Search Engine: Elasticsearch
- Containerization: Docker & Docker Compose
Before you begin, ensure you have the following installed on your local machine:
Multilingual-Service/
├── app/
│ ├── es_client.py
│ ├── main.py
│ └── models.py
├── docker-compose.yml
├── Dockerfile
├── requirements.txt
└── README.md
git clone <your-repository-url>
cd Multilingual-ServiceThis will start both Elasticsearch and the FastAPI API.
docker-compose up --build- FastAPI will be available at: http://localhost:4321/docs
- Elasticsearch will be available at: http://localhost:1234
To stop all running services:
docker-compose downNote: You must have a local Elasticsearch instance running on
localhost:9200for this to work.
This endpoint adds a new document to the search index or updates an existing one with the same identifier.
- URL:
/documents - Method: POST
- Body: JSON object with the document's identifier and a body object containing language-to-text mappings.
Example using curl:
curl -X POST "http://localhost:4321/documents" \
-H "Content-Type: application/json" \
-d '{
"identifier": "doc-001",
"body": {
"en": "Hello world! This is a test of the search service.",
"fa": "سلام دنیا! این یک آزمایش برای سرویس جستجو است.",
"es": "Hola mundo! Esta es una prueba del servicio de búsqueda."
}
}'This endpoint searches for documents containing a specific query within the text of a specified language, or finds documents available in a specific language.
- URL:
/search - Method: GET
- Query Parameters:
q(optional): The search term.lang(optional): The 2-letter language code to search within (e.g., en, fa).exists_lang(optional): Find documents available in a specific language.
Examples using curl:
Search for "test" in English documents:
curl --get "http://localhost:4321/search" --data-urlencode "q=test" --data-urlencode "lang=en"Search for "آزمایش" in Persian (Farsi) documents:
curl --get "http://localhost:4321/search" --data-urlencode "q=آزمایش" --data-urlencode "lang=fa"Find all documents that have a Persian translation:
curl --get "http://localhost:4321/search" --data-urlencode "exists_lang=fa"To stop all running services (FastAPI and Elasticsearch):
docker-compose down- Make sure the ports in
docker-compose.ymlmatch those in your documentation and code.