2020-11-19 22:33:53 +00:00
|
|
|
from typing import List, Generator
|
2020-11-18 22:54:13 +00:00
|
|
|
|
|
|
|
from sonic import IngestClient, SearchClient
|
|
|
|
|
|
|
|
from archivebox.util import enforce_types
|
2020-11-19 13:06:13 +00:00
|
|
|
from archivebox.config import SEARCH_BACKEND_HOST_NAME, SEARCH_BACKEND_PORT, SEARCH_BACKEND_PASSWORD, SONIC_BUCKET, SONIC_COLLECTION
|
|
|
|
|
2021-01-20 19:51:46 +00:00
|
|
|
MAX_SONIC_TEXT_LENGTH = 2000
|
2020-11-18 22:54:13 +00:00
|
|
|
|
|
|
|
@enforce_types
|
|
|
|
def index(snapshot_id: str, texts: List[str]):
|
2020-11-19 13:06:13 +00:00
|
|
|
with IngestClient(SEARCH_BACKEND_HOST_NAME, SEARCH_BACKEND_PORT, SEARCH_BACKEND_PASSWORD) as ingestcl:
|
2020-11-18 22:54:13 +00:00
|
|
|
for text in texts:
|
2020-11-26 23:12:54 +00:00
|
|
|
chunks = [text[i:i+MAX_SONIC_TEXT_LENGTH] for i in range(0, len(text), MAX_SONIC_TEXT_LENGTH)]
|
|
|
|
for chunk in chunks:
|
|
|
|
ingestcl.push(SONIC_COLLECTION, SONIC_BUCKET, snapshot_id, str(chunk))
|
|
|
|
|
2020-11-18 22:54:13 +00:00
|
|
|
@enforce_types
|
2020-11-19 22:33:53 +00:00
|
|
|
def search(text: str) -> List[str]:
|
2020-11-19 13:06:13 +00:00
|
|
|
with SearchClient(SEARCH_BACKEND_HOST_NAME, SEARCH_BACKEND_PORT, SEARCH_BACKEND_PASSWORD) as querycl:
|
2020-11-19 21:45:12 +00:00
|
|
|
snap_ids = querycl.query(SONIC_COLLECTION, SONIC_BUCKET, text)
|
2020-11-18 22:54:13 +00:00
|
|
|
return snap_ids
|
2020-11-19 21:45:12 +00:00
|
|
|
|
|
|
|
@enforce_types
|
2020-11-19 22:33:53 +00:00
|
|
|
def flush(snapshot_ids: Generator[str, None, None]):
|
2020-11-19 21:45:12 +00:00
|
|
|
with IngestClient(SEARCH_BACKEND_HOST_NAME, SEARCH_BACKEND_PORT, SEARCH_BACKEND_PASSWORD) as ingestcl:
|
|
|
|
for id in snapshot_ids:
|
|
|
|
ingestcl.flush_object(SONIC_COLLECTION, SONIC_BUCKET, str(id))
|