feature/keywords-analysis #289

Merged
genja merged 3 commits from feature/keywords-analysis into dev 2026-08-17 14:53:49 +02:00
6 changed files with 70 additions and 41 deletions
@@ -781,6 +781,7 @@ async def _build_eu_cordis_search_query(
topic_project_ids_by_type = await get_eu_project_ids_by_openalex_topic_ids(
project_types=project_types,
topic_ids=topic_ids,
match_all=False
)
topic_filter = _eu_project_type_index_filter(topic_project_ids_by_type)
@@ -829,13 +830,14 @@ async def _build_eu_cordis_search_query(
)
if keyword_clauses:
must_clauses.append({"bool": {"must": keyword_clauses}})
must_clauses.append({"bool": {"should": keyword_clauses, "minimum_should_match": 1}})
# === Independent OpenAlex topic filters ===
if filter_topic_ids:
filter_topic_project_ids_by_type = await get_eu_project_ids_by_openalex_topic_ids(
project_types=project_types,
topic_ids=filter_topic_ids,
match_all=True
)
topic_filter = _eu_project_type_index_filter(filter_topic_project_ids_by_type)
+58 -30
View File
@@ -115,17 +115,23 @@ async def get_person_ids_by_institution_ids(
}
)
async def get_bund_project_ids_by_topic_ids(topic_ids: list[str]) -> list[str]:
async def get_bund_project_ids_by_topic_ids(
topic_ids: list[str],
match_all: bool = True,
) -> list[str]:
"""
Query the `projects_bund_openalex_topics` index to find BUND project IDs
that match any of the given OpenAlex topic IDs.
that match:
- ALL of the given OpenAlex topic IDs if match_all=True
- ANY of the given OpenAlex topic IDs if match_all=False
Topics may be distributed across multiple documents for the same project.
"""
if not topic_ids:
return []
required_topics = set(topic_ids)
project_topics: dict[str, set[str]] = defaultdict(set)
body = {
@@ -143,8 +149,8 @@ async def get_bund_project_ids_by_topic_ids(topic_ids: list[str]) -> list[str]:
cluster=ESCluster.INSE,
body=body,
)
hits = response.get("hits", {}).get("hits", [])
hits = response.get("hits", {}).get("hits", [])
for hit in hits:
source = hit["_source"]
@@ -163,26 +169,35 @@ async def get_bund_project_ids_by_topic_ids(topic_ids: list[str]) -> list[str]:
if topic_id in required_topics:
project_topics[project_id].add(topic_id)
if match_all:
return [
project_id
for project_id, found_topics in project_topics.items()
if required_topics.issubset(found_topics)
]
# OR match: project has at least one requested topic
return list(project_topics.keys())
return [
project_id
for project_id, found_topics in project_topics.items()
if required_topics.issubset(found_topics)
]
async def get_dfg_project_ids_by_topic_ids(
topic_ids: list[str],
match_all: bool = True,
) -> list[str]:
"""
Fetches project IDs whose topic_matches contain:
async def get_dfg_project_ids_by_topic_ids(topic_ids: list[str]) -> list[str]:
"""Fetches project IDs whose topic_matches contain EVERY given topic ID,
even if the topics are distributed across multiple documents for the same project.
- ALL given topic IDs if match_all=True
- ANY given topic ID if match_all=False
Topics may be distributed across multiple documents for the same project.
"""
if not topic_ids:
return []
required_topics = set(topic_ids)
project_topics: dict[str, set[str]] = defaultdict(set)
body = {
"size": 10000,
"_source": ["project_id", "topic_matches"],
@@ -192,7 +207,7 @@ async def get_dfg_project_ids_by_topic_ids(topic_ids: list[str]) -> list[str]:
}
},
}
print("BODY", body)
response = await elasticsearch.search(
index=ESIndex.PROJECTS_DFG_TOPICS,
cluster=ESCluster.INSE,
@@ -201,7 +216,6 @@ async def get_dfg_project_ids_by_topic_ids(topic_ids: list[str]) -> list[str]:
hits = response.get("hits", {}).get("hits", [])
for hit in hits:
source = hit["_source"]
@@ -219,12 +233,15 @@ async def get_dfg_project_ids_by_topic_ids(topic_ids: list[str]) -> list[str]:
if topic_id in required_topics:
project_topics[project_id].add(topic_id)
if match_all:
return [
project_id
for project_id, found_topics in project_topics.items()
if required_topics.issubset(found_topics)
]
return [
project_id
for project_id, found_topics in project_topics.items()
if required_topics.issubset(found_topics)
]
# OR match: project has at least one requested topic
return list(project_topics.keys())
async def get_openalex_topics_for_bund_projects(project_ids: list[str]) -> dict[str, list[dict]]:
@@ -428,14 +445,18 @@ async def get_dfg_institutes_by_ids(
#EU Cordis Projects
async def get_eu_project_ids_by_openalex_topic_ids(
project_types: list[EuCordisProjectType],
topic_ids: list[str],
match_all: bool = True,
) -> dict[EuCordisProjectType, list[str]]:
"""
Returns EU CORDIS project IDs whose mapped OpenAlex topic_matches contain
EVERY given topic ID. Same semantics as DFG topic filtering.
Returns EU CORDIS project IDs whose mapped OpenAlex topic_matches contain:
- ALL given topic IDs if match_all=True
- ANY given topic ID if match_all=False
Topics may be distributed across multiple documents for the same project.
"""
if not topic_ids:
return {project_type: [] for project_type in project_types}
@@ -474,15 +495,18 @@ async def get_eu_project_ids_by_openalex_topic_ids(
for hit in hits:
source = hit.get("_source", {})
project_id = source.get("project_id")
if not project_id:
continue
topic_matches = source.get("topic_matches") or []
if isinstance(topic_matches, dict):
topic_matches = [topic_matches]
for match in topic_matches:
topic_id = match.get("topic_id")
if topic_id in required_topics:
project_topics[project_id].add(topic_id)
@@ -491,11 +515,15 @@ async def get_eu_project_ids_by_openalex_topic_ids(
from_ += page_size
result[project_type] = [
project_id
for project_id, found_topics in project_topics.items()
if required_topics.issubset(found_topics)
]
if match_all:
result[project_type] = [
project_id
for project_id, found_topics in project_topics.items()
if required_topics.issubset(found_topics)
]
else:
# OR match: project has at least one requested topic
result[project_type] = list(project_topics.keys())
return result
@@ -426,9 +426,8 @@ async def search_dfg_projects(
# === Primary search clause: mutually exclusive fuzzy(topic_ids) vs keyword phrase search ===
if fuzzy and topic_ids:
matching_project_ids = await get_dfg_project_ids_by_topic_ids(
topic_ids=topic_ids
topic_ids=topic_ids, match_all = False
)
if not matching_project_ids:
return DfgProjectPage(elements=[], total=0, page=page, size=size)
@@ -493,12 +492,12 @@ async def search_dfg_projects(
}
)
if keyword_clauses:
must_clauses.append({"bool": {"must": keyword_clauses}})
must_clauses.append({"bool": {"should": keyword_clauses, "minimum_should_match": 1}})
# === Additional independent filters (applied regardless of fuzzy/keyword mode) ===
if filter_topic_ids:
matching_filter_project_ids = await get_dfg_project_ids_by_topic_ids(
topic_ids=filter_topic_ids
topic_ids=filter_topic_ids, match_all=True
)
if not matching_filter_project_ids:
@@ -558,7 +557,7 @@ async def search_dfg_projects(
)
if name_must_clauses:
must_clauses.append({"bool": {"must": name_must_clauses}})
must_clauses.append({"bool": {"should": name_must_clauses, "minimum_should_match": 1}})
if institution_aliases:
matching_institution_ids = await get_institution_ids_by_institution_aliases(
@@ -987,7 +986,7 @@ async def search_bund_projects_with_filters(
)
# === Primary search clause: fuzzy(topic_ids) vs keyword phrase search ===
if fuzzy and topic_ids:
matching_project_ids = await get_bund_project_ids_by_topic_ids(topic_ids=topic_ids)
matching_project_ids = await get_bund_project_ids_by_topic_ids(topic_ids=topic_ids, match_all=False)
if not matching_project_ids:
return BundProjectPage(elements=[], total=0, page=page, size=size)
must_clauses.append({"terms": {"_id": matching_project_ids}})
@@ -1010,7 +1009,7 @@ async def search_bund_projects_with_filters(
# === Additional independent filters (applied regardless of fuzzy/keyword mode) ===
if filter_topic_ids:
matching_filter_ids = await get_bund_project_ids_by_topic_ids(topic_ids=filter_topic_ids)
matching_filter_ids = await get_bund_project_ids_by_topic_ids(topic_ids=filter_topic_ids, match_all=True)
if not matching_filter_ids:
return BundProjectPage(elements=[], total=0, page=page, size=size)
must_clauses.append({"terms": {"_id": matching_filter_ids}})
@@ -115,7 +115,7 @@ export class BundProjectTableComponent implements OnInit {
return this.queryParamMap().getAll(this.queryParamKeys.topicId);
});
selectedTopicMatchesQueryParams = computed(() => {
return this.queryParamMap().get('topics')?.split(',') || [];
return this.queryParamMap().getAll('topics');
});
selectedFundingTypesQueryParams = computed(() => {
return this.queryParamMap().getAll(this.queryParamKeys.funding_type);
@@ -112,7 +112,7 @@ export class DfgProjectTableComponent implements OnInit {
return this.queryParamMap().getAll(this.queryParamKeys.topicId);
});
selectedTopicMatchesQueryParams = computed(() => {
return this.queryParamMap().get('topics')?.split(',') || [];
return this.queryParamMap().getAll('topics');
});
keywordTopicMap = computed(() => {
return (
@@ -103,7 +103,7 @@ export class EuProjectTableComponent {
return this.queryParamMap().getAll(this.queryParamKeys.topicId);
});
selectedTopicMatchesQueryParams = computed(() => {
return this.queryParamMap().get('topics')?.split(',') || [];
return this.queryParamMap().getAll('topics');
});
selectedTypesQueryParams = computed(() => {
return this.queryParamMap().getAll(this.queryParamKeys.projectType);