diff --git a/service/src/data/search_scores.json b/service/src/data/search_scores.json new file mode 100644 index 00000000..f91d778f --- /dev/null +++ b/service/src/data/search_scores.json @@ -0,0 +1,206 @@ +{ + "query": "of", + "max_score": 0.6724651, + "results": [ + { + "topic_name": "Reformed Theology and Governance", + "score": 0.6724651 + }, + { + "topic_name": "Law in Society and Culture", + "score": 0.67059207 + }, + { + "topic_name": "Umberto Eco and Semiotics", + "score": 0.6702657 + }, + { + "topic_name": "National Identity and Symbolism", + "score": 0.66791224 + }, + { + "topic_name": "Posthumanist Ethics and Activism", + "score": 0.6668408 + }, + { + "topic_name": "Semiotics and Representation Studies", + "score": 0.6653223 + }, + { + "topic_name": "Philosophy, Ethics, and Existentialism", + "score": 0.6633806 + }, + { + "topic_name": "Decadence, Literature, and Society", + "score": 0.66336894 + }, + { + "topic_name": "Environmental Philosophy and Ethics", + "score": 0.6618414 + }, + { + "topic_name": "Hermeneutics and Narrative Identity", + "score": 0.6599164 + }, + { + "topic_name": "Ecocriticism and Environmental Literature", + "score": 0.6554141 + }, + { + "topic_name": "Art, Aesthetics, and Perception", + "score": 0.6551213 + }, + { + "topic_name": "Cultural Studies and Postmodernism", + "score": 0.6535654 + }, + { + "topic_name": "Education, Philosophy, and Society", + "score": 0.65311265 + }, + { + "topic_name": "Philosophy and Literary Analysis", + "score": 0.6528132 + }, + { + "topic_name": "Libraries and Information Services", + "score": 0.65191627 + }, + { + "topic_name": "Rhetoric and Communication Studies", + "score": 0.6518581 + }, + { + "topic_name": "Ethics, Aesthetics, and Art", + "score": 0.6518519 + }, + { + "topic_name": "Foucault, Power, and Ethics", + "score": 0.6514168 + }, + { + "topic_name": "Media, Communication, and Education", + "score": 0.6500728 + }, + { + "topic_name": "Corporeality, Perception, and Education", + "score": 0.6498234 + }, + { + "topic_name": "Visual Culture and Art Theory", + "score": 0.6482606 + }, + { + "topic_name": "Sound Studies and Aurality", + "score": 0.647634 + }, + { + "topic_name": "Emile Durkheim and Sociology", + "score": 0.64713025 + }, + { + "topic_name": "Phenomenology and Existential Philosophy", + "score": 0.64691377 + }, + { + "topic_name": "Critical Realism in Sociology", + "score": 0.64657044 + }, + { + "topic_name": "Contemporary Literature and Criticism", + "score": 0.6464033 + }, + { + "topic_name": "Diverse Cultural and Social Studies", + "score": 0.6460452 + }, + { + "topic_name": "Literary and Philosophical Studies", + "score": 0.6453686 + }, + { + "topic_name": "Poetry Analysis and Criticism", + "score": 0.6450021 + }, + { + "topic_name": "Hume's philosophy and hair distribution", + "score": 0.6447551 + }, + { + "topic_name": "Law, Rights, and Freedoms", + "score": 0.6444795 + }, + { + "topic_name": "Interdisciplinary Studies: Technology, Society, and Humanities", + "score": 0.64430857 + }, + { + "topic_name": "European Cultural and National Identity", + "score": 0.64404917 + }, + { + "topic_name": "Critical Theory and Philosophy", + "score": 0.64360785 + }, + { + "topic_name": "Social Representations and Identity", + "score": 0.6434338 + }, + { + "topic_name": "Modern American Literature Studies", + "score": 0.643306 + }, + { + "topic_name": "Information Systems Theories and Implementation", + "score": 0.64326954 + }, + { + "topic_name": "Qualitative Research Methods and Ethics", + "score": 0.64290214 + }, + { + "topic_name": "Literary Theory and Cultural Hermeneutics", + "score": 0.64285755 + }, + { + "topic_name": "Literature, Culture, and Aesthetics", + "score": 0.6425061 + }, + { + "topic_name": "Political and Economic history of UK and US", + "score": 0.6419141 + }, + { + "topic_name": "Art, Politics, and Modernism", + "score": 0.6418526 + }, + { + "topic_name": "Cultural Identity and Representation", + "score": 0.6417024 + }, + { + "topic_name": "Karl Barth and Christian Theology", + "score": 0.6416898 + }, + { + "topic_name": "Critical Theory and Political Philosophy", + "score": 0.6409569 + }, + { + "topic_name": "Educational Philosophies and Pedagogies", + "score": 0.6407592 + }, + { + "topic_name": "Literary and Cultural Studies", + "score": 0.64029 + }, + { + "topic_name": "Cultural, Social, and Media Studies", + "score": 0.64006495 + }, + { + "topic_name": "Families in Therapy and Culture", + "score": 0.63989353 + } + ] +} diff --git a/service/src/main/inse/topics/logic.py b/service/src/main/inse/topics/logic.py index 681f480f..0ce6c903 100644 --- a/service/src/main/inse/topics/logic.py +++ b/service/src/main/inse/topics/logic.py @@ -297,7 +297,8 @@ async def vector_search_topics( }, "_source": { "excludes": ["vector"] #remove vectors - } + }, + "size": limit } try: @@ -315,8 +316,25 @@ async def vector_search_topics( # 3. Format result hits = raw_results.get("hits", {}).get("hits", []) - total = len(hits) + # Log scores to JSON for threshold analysis TODO: Remove after finetune + try: + from .score_logger import log_scores_to_json + log_scores_to_json(hits, query) + except Exception as exc: + logger.warning(f"Could not log search scores: {exc}") + + # Apply dynamic threshold cutoff based on the best match's score + if hits: + max_score = hits[0].get("_score", 0.0) + if max_score > 0.75: + threshold = max_score - 0.1 + else: + threshold = 0.65 + + hits = [hit for hit in hits if hit.get("_score", 0.0) >= threshold] + + total = len(hits) results = [hit.get("_source", {}) for hit in hits] data = { diff --git a/service/src/main/inse/topics/score_logger.py b/service/src/main/inse/topics/score_logger.py new file mode 100644 index 00000000..0493d9ad --- /dev/null +++ b/service/src/main/inse/topics/score_logger.py @@ -0,0 +1,36 @@ +import json +from pathlib import Path +from typing import Any, Dict, List + +def log_scores_to_json(hits: List[Dict[str, Any]], query: str) -> None: + """ + Utility function to log the names and scores of all vector search hits to a JSON file. + This helps in debugging and manually tweaking the similarity score thresholds. + """ + scores_data = [] + for hit in hits: + source = hit.get("_source", {}) + topic_name = source.get("display_name") or source.get("name") or "Unknown" + score = hit.get("_score", 0.0) + scores_data.append({ + "topic_name": topic_name, + "score": score + }) + + + data_dir = Path("/data") + + output_file = data_dir / "search_scores.json" + + log_payload = { + "query": query, + "max_score": hits[0].get("_score", 0.0) if hits else 0.0, + "results": scores_data + } + + try: + with open(output_file, "w", encoding="utf-8") as f: + json.dump(log_payload, f, indent=2, ensure_ascii=False) + print(f"DEBUG: Saved search scores for query '{query}' to {output_file}") + except Exception as exc: + print(f"DEBUG: Failed to save search scores: {exc}")