Skip to content
Search

Whoosh

lastmod 2024-11-21created 2024-11-211 edits

pure-Python search engine

Install

pip install whoosh

Example

from whoosh.index import create_in
from whoosh.fields import Schema, TEXT
from whoosh.qparser import QueryParser

# 스키마 정의
schema = Schema(content=TEXT(stored=True))

# 인덱스 생성
import os
if not os.path.exists("indexdir"):
    os.mkdir("indexdir")
ix = create_in("indexdir", schema)

# 문서 추가
writer = ix.writer()
writer.add_document(content="Python은 강력한 프로그래밍 언어입니다.")
writer.add_document(content="데이터 분석과 머신러닝에 적합합니다.")
writer.commit()

# 검색
with ix.searcher() as searcher:
    query = QueryParser("content", ix.schema).parse("Python")
    results = searcher.search(query)
    for result in results:
        print(result["content"])

Features

  • Uses the Okapi BM25F ranking function
  • No need for a crappy java environment like Lucene's
  • All indexes must be unicode

See also

Favorite site

author Zer0 sitemap