fixed projects top 20 bug

This commit is contained in:
Kazgatari
2026-07-23 20:00:32 +02:00
parent db2fb18e89
commit f5f4fb7799
@@ -992,18 +992,34 @@ async def get_top_topics_for_entity(
from inse.patents.search_client import PatentSearchClient
patent_client = PatentSearchClient()
short_id = entity_id.split("/")[-1]
full_id = f"https://openalex.org/{short_id}"
aliases = [short_id, entity_id]
aliases = [short_id, entity_id, full_id]
if entity_type == "institution":
try:
from inse.institutions.institutions_search_client import InstitutionsSearchClient
client = InstitutionsSearchClient()
resolved_aliases = await client.get_institution_aliases_by_oa_institution_id(entity_id)
resolved_aliases = await client.get_institution_aliases_by_oa_institution_id(short_id)
if resolved_aliases:
aliases = resolved_aliases
aliases.extend(resolved_aliases)
inst_res = await elasticsearch.search(
index=str(ESIndex.OPENALEX_INSTITUTIONS),
query={"terms": {"id": [full_id, short_id]}},
size=1,
source=["display_name"],
cluster="nes"
)
hits = inst_res.get("hits", {}).get("hits", [])
if hits:
d_name = hits[0]["_source"].get("display_name")
if d_name:
aliases.append(d_name)
except Exception as e:
logger.warning("Failed to resolve institution aliases in get_top_topics_for_entity: %s", e)
aliases = list(set([a for a in aliases if a]))
include_german = True
if entity_type == "country" and entity_id.strip().upper() != "DE":
include_german = False
@@ -1100,22 +1116,28 @@ async def get_top_topics_for_entity(
for i in range(0, len(p_ids), 1000):
chunk = p_ids[i:i+1000]
payload = {
"query": {"terms": {"project_id": chunk}},
"query": {
"bool": {
"should": [
{"terms": {"project_id": chunk}},
{"terms": {"project_id.keyword": chunk}}
],
"minimum_should_match": 1
}
},
"size": 0,
"aggs": {
"topic_matches": {
"nested": {"path": "topic_matches"},
"aggs": {
"topics": {
"terms": {"field": "topic_matches.topic_display_name", "size": 100}
}
"topics": {
"terms": {
"field": "topic_matches.topic_display_name.keyword",
"size": 100
}
}
}
}
try:
res = await patent_client._search(index=topic_idx, payload=payload)
buckets = res.get("aggregations", {}).get("topic_matches", {}).get("topics", {}).get("buckets", [])
buckets = res.get("aggregations", {}).get("topics", {}).get("buckets", [])
for b in buckets:
t_name = b["key"]
cnt = b["doc_count"]