__init__.py 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. from typing import List, Union
  2. from pathlib import Path
  3. from importlib import import_module
  4. from django.db.models import QuerySet, Model
  5. from archivebox.index.schema import Link
  6. from archivebox.util import enforce_types
  7. from archivebox.config import stderr, OUTPUT_DIR, USE_INDEXING_BACKEND, USE_SEARCHING_BACKEND, SEARCH_BACKEND_ENGINE
  8. from .utils import get_indexable_content, log_index_started
  9. def indexing_enabled():
  10. return USE_INDEXING_BACKEND
  11. def search_backend_enabled():
  12. return USE_SEARCHING_BACKEND
  13. def get_backend():
  14. return f'search.backends.{SEARCH_BACKEND_ENGINE}'
  15. def import_backend():
  16. backend_string = get_backend()
  17. try:
  18. backend = import_module(backend_string)
  19. except Exception as err:
  20. raise Exception("Could not load '%s' as a backend: %s" % (backend_string, err))
  21. return backend
  22. @enforce_types
  23. def write_search_index(snapshot: Model, texts: Union[List[str], None]=None, out_dir: Path=OUTPUT_DIR, skip_text_index: bool=False) -> None:
  24. if not indexing_enabled():
  25. return
  26. if not skip_text_index and texts:
  27. from core.models import Snapshot
  28. backend = import_backend()
  29. try:
  30. backend.index(snapshot_id=str(snapshot.id), texts=texts)
  31. except Exception as err:
  32. stderr()
  33. stderr(
  34. f'[X] The search backend threw an exception={err}:',
  35. color='red',
  36. )
  37. @enforce_types
  38. def query_search_index(query: str, out_dir: Path=OUTPUT_DIR) -> QuerySet:
  39. from core.models import Snapshot
  40. if search_backend_enabled():
  41. backend = import_backend()
  42. try:
  43. snapshot_ids = backend.search(query)
  44. except Exception as err:
  45. stderr()
  46. stderr(
  47. f'[X] The search backend threw an exception={err}:',
  48. color='red',
  49. )
  50. raise
  51. else:
  52. # TODO preserve ordering from backend
  53. qsearch = Snapshot.objects.filter(pk__in=snapshot_ids)
  54. return qsearch
  55. return Snapshot.objects.none()
  56. @enforce_types
  57. def flush_search_index(snapshots: QuerySet):
  58. if not indexing_enabled() or not snapshots:
  59. return
  60. backend = import_backend()
  61. snapshot_ids=(str(pk) for pk in snapshots.values_list('pk',flat=True))
  62. try:
  63. backend.flush(snapshot_ids)
  64. except Exception as err:
  65. stderr()
  66. stderr(
  67. f'[X] The search backend threw an exception={err}:',
  68. color='red',
  69. )
  70. @enforce_types
  71. def index_snapshots(snapshots: Union[List[Model],None], out_dir: Path=OUTPUT_DIR):
  72. if not snapshots:
  73. return
  74. from core.models import Snapshot, ArchiveResult
  75. for snapshot in snapshots:
  76. snap = Snapshot.objects.filter(url=snapshot.url).first()
  77. if snap:
  78. results = ArchiveResult.objects.indexable().filter(snapshot=snap)
  79. log_index_started(snapshot.url)
  80. try:
  81. texts = get_indexable_content(results)
  82. except Exception as err:
  83. stderr()
  84. stderr(
  85. f'[X] An Exception ocurred reading the indexable content={err}:',
  86. color='red',
  87. )
  88. else:
  89. write_search_index(snapshot, texts, out_dir=out_dir)