File size: 676 Bytes
e61e934
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
import os
import re
import json
from whoosh.index import open_dir
from whoosh.qparser import MultifieldParser

WHOOSH_INDEX_PATH = "/home/user/app/persistent/whoosh_index"

_ix = None

def _load_whoosh():
    global _ix
    if _ix is None and os.path.exists(WHOOSH_INDEX_PATH):
        _ix = open_dir(WHOOSH_INDEX_PATH)
    return _ix

def _bm25_search(query, top_n=10):
    ix = _load_whoosh()
    if not ix:
        return []
    parser = MultifieldParser(["text", "title"], schema=ix.schema)
    q = parser.parse(query)
    with ix.searcher() as s:
        results = s.search(q, limit=top_n)
        return [{"text": r["text"], "file": r.get("file", "")} for r in results]