Skip to content

Whoosh

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

  • Okapi BM25F ranking function 사용
  • Lucene 같이 엿같은 java 환경 안 써도 됨
  • 모든 인덱스는 반드시 unicode이어야 함

See also

Favorite site