diff --git a/background-jobs/app/sync.py b/background-jobs/app/sync.py index 72f330b4..c0cf185e 100644 --- a/background-jobs/app/sync.py +++ b/background-jobs/app/sync.py @@ -6,6 +6,8 @@ import time import threading from concurrent.futures import ThreadPoolExecutor, as_completed +POSTPROC_INDEX = "aktuell_postproc" + def print_pretty_json(data): print(json.dumps(data, indent=2)) @@ -34,15 +36,32 @@ def sync_single_news(news_id, max_retries=3): print(f"Error: {news_id} - {error}") return False -def update_keywords_single(postproc_entry, max_retries=3): +def update_keywords_single(postproc_entry, target_index=POSTPROC_INDEX, max_retries=3): doc_id = postproc_entry["_id"] - news_id = postproc_entry["_source"]["news_id"] + source = postproc_entry["_source"] + news_id = source["news_id"] attempt = 0 while attempt < max_retries: try: news_text = es_client.get_entry("aktuell", news_id)["_source"]["full_content"] keywords = llm_client.extract_keywords(news_text) - es_client.update_entry("aktuell_postproc", doc_id, {"keywords": keywords}) + if not keywords: + # Empty article or unparseable LLM answer: writing an empty list + # would look like a success and block a later retry. + print(f"Skipped {news_id}: no keywords extracted.") + return False + if target_index == POSTPROC_INDEX: + es_client.update_entry(POSTPROC_INDEX, doc_id, {"keywords": keywords}) + else: + es_client.add_entry( + target_index, + { + "news_id": news_id, + "relevant": source.get("relevant", 0), + "authors": source.get("authors", []), + "keywords": keywords, + }, + ) return True except Exception as error: attempt += 1 @@ -100,6 +119,16 @@ def main(): help="Process at most this many entries (used by --update-keywords, e.g. for a smoke test).", ) + parser.add_argument( + "--target-index", + default=POSTPROC_INDEX, + help=( + f"Index that --update-keywords writes to (default: {POSTPROC_INDEX}). Any other " + "index gets new documents instead of in-place updates, and every entry is " + "re-extracted rather than only those missing keywords." + ), + ) + args = parser.parse_args() if args.delete_all: @@ -143,10 +172,20 @@ def main(): if args.update_keywords: start_time = time.time() - postproc_entries = es_client.get_all_entries("aktuell_postproc", fields=["news_id", "keywords"]) + postproc_entries = es_client.get_all_entries( + POSTPROC_INDEX, fields=["news_id", "keywords", "relevant", "authors"] + ) - pending = [entry for entry in postproc_entries if not entry["_source"].get("keywords")] - print(f"Number of entries without keywords: {len(pending)}") + if args.target_index == POSTPROC_INDEX: + pending = [entry for entry in postproc_entries if not entry["_source"].get("keywords")] + print(f"Number of entries without keywords: {len(pending)}") + else: + # Writing elsewhere never marks the postproc entries, so the "missing + # keywords" filter would select the same entries on every run. Pick + # entries that already have keywords instead: those are known to come + # from articles with actual content, unlike the empty archive stubs. + pending = [entry for entry in postproc_entries if entry["_source"].get("keywords")] + print(f"Re-extracting keywords into '{args.target_index}': {len(pending)} candidates.") if args.limit is not None: pending = pending[:args.limit] @@ -159,7 +198,10 @@ def main(): progress_lock = threading.Lock() with ThreadPoolExecutor(max_workers=args.threads) as executor: - futures = [executor.submit(update_keywords_single, entry) for entry in pending] + futures = [ + executor.submit(update_keywords_single, entry, args.target_index) + for entry in pending + ] for future in as_completed(futures): was_successful = future.result() with progress_lock: