A distributed search engine implementation using MapReduce for building inverted indexes and Flask for serving search requests. The system consists of three main components: a MapReduce pipeline for index construction, an index server for query processing, and a search server for the web interface.
The search engine follows a distributed architecture with the following components:
- Purpose: Builds inverted indexes from document collections using a multi-stage MapReduce pipeline
- Input: CSV files containing documents with columns: docid, title, body
- Output: Inverted index files containing term-to-document mappings with TF-IDF scores
- Pipeline Stages:
- Stage 0: Document counting
- Stage 1: Text cleaning and stopword removal
- Stage 2: Term frequency calculation
- Stage 3: Document frequency calculation
- Stage 4: TF-IDF computation
- Stage 5: Index formatting
- Stage 6: Final index generation
- Purpose: Provides REST API endpoints for query processing
- Ports: Runs on ports 9000, 9001, and 9002 (distributed across 3 instances)
- API Endpoint:
/api/v1/hits/ - Features:
- TF-IDF based document ranking
- PageRank integration with configurable weights
- Stopword filtering
- Cosine similarity calculation
- Purpose: Web interface for search queries
- Port: 8000
- Features:
- Concurrent requests to all index server instances
- Result aggregation and ranking
- SQLite database integration for document metadata
- Web interface with adjustable PageRank weights
-
Clone the repository:
git clone <repository-url> cd search-engine
-
Install dependencies:
pip install -r requirements.txt
-
Set up the database:
# Ensure the database file exists at var/search.sqlite3 mkdir -p var # Copy your SQLite database to var/search.sqlite3
-
Prepare input data:
- Place CSV files in
inverted_index/input/ - CSV format:
docid,title,body
- Place CSV files in
-
Run the MapReduce pipeline:
cd inverted_index ./pipeline.sh input -
Copy generated indexes:
# Copy the generated index files to the index server cp output6/part-* ../index_server/index/inverted_index/
-
Start the Index Server:
./bin/index start
This starts 3 Flask instances on ports 9000, 9001, and 9002.
-
Start the Search Server:
./bin/searchdb start
This starts the web interface on port 8000.
-
Access the search interface: Open your browser and navigate to
http://localhost:8000
-
Check status:
./bin/index status # Check index server status ./bin/searchdb status # Check search server status
-
Stop services:
./bin/index stop # Stop index server ./bin/searchdb stop # Stop search server
-
Restart services:
./bin/index restart # Restart index server ./bin/searchdb restart # Restart search server
Endpoint: GET /api/v1/hits/
Parameters:
q: Search query (required)w: PageRank weight (optional, default: 0.5)
Example:
curl "http://localhost:9000/api/v1/hits/?q=hello+world&w=0.5"Response:
{
"hits": [
{
"docid": 12345,
"score": 0.1234
}
]
}The search interface provides:
- Text input for search queries
- Slider for adjusting PageRank weight (0.0 to 1.0)
- Display of search results with titles, summaries, and URLs
- Index files: Located in
index_server/index/inverted_index/ - Stopwords:
index_server/index/stopwords.txt - PageRank scores:
index_server/index/pagerank.out
- Database:
var/search.sqlite3 - Index server URLs: Configured in
search_server/search/config.py
search-engine/
├── bin/ # Service management scripts
├── inverted_index/ # MapReduce pipeline
│ ├── input/ # Input CSV files
│ ├── map*.py # Mapper scripts
│ ├── reduce*.py # Reducer scripts
│ └── pipeline.sh # Pipeline execution script
├── index_server/ # Index server implementation
│ └── index/
│ ├── api/ # REST API endpoints
│ └── inverted_index/ # Generated index files
├── search_server/ # Web interface
│ └── search/
│ ├── templates/ # HTML templates
│ ├── static/ # CSS and static files
│ └── views/ # Web view handlers
├── tests/ # Test suite
└── var/ # Runtime data (logs, database)
Run the test suite:
pytest tests/Key test categories:
- Pipeline tests: Verify MapReduce pipeline correctness
- Index server tests: Test REST API functionality
- Search server tests: Test web interface and concurrency
- Integration tests: End-to-end functionality
- Python 3.x
- Flask: Web framework for servers
- Madoop: MapReduce implementation
- SQLite3: Database for document metadata
- BeautifulSoup4: HTML parsing for tests
- Requests: HTTP client for server communication
- Term Frequency (TF): Frequency of term in document
- Inverse Document Frequency (IDF):
log(total_docs / docs_containing_term) - TF-IDF:
TF * IDF
The final score combines TF-IDF similarity with PageRank:
score = w * PageRank + (1-w) * cosine_similarity
Where w is the configurable PageRank weight.
- Clean and normalize query terms
- Remove stopwords
- Calculate query vector using TF-IDF
- Find documents containing all query terms
- Compute cosine similarity between query and document vectors
- Combine with PageRank scores
- Return top-ranked results
- Distributed indexing: 3 index server instances for parallel processing
- Concurrent queries: Search server makes parallel requests to all index instances
- Efficient data structures: In-memory inverted indexes for fast lookups
- Result merging: Uses heap merge for efficient result aggregation
- Index server not starting: Check if ports 9000-9002 are available
- Search server not starting: Ensure index server is running and database exists
- No search results: Verify inverted index files are properly generated and copied
- Database errors: Check that
var/search.sqlite3exists and is accessible
This project is part of an academic assignment and follows the course guidelines.