improve es query: only rank target authors in works

This commit is contained in:
Steven Tischer
2026-07-09 09:25:27 +02:00
parent 62d3adb745
commit 16e582e942
2 changed files with 44 additions and 29 deletions
+34 -28
View File
@@ -16,7 +16,7 @@ logger = logging.getLogger(__name__)
# computed, so a strong citation weight can still lift a low-works author.
CANDIDATE_POOL_SIZE = 10000
# Number of authors to return in the final result
RESULT_SIZE = 20
RESULT_SIZE = 100
def _keyword_clause(keyword: str) -> dict:
@@ -96,39 +96,44 @@ def _build_query(
"authors": {
"nested": {"path": "authorships"},
"aggs": {
"by_author": {
"terms": {
"field": "authorships.author.id",
"size": CANDIDATE_POOL_SIZE,
},
"uni_only": {
"filter": _institution_filter(uni_id),
"aggs": {
"author_name": {
"by_author": {
"terms": {
"field": "authorships.author.display_name.keyword",
"size": 1,
"field": "authorships.author.id",
"size": CANDIDATE_POOL_SIZE,
},
},
"to_work": {
"reverse_nested": {},
"aggs": {
"citations": {
"sum": {"field": "cited_by_count"},
"author_name": {
"terms": {
"field": "authorships.author.display_name.keyword",
"size": 1,
},
},
},
},
"score": {
"bucket_script": {
"buckets_path": {
"works": "_count",
"citations": "to_work>citations",
"to_work": {
"reverse_nested": {},
"aggs": {
"citations": {
"sum": {"field": "cited_by_count"},
},
},
},
"score": {
"bucket_script": {
"buckets_path": {
"works": "_count",
"citations": "to_work>citations",
},
"script": score_script,
},
},
"score_sort": {
"bucket_sort": {
"sort": [{"score": {"order": "desc"}}],
"size": RESULT_SIZE,
},
},
"script": score_script,
},
},
"score_sort": {
"bucket_sort": {
"sort": [{"score": {"order": "desc"}}],
"size": RESULT_SIZE,
},
},
},
@@ -190,6 +195,7 @@ async def rank_authors_by_keywords(
author_buckets = (
response.get("aggregations", {})
.get("authors", {})
.get("uni_only", {})
.get("by_author", {})
.get("buckets", [])
)
+10 -1
View File
@@ -102,5 +102,14 @@ Content-Type: application/json
{
//"author_id": "https://openalex.org/A5036533333"
"keywords": ["Keyword 1", "Keyword 2"]
"keywords": ["Computer science"]
}
###
POST http://localhost:8000/api/rank/rank-author
Content-Type: application/json
{
"keywords": ["Computer science"],
"weights": { "citation": 0.6, "works": 0.3 }
}