Saturday, December 17, 2022

Facets in Whoosh - an example !

from whoosh import index, qparser
from whoosh.fields import *

# Define the schema for the index
schema = Schema(title=TEXT(stored=True),
                author=TEXT(stored=True),
                category=KEYWORD(stored=True),
                content=TEXT)

# Create the index
ix = index.create_in("indexdir", schema)

# Open the index for writing
writer = ix.writer()

# Add documents to the index
writer.add_document(title="Document 1",
                    author="Author 1",
                    category="Category 1",
                    content="This is the content of document 1")
writer.add_document(title="Document 2",
                    author="Author 2",
                    category="Category 2",
                    content="This is the content of document 2")
writer.add_document(title="Document 3",
                    author="Author 3",
                    category="Category 1",
                    content="This is the content of document 3")

# Commit the changes
writer.commit()

# Open the index for reading
searcher = ix.searcher()

# Parse the query
parser = qparser.QueryParser("content", ix.schema)
query = parser.parse("document")

# Perform the search and return the results
results = searcher.search(query)

# Iterate through the results and print them
for result in results:
    print(result)

# Faceted search:
facet = searcher.facet_by_fieldname("category")
for category in facet:
    print(f"Category: {category}")
    print(f"Number of documents: {facet[category]}")

No comments:

Post a Comment