from whoosh import index from whoosh.fields import Schema, TEXT, ID from whoosh.qparser import QueryParser def create_index(): # Define the schema for the search index schema = Schema(title=TEXT(stored=True), content=TEXT(stored=True), path=ID(stored=True)) # Create a new index in a directory ix = index.create_in("index_directory", schema) # Open a writer to add documents to the index writer = ix.writer() # Add sample documents to the index writer.add_document(title="Document 1", content="This is the content of document 1", path="/doc1") writer.add_document(title="Document 2", content="This is the content of document 2", path="/doc2") writer.add_document(title="Document 3", content="This is the content of document 3", path="/doc3") # Commit the changes and close the writer writer.commit() def search(query): # Open the index directory ix = index.open_dir("index_directory") # Create a searcher to perform searches with ix.searcher() as searcher: # Create a query parser for the schema query_parser = QueryParser("content", schema=ix.schema) # Parse the user's query parsed_query = query_parser.parse(query) # Execute the search and get the results results = searcher.search(parsed_query) # Print the search results for hit in results: print("Title:", hit["title"]) print("Content:", hit["content"]) print("Path:", hit["path"]) print() # Create the search index create_index() # Perform a search search("content")
Article search engine using Python
Article search engine using Python