https://github.com/darenr/python-whoosh-simple-example
An example of how to use whoosh to index and search documents
https://github.com/darenr/python-whoosh-simple-example
python3 search-engine whoosh
Last synced: 5 months ago
JSON representation
An example of how to use whoosh to index and search documents
- Host: GitHub
- URL: https://github.com/darenr/python-whoosh-simple-example
- Owner: darenr
- License: apache-2.0
- Created: 2020-09-05T03:05:50.000Z (about 5 years ago)
- Default Branch: master
- Last Pushed: 2020-11-11T18:21:36.000Z (almost 5 years ago)
- Last Synced: 2025-05-08T01:15:38.689Z (5 months ago)
- Topics: python3, search-engine, whoosh
- Language: Python
- Homepage:
- Size: 15.6 KB
- Stars: 11
- Watchers: 1
- Forks: 4
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# python-whoosh-simple-example
An example of how to use whoosh to index and search documents```
docs = [
{
"id": "1",
"title": "First document banana",
"description": "This is the first document we've added!",
"tags": ['foo', 'bar']
},
{
"id": "2",
"title": "Second document hatstand",
"description": "The second one is even more interesting!",
"tags": ['alice']
},
{
"id": "3",
"title": "Third document slug",
"description": "The third one is less interesting!",
"tags": ['bob']
},
]schema = Schema(
id=ID(stored=True),
title=TEXT(stored=True),
description=TEXT(stored=True, analyzer=StemmingAnalyzer()),
tags=KEYWORD(stored=True)
)engine = SearchEngine(schema)
engine.index_documents(docs)print(f"indexed {engine.get_index_size()} documents")
fields_to_search = ["title", "description", "tags"]
for q in ["hatstand", "banana", "first", "second", "alice", "bob"]:
print(f"Query:: {q}")
print("\t", engine.query(q, fields_to_search, highlight=True))
print("-"*70)
```produces the following output:
```
indexed 3 documents
Query:: hatstand
[{'description': 'The second one is even more interesting!', 'id': '2', 'tags': ['alice'], 'title': 'Second document hatstand'}]
----------------------------------------------------------------------
Query:: banana
[{'description': "This is the first document we've added!", 'id': '1', 'tags': ['foo', 'bar'], 'title': 'First document banana'}]
----------------------------------------------------------------------
Query:: first
[{'description': 'This is the first document we\'ve added', 'id': '1', 'tags': ['foo', 'bar'], 'title': 'First document banana'}]
----------------------------------------------------------------------
Query:: second
[{'description': 'The second one is even more interesting', 'id': '2', 'tags': ['alice'], 'title': 'Second document hatstand'}]
----------------------------------------------------------------------
Query:: alice
[{'description': 'The second one is even more interesting!', 'id': '2', 'tags': ['alice'], 'title': 'Second document hatstand'}]
----------------------------------------------------------------------
Query:: bob
[{'description': 'The third one is less interesting!', 'id': '3', 'tags': ['bob'], 'title': 'Third document slug'}]
```