mirror of
https://github.com/ArchiveBox/ArchiveBox.git
synced 2026-04-05 23:37:58 +10:00
Use QuerySets for search backend API instead of pks
This commit is contained in:
@@ -1,7 +1,8 @@
|
||||
from typing import List, Union, Generator
|
||||
from typing import List, Union
|
||||
from pathlib import Path
|
||||
from importlib import import_module
|
||||
|
||||
from django.db.models import QuerySet
|
||||
|
||||
from archivebox.index.schema import Link
|
||||
from archivebox.util import enforce_types
|
||||
@@ -39,16 +40,24 @@ def write_search_index(link: Link, texts: Union[List[str], None]=None, out_dir:
|
||||
backend.index(snapshot_id=str(snap.id), texts=texts)
|
||||
|
||||
@enforce_types
|
||||
def query_search_index(text: str) -> List[str]:
|
||||
def query_search_index(query: str, out_dir: Path=OUTPUT_DIR) -> QuerySet:
|
||||
if search_backend_enabled():
|
||||
setup_django(out_dir, check_db=True)
|
||||
from core.models import Snapshot
|
||||
|
||||
backend = import_backend()
|
||||
return backend.search(text)
|
||||
snapshot_ids = backend.search(query)
|
||||
# TODO preserve ordering from backend
|
||||
qsearch = Snapshot.objects.filter(pk__in=snapshot_ids)
|
||||
return qsearch
|
||||
else:
|
||||
return []
|
||||
|
||||
@enforce_types
|
||||
def flush_search_index(snapshot_ids: Generator[str, None, None]):
|
||||
if not indexing_enabled() or not snapshot_ids:
|
||||
def flush_search_index(snapshots: QuerySet):
|
||||
if not indexing_enabled() or not snapshots:
|
||||
return
|
||||
backend = import_backend()
|
||||
snapshot_ids=(str(pk) for pk in snapshots.values_list('pk',flat=True))
|
||||
|
||||
backend.flush(snapshot_ids)
|
||||
|
||||
Reference in New Issue
Block a user