Autocomplétion avec Whoosh‑NG
Cet exemple démontre la fonctionnalité autocomplete/suggestion avec le plugin whoosh_modern.autocomplete.
1. Installation
pip install "whoosh-ng[autocomplete]"
2. Schéma avec champ Keyword pour les termes
from whoosh import index
from whoosh.fields import Schema, TEXT, KEYWORD
from whoosh_modern.autocomplete.plugin import AutocompletePlugin
from whoosh.plugins.manager import PluginManager
schema = Schema(
title=TEXT(stored=True),
tags=KEYWORD(stored=True, commas=True),
)
ix = index.create_in("autocomplete_index", schema)
3. Enregistrer le plugin
AutocompletePlugin().register(PluginManager())
4. Indexer les documents
with ix.writer() as w:
w.add_document(title="Python Programming", tags="python,programming,language")
w.add_document(title="JavaScript Basics", tags="javascript,programming,web")
w.add_document(title="Machine Learning", tags="ml,ai,data-science")
w.commit()