added relevancy rescoring for persons search based on publications, citations, h_index, mean_citations and name quality
This commit is contained in:
@@ -1,11 +1,13 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
from typing import List, Optional, Set, Any
|
||||
|
||||
import json
|
||||
def build_authors_search(
|
||||
using: Any = None,
|
||||
index: Optional[str] = None,
|
||||
query: Optional[str] = None,
|
||||
institution_name: Optional[str] = None,
|
||||
sort_by: Optional[str] = None,
|
||||
sort_order: Optional[str] = None,
|
||||
topics: Optional[List[str]] = None,
|
||||
page: int = 1,
|
||||
page_size: int = 10,
|
||||
@@ -13,7 +15,7 @@ def build_authors_search(
|
||||
strict: Optional[bool] = False,
|
||||
track_total_hits: Optional[bool] = False,
|
||||
filter_mode: Optional[str] = "from",
|
||||
filter_year: Optional[int] = None
|
||||
filter_year: Optional[int] = None,
|
||||
) -> dict:
|
||||
"""
|
||||
Builds an Elasticsearch search request query body for authors as a dictionary.
|
||||
@@ -167,7 +169,7 @@ def build_authors_search(
|
||||
]
|
||||
}
|
||||
})
|
||||
|
||||
|
||||
if topics:
|
||||
for t in topics:
|
||||
if t:
|
||||
@@ -220,11 +222,326 @@ def build_authors_search(
|
||||
body["track_total_hits"] = track_total_hits
|
||||
|
||||
# 5. Sorting
|
||||
if not query:
|
||||
if sort_by and sort_order:
|
||||
body["sort"] = [
|
||||
{"cited_by_count": {"order": "desc"}},
|
||||
{"works_count": {"order": "desc"}}
|
||||
{sort_by: {"order": sort_order}},
|
||||
]
|
||||
return body
|
||||
|
||||
|
||||
def build_authors_search_rescored(
|
||||
using: Any = None,
|
||||
index: Optional[str] = None,
|
||||
query: Optional[str] = None,
|
||||
institution_name: Optional[str] = None,
|
||||
sort_by: Optional[str] = None,
|
||||
sort_order: Optional[str] = None,
|
||||
topics: Optional[List[str]] = None,
|
||||
page: int = 1,
|
||||
page_size: int = 10,
|
||||
include: Optional[Set[str]] = None,
|
||||
strict: Optional[bool] = False,
|
||||
track_total_hits: Optional[bool] = False,
|
||||
filter_mode: Optional[str] = "from",
|
||||
filter_year: Optional[int] = None,
|
||||
name_quality_boost = True,
|
||||
h_index_boost = True,
|
||||
citations_boost =True,
|
||||
works_boost = True,
|
||||
_2yr_mean_citedness_boost = True,
|
||||
) -> dict:
|
||||
"""
|
||||
Builds an Elasticsearch search request query body for authors as a dictionary.
|
||||
"""
|
||||
# 1. Build Query Clauses
|
||||
query_clause = None
|
||||
if query:
|
||||
query_clause = {
|
||||
"bool": {
|
||||
"should": [
|
||||
{
|
||||
"match_phrase": {
|
||||
"display_name": {
|
||||
"query": query,
|
||||
"boost": 5
|
||||
}
|
||||
}
|
||||
},
|
||||
{
|
||||
"match_phrase_prefix": {
|
||||
"display_name": {
|
||||
"query": query,
|
||||
"boost": 3
|
||||
}
|
||||
}
|
||||
},
|
||||
{
|
||||
"match": {
|
||||
"display_name": {
|
||||
"query": query,
|
||||
"fuzziness": 1
|
||||
}
|
||||
}
|
||||
}
|
||||
],
|
||||
"minimum_should_match": 1
|
||||
}
|
||||
}
|
||||
|
||||
# 2. Build Filters
|
||||
filter_clauses = []
|
||||
|
||||
# Filter against false entries (nested ((Works > 2 AND Year > 2020) OR has last_known_institution))
|
||||
filter_clauses.append({
|
||||
"bool": {
|
||||
"should": [
|
||||
{
|
||||
"bool": {
|
||||
"filter": [
|
||||
{
|
||||
"range": { "works_count": { "gt": 2 } }
|
||||
},
|
||||
{
|
||||
"nested": {
|
||||
"path": "counts_by_year",
|
||||
"query": {
|
||||
"range": { "counts_by_year.year": { "gt": 2020 } } # hardcoded for now
|
||||
}
|
||||
}
|
||||
}
|
||||
]
|
||||
}
|
||||
},
|
||||
{
|
||||
"nested": {
|
||||
"path": "last_known_institutions",
|
||||
"query": {
|
||||
"exists": {
|
||||
"field": "last_known_institutions.id"
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
],
|
||||
"minimum_should_match": 1
|
||||
}
|
||||
})
|
||||
# Institution filter with optional year-based filtering
|
||||
if institution_name:
|
||||
if filter_year is not None:
|
||||
target_year = filter_year
|
||||
mode = filter_mode or "from"
|
||||
else:
|
||||
from datetime import datetime
|
||||
current_year = datetime.now().year
|
||||
target_year = current_year - 1
|
||||
mode = "from"
|
||||
|
||||
range_query = {"gte": target_year}
|
||||
if mode == "in":
|
||||
range_query["lte"] = target_year
|
||||
|
||||
if strict:
|
||||
filter_clauses.append({
|
||||
"nested": {
|
||||
"path": "affiliations",
|
||||
"query": {
|
||||
"bool": {
|
||||
"filter": [
|
||||
{
|
||||
"match_phrase": {
|
||||
"affiliations.institution.display_name": institution_name
|
||||
}
|
||||
},
|
||||
{
|
||||
"range": {
|
||||
"affiliations.years": range_query
|
||||
}
|
||||
}
|
||||
]
|
||||
}
|
||||
}
|
||||
}
|
||||
})
|
||||
else:
|
||||
filter_clauses.append({
|
||||
"bool": {
|
||||
"filter": [
|
||||
{
|
||||
"nested": {
|
||||
"path": "last_known_institutions",
|
||||
"query": {
|
||||
"match_phrase": {
|
||||
"last_known_institutions.display_name": institution_name
|
||||
}
|
||||
}
|
||||
}
|
||||
},
|
||||
{
|
||||
"nested": {
|
||||
"path": "affiliations",
|
||||
"query": {
|
||||
"bool": {
|
||||
"filter": [
|
||||
{
|
||||
"match_phrase": {
|
||||
"affiliations.institution.display_name": institution_name
|
||||
}
|
||||
},
|
||||
{
|
||||
"range": {
|
||||
"affiliations.years": range_query
|
||||
}
|
||||
}
|
||||
]
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
]
|
||||
}
|
||||
})
|
||||
|
||||
if topics:
|
||||
for t in topics:
|
||||
if t:
|
||||
filter_clauses.append({
|
||||
"nested": {
|
||||
"path": "topics",
|
||||
"query": {
|
||||
"match_phrase": {
|
||||
"topics.display_name": t
|
||||
}
|
||||
}
|
||||
}
|
||||
})
|
||||
functions = []
|
||||
if not sort_by:
|
||||
|
||||
if (name_quality_boost): # Name quality boost, boost names which are not "Value Value"
|
||||
name_boost_query={
|
||||
"script_score": {
|
||||
"script": {
|
||||
"lang": "painless",
|
||||
"source": "String name = doc['display_name.keyword'].value.toLowerCase(); String[] parts = name.splitOnToken(' ');Set unique = new HashSet();for (String p : parts) { unique.add(p); }return unique.size() > 1 ? 1.0 : 0.3;"
|
||||
}
|
||||
}
|
||||
}
|
||||
functions.append(name_boost_query)
|
||||
if (h_index_boost): # h_index boost
|
||||
h_index_boost_query={
|
||||
"script_score": {
|
||||
"script": {
|
||||
"lang": "painless",
|
||||
"source": "if (!doc.containsKey('summary_stats.h_index') || doc['summary_stats.h_index'].empty) return 1.0;double h = doc['summary_stats.h_index'].value;return 1.0 + Math.log1p(h) / 5.0;"
|
||||
}
|
||||
}
|
||||
}
|
||||
functions.append(h_index_boost_query)
|
||||
if (citations_boost): # cited_by_count boost
|
||||
cited_by_count_boost_query = {
|
||||
"script_score": {
|
||||
"script": {
|
||||
"lang": "painless",
|
||||
"source": "if (!doc.containsKey('cited_by_count') || doc['cited_by_count'].empty) return 1.0;double citations = doc['cited_by_count'].value;return 1.0 + Math.log1p(citations) / 10.0;"
|
||||
}
|
||||
}
|
||||
}
|
||||
functions.append(cited_by_count_boost_query)
|
||||
|
||||
if (_2yr_mean_citedness_boost): # 2yr mean citedness boost
|
||||
mean_citedness_boost_query = {
|
||||
"script_score": {
|
||||
"script": {
|
||||
"lang": "painless",
|
||||
"source": "if (!doc.containsKey('summary_stats.2yr_mean_citedness') || doc['summary_stats.2yr_mean_citedness'].empty) return 1.0;double recent = doc['summary_stats.2yr_mean_citedness'].value;return 1.0 + Math.log1p(recent) / 8.0;"
|
||||
}
|
||||
}
|
||||
}
|
||||
functions.append(mean_citedness_boost_query)
|
||||
|
||||
if (works_boost): # works_count boost
|
||||
works_count_boost_query = {
|
||||
"script_score": {
|
||||
"script": {
|
||||
"lang": "painless",
|
||||
"source": "if (!doc.containsKey('works_count') || doc['works_count'].empty) return 1.0;double works = doc['works_count'].value;return 1.0 + Math.log1p(works) / 8.0;"
|
||||
}
|
||||
}
|
||||
}
|
||||
functions.append(works_count_boost_query)
|
||||
# Combine query and filters
|
||||
body = {}
|
||||
|
||||
if functions:
|
||||
inner_query = None
|
||||
if query_clause:
|
||||
if filter_clauses:
|
||||
inner_query = {
|
||||
"bool": {
|
||||
"must": [query_clause],
|
||||
"filter": filter_clauses
|
||||
}
|
||||
}
|
||||
else:
|
||||
inner_query = query_clause
|
||||
else:
|
||||
if filter_clauses:
|
||||
inner_query = {
|
||||
"bool": {
|
||||
"filter": filter_clauses
|
||||
}
|
||||
}
|
||||
else:
|
||||
inner_query = {"match_all": {}}
|
||||
|
||||
body["query"] = {
|
||||
"function_score": {
|
||||
"query": inner_query,
|
||||
"functions": functions,
|
||||
"score_mode": "multiply",
|
||||
"boost_mode": "replace"
|
||||
}
|
||||
}
|
||||
else:
|
||||
if query_clause:
|
||||
if filter_clauses:
|
||||
body["query"] = {
|
||||
"bool": {
|
||||
"must": [query_clause],
|
||||
"filter": filter_clauses
|
||||
}
|
||||
}
|
||||
else:
|
||||
body["query"] = query_clause
|
||||
else:
|
||||
if filter_clauses:
|
||||
body["query"] = {
|
||||
"bool": {
|
||||
"filter": filter_clauses
|
||||
}
|
||||
}
|
||||
else:
|
||||
body["query"] = {"match_all": {}}
|
||||
|
||||
# 3. Source Filtering
|
||||
if include:
|
||||
body["_source"] = list(include)
|
||||
else:
|
||||
body["_source"] = True
|
||||
|
||||
# 4. Pagination
|
||||
es_size = page_size
|
||||
es_from = (page - 1) * page_size
|
||||
body["from"] = es_from
|
||||
body["size"] = es_size
|
||||
body["track_total_hits"] = track_total_hits
|
||||
|
||||
# 5. Sorting
|
||||
if sort_by and sort_order:
|
||||
body["sort"] = [
|
||||
{sort_by: {"order": sort_order}},
|
||||
]
|
||||
|
||||
return body
|
||||
|
||||
|
||||
Reference in New Issue
Block a user