Skip to content

About

Distributed search engine using MapReduce for index construction and Flask for web-based query processing with TF-IDF and PageRank ranking.

Resources

Stars

1 star

Watchers

1 watching

Forks

Latest commit

 

History

39 Commits

Folders and files

Repository files navigation

Search Engine

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.

Architecture

The search engine follows a distributed architecture with the following components:

1. MapReduce Pipeline (inverted_index/)

  • 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

2. Index Server (index_server/)

  • 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

3. Search Server (search_server/)

  • 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

Installation

  1. Clone the repository:

    git clone <repository-url>
    cd search-engine
  2. Install dependencies:

    pip install -r requirements.txt
  3. Set up the database:

    # Ensure the database file exists at var/search.sqlite3
    mkdir -p var
    # Copy your SQLite database to var/search.sqlite3

Usage

Building the Inverted Index

  1. Prepare input data:

    • Place CSV files in inverted_index/input/
    • CSV format: docid,title,body
  2. Run the MapReduce pipeline:

    cd inverted_index
    ./pipeline.sh input
  3. Copy generated indexes:

    # Copy the generated index files to the index server
    cp output6/part-* ../index_server/index/inverted_index/

Starting the Services

  1. Start the Index Server:

    ./bin/index start

    This starts 3 Flask instances on ports 9000, 9001, and 9002.

  2. Start the Search Server:

    ./bin/searchdb start

    This starts the web interface on port 8000.

  3. Access the search interface: Open your browser and navigate to http://localhost:8000

Service Management

  • 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

API Usage

Index Server API

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
    }
  ]
}

Search Server Web Interface

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

Configuration

Index Server Configuration

  • Index files: Located in index_server/index/inverted_index/
  • Stopwords: index_server/index/stopwords.txt
  • PageRank scores: index_server/index/pagerank.out

Search Server Configuration

  • Database: var/search.sqlite3
  • Index server URLs: Configured in search_server/search/config.py

File Structure

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)

Testing

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

Dependencies

  • 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

Algorithm Details

TF-IDF Calculation

  • Term Frequency (TF): Frequency of term in document
  • Inverse Document Frequency (IDF): log(total_docs / docs_containing_term)
  • TF-IDF: TF * IDF

Document Ranking

The final score combines TF-IDF similarity with PageRank:

score = w * PageRank + (1-w) * cosine_similarity

Where w is the configurable PageRank weight.

Query Processing

  1. Clean and normalize query terms
  2. Remove stopwords
  3. Calculate query vector using TF-IDF
  4. Find documents containing all query terms
  5. Compute cosine similarity between query and document vectors
  6. Combine with PageRank scores
  7. Return top-ranked results

Performance Features

  • 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

Troubleshooting

  1. Index server not starting: Check if ports 9000-9002 are available
  2. Search server not starting: Ensure index server is running and database exists
  3. No search results: Verify inverted index files are properly generated and copied
  4. Database errors: Check that var/search.sqlite3 exists and is accessible

License

This project is part of an academic assignment and follows the course guidelines.

About

Distributed search engine using MapReduce for index construction and Flask for web-based query processing with TF-IDF and PageRank ranking.

Resources

Stars

1 star

Watchers

1 watching

Forks

Releases

Packages

Contributors

Languages