Upload app.py
Browse files
app.py
CHANGED
|
@@ -289,7 +289,7 @@ def trim_record(rec: dict) -> dict:
|
|
| 289 |
"HasTxt": True,
|
| 290 |
}
|
| 291 |
|
| 292 |
-
def add_summaries(items: list[dict], query: str = "", matched_snippets: bool = False) -> list[dict]:
|
| 293 |
if fulltext_databases is None or not items:
|
| 294 |
return items
|
| 295 |
summaries = fulltext_databases.summaries([item["doc_id"] for item in items])
|
|
@@ -298,8 +298,13 @@ def add_summaries(items: list[dict], query: str = "", matched_snippets: bool = F
|
|
| 298 |
if matched_snippets and query:
|
| 299 |
snippet = build_first_match_snippet(summary, query)
|
| 300 |
normalized_summary = normalize_text(summary)
|
| 301 |
-
|
| 302 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 303 |
file_path = get_doc_storage_path(rec) if rec else None
|
| 304 |
if file_path and file_path.exists():
|
| 305 |
text = file_path.read_text(encoding="utf-8", errors="ignore")
|
|
@@ -385,9 +390,11 @@ def fulltext_search(q="", sources_filter=None, folders=None, min_size=None, max_
|
|
| 385 |
matched_doc_ids = set()
|
| 386 |
for source_slug in source_slugs:
|
| 387 |
matched_doc_ids.update(f"{source_slug}:{doc_number}" for doc_number in fulltext_databases.search_source(source_slug, q, exact))
|
| 388 |
-
|
|
|
|
| 389 |
if exact and ("*" in q or "?" in q or len(normalize_text(q)) > 3):
|
| 390 |
-
|
|
|
|
| 391 |
indices.update(metadata_matches(q, exact, search_paths))
|
| 392 |
filtered = apply_filters(indices, sources_filter, folders, min_size, max_size)
|
| 393 |
tokens = query_tokens(q)
|
|
@@ -403,6 +410,7 @@ def fulltext_search(q="", sources_filter=None, folders=None, min_size=None, max_
|
|
| 403 |
[trim_record(records[idx]) for idx in filtered[start:start + page_size]],
|
| 404 |
q,
|
| 405 |
matched_snippets=True,
|
|
|
|
| 406 |
)
|
| 407 |
return {"results": result_items, "total": total, "page": page, "page_size": page_size}
|
| 408 |
|
|
|
|
| 289 |
"HasTxt": True,
|
| 290 |
}
|
| 291 |
|
| 292 |
+
def add_summaries(items: list[dict], query: str = "", matched_snippets: bool = False, content_indices: set[int] | None = None) -> list[dict]:
|
| 293 |
if fulltext_databases is None or not items:
|
| 294 |
return items
|
| 295 |
summaries = fulltext_databases.summaries([item["doc_id"] for item in items])
|
|
|
|
| 298 |
if matched_snippets and query:
|
| 299 |
snippet = build_first_match_snippet(summary, query)
|
| 300 |
normalized_summary = normalize_text(summary)
|
| 301 |
+
rec = record_map.get(item["doc_id"])
|
| 302 |
+
rec_index = record_map_index.get(item["doc_id"])
|
| 303 |
+
if (
|
| 304 |
+
content_indices is not None
|
| 305 |
+
and rec_index in content_indices
|
| 306 |
+
and not any(token in normalized_summary for token in query_tokens(query))
|
| 307 |
+
):
|
| 308 |
file_path = get_doc_storage_path(rec) if rec else None
|
| 309 |
if file_path and file_path.exists():
|
| 310 |
text = file_path.read_text(encoding="utf-8", errors="ignore")
|
|
|
|
| 390 |
matched_doc_ids = set()
|
| 391 |
for source_slug in source_slugs:
|
| 392 |
matched_doc_ids.update(f"{source_slug}:{doc_number}" for doc_number in fulltext_databases.search_source(source_slug, q, exact))
|
| 393 |
+
content_indices = {idx for doc_id in matched_doc_ids if (idx := record_map_index.get(doc_id)) is not None}
|
| 394 |
+
indices = set(content_indices)
|
| 395 |
if exact and ("*" in q or "?" in q or len(normalize_text(q)) > 3):
|
| 396 |
+
content_indices = verify_fulltext_matches(content_indices, q)
|
| 397 |
+
indices = set(content_indices)
|
| 398 |
indices.update(metadata_matches(q, exact, search_paths))
|
| 399 |
filtered = apply_filters(indices, sources_filter, folders, min_size, max_size)
|
| 400 |
tokens = query_tokens(q)
|
|
|
|
| 410 |
[trim_record(records[idx]) for idx in filtered[start:start + page_size]],
|
| 411 |
q,
|
| 412 |
matched_snippets=True,
|
| 413 |
+
content_indices=content_indices,
|
| 414 |
)
|
| 415 |
return {"results": result_items, "total": total, "page": page, "page_size": page_size}
|
| 416 |
|