Skip to content

A Dockerized FastAPI service leveraging Elasticsearch for high-performance, dynamic, and multilingual document indexing and searching.

License

Notifications You must be signed in to change notification settings

AsefAfsahi/Multilingual-Service

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

6 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Multilingual Search Service with Elasticsearch and FastAPI

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.


Features

  • 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.

Technology Stack

  • Backend: Python 3.9
  • API Framework: FastAPI
  • ASGI Server: Uvicorn
  • Database/Search Engine: Elasticsearch
  • Containerization: Docker & Docker Compose

Prerequisites

Before you begin, ensure you have the following installed on your local machine:


Project Structure

Multilingual-Service/
├── app/
│   ├── es_client.py
│   ├── main.py
│   └── models.py
├── docker-compose.yml
├── Dockerfile
├── requirements.txt
└── README.md

Setup and Installation

1. Clone the Repository

git clone <your-repository-url>
cd Multilingual-Service

2. Run with Docker Compose (Recommended)

This will start both Elasticsearch and the FastAPI API.

docker-compose up --build

To stop all running services:

docker-compose down

3. Run Locally (Without Docker)

Note: You must have a local Elasticsearch instance running on localhost:9200 for this to work.

API Endpoints

1. Index a Document

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."
           }
         }'

2. Search for Documents

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"

Stopping the Services

To stop all running services (FastAPI and Elasticsearch):

docker-compose down

Notes

  • Make sure the ports in docker-compose.yml match those in your documentation and code.

About

A Dockerized FastAPI service leveraging Elasticsearch for high-performance, dynamic, and multilingual document indexing and searching.

Topics

Resources

License

Stars

Watchers

Forks