A RESTful API service for product search using Elasticsearch. This service provides powerful search capabilities with features like fuzzy matching, pagination, and sorting.
- Product search with fuzzy matching
- Pagination support
- Sorting by relevance and product name
- Elasticsearch integration
- Swagger documentation
- Clean architecture implementation
Before running this project, make sure you have the following installed:
- Go 1.23 or later (Download here)
- Elasticsearch 8.16.0 (Download here)
swagcommand line tool for Swagger generation (Official Documentation)- Make (for running Makefile commands)
- Install the required Go package for Swagger:
go install github.com/swaggo/swag/cmd/swag@latest- Clone the repository:
git clone https://github.com/paulmhrdka/product-service.git
cd product-service- Install dependencies:
go mod download-
Start Elasticsearch locally on port 9200
-
Create .env file in project root:
SERVER_PORT=8080
ES_URLS=http://localhost:9200The project includes several make commands for common operations:
- Generate Swagger documentation:
make swagger- Run the application:
make run- Run data migration (to import products from Excel):
make migrate- Build the application:
make buildGET /api/v1/products/search
Query Parameters:
q(optional): Search query stringpage(optional): Page number (default: 1)size(optional): Items per page (default: 10, max: 100)
Example Request:
curl "http://localhost:8080/api/v1/products/search?q=paracetamol&page=1&size=10"Example Response:
{
"success": true,
"message": "Products retrieved successfully",
"data": [
{
"id": "1",
"product_name": "Paracetamol 500mg",
"drug_generic": "Paracetamol",
"company": "Company Name",
"score": 0.9
}
],
}.
├── api/
│ └── swagger/ # Swagger documentation
├── cmd/
│ ├── api/ # Application entrypoint
│ └── migration/ # Data migration tool
├── config/ # Configuration
├── internal/
│ ├── domain/ # Domain models
│ ├── delivery/ # HTTP handlers and routing
│ ├── repository/ # Data access layer
│ └── usecase/ # Business logic
├── pkg/ # Shared packages
├── Makefile
└── README.md
To import product data from Excel:
-
Prepare your Excel file with the following columns:
- id
- product_name
- drug_generic
- company
-
Place the Excel file in the
datadirectory asproducts.xlsx -
Run the migration:
make migrateAfter starting the application, you can access the Swagger documentation at:
http://localhost:8080/swagger/index.html
To add new features or modify existing ones:
- Create or modify handlers in
internal/delivery/handler/ - Update business logic in
internal/usecase/ - Add new repository methods in
internal/repository/if needed - Update Swagger annotations and regenerate documentation
- Add tests for new functionality
The API uses standardized error responses:
- 200: Successful operation
- 400: Bad request
- 422: Validation error
- 500: Internal server error
Example error response:
{
"success": false,
"message": "Validation failed",
"error": [
{
"field": "size",
"message": "page size cannot exceed 100"
}
],
}