Search engine in cpp

 Creating a full-fledged search engine in C++ is a complex and ambitious project that typically requires a significant amount of resources, expertise, and computational power. A full search engine, like Google, involves web crawling, indexing, ranking, and a user interface, and may not be a feasible project for a single developer. However, I can provide a simplified example of a basic text-based search engine to get you started:


Source code


#include <iostream>

#include <string>

#include <vector>

#include <map>


std::map<std::string, std::vector<std::string>> index;


void indexDocument(const std::string& document, const std::string& documentName) {

    std::string word;

    std::istringstream iss(document);


    while (iss >> word) {

        // Remove punctuation and convert to lowercase (simplified)

        std::string cleanedWord = "";

        for (char c : word) {

            if (isalpha(c)) {

                cleanedWord += tolower(c);

            }

        }


        if (!cleanedWord.empty()) {

            index[cleanedWord].push_back(documentName);

        }

    }

}


std::vector<std::string> search(const std::string& query) {

    std::string cleanedQuery = "";

    for (char c : query) {

        if (isalpha(c)) {

            cleanedQuery += tolower(c);

        }

    }


    if (index.find(cleanedQuery) != index.end()) {

        return index[cleanedQuery];

    } else {

        return {};

    }

}


int main() {

    std::string documentName, document;


    // Index some example documents

    documentName = "document1.txt";

    document = "This is an example document for the search engine.";

    indexDocument(document, documentName);


    documentName = "document2.txt";

    document = "C++ is a programming language used to build software applications.";

    indexDocument(document, documentName);


    std::string query;

    std::cout << "Enter a search query: ";

    std::cin.ignore();

    std::getline(std::cin, query);


    std::vector<std::string> results = search(query);


    if (!results.empty()) {

        std::cout << "Search results:\n";

        for (const std::string& result : results) {

            std::cout << result << "\n";

        }

    } else {

        std::cout << "No results found for the query.\n";

    }


    return 0;

}




This example demonstrates a simplified text-based search engine. It allows you to index and search documents by keywords. The search engine tokenizes the input text, removes punctuation and converts words to lowercase for a basic search functionality. This is far from a production-grade search engine but serves as a starting point for learning about the basic principles involved.


To build a more advanced search engine, you would need to consider issues like web crawling, ranking algorithms, distributed systems, and much more. Building a fully-featured search engine is a massive project that typically involves teams of engineers and significant computational resources.

Next Post Previous Post
No Comment
Add Comment
comment url