Compare commits

...
6 changed files with 14 additions and 91 deletions
@@ -96,8 +96,7 @@ class Work(BaseModel):
updated_date: datetime | None = Field(default=None, description="When this work was last updated (ISO 8601 datetime).")
snippet: StrictStr | None = Field(default=None, description="Passage of the title or abstract containing the searched keyword, with the matches wrapped in `<em>`. Only set by the keyword search, and only when the keyword actually occurs in the text.")
snippet_field: StrictStr | None = Field(default=None, description="Which field `snippet` was taken from: `title` or `abstract`.")
matched_via: list[StrictStr] | None = Field(default=None, description="Names of the topics and OpenAlex keywords that matched the searched keywords. Set by the keyword search so a work matched purely on metadata can explain itself when there is no `snippet`.")
__properties: ClassVar[list[str]] = ["id", "doi", "title", "display_name", "publication_year", "publication_date", "type", "language", "cited_by_count", "is_retracted", "is_paratext", "primary_location", "locations", "best_oa_location", "open_access", "authorships", "ids", "biblio", "abstract_inverted_index", "referenced_works", "referenced_works_count", "related_works", "topics", "primary_topic", "keywords", "funders", "awards", "fwci", "citation_normalized_percentile", "cited_by_percentile_year", "counts_by_year", "sustainable_development_goals", "mesh", "indexed_in", "has_content", "content_url", "created_date", "updated_date", "snippet", "snippet_field", "matched_via"]
__properties: ClassVar[list[str]] = ["id", "doi", "title", "display_name", "publication_year", "publication_date", "type", "language", "cited_by_count", "is_retracted", "is_paratext", "primary_location", "locations", "best_oa_location", "open_access", "authorships", "ids", "biblio", "abstract_inverted_index", "referenced_works", "referenced_works_count", "related_works", "topics", "primary_topic", "keywords", "funders", "awards", "fwci", "citation_normalized_percentile", "cited_by_percentile_year", "counts_by_year", "sustainable_development_goals", "mesh", "indexed_in", "has_content", "content_url", "created_date", "updated_date", "snippet", "snippet_field"]
model_config = ConfigDict(
validate_by_name=True,
@@ -278,8 +277,7 @@ class Work(BaseModel):
"created_date": obj.get("created_date"),
"updated_date": obj.get("updated_date"),
"snippet": obj.get("snippet"),
"snippet_field": obj.get("snippet_field"),
"matched_via": obj.get("matched_via")
"snippet_field": obj.get("snippet_field")
})
return _obj
+1 -1
View File
@@ -66,7 +66,7 @@ RELATED_BLOCK_SIZE = 5
RELATED_TOPIC_K = 1
EXACT_ID_CAP = 1000
KEYWORD_SEARCH_FIELDS = ["maintitle^3", "intro_content^2", "tags^2", "full_content"]
KEYWORD_SEARCH_FIELDS = ["maintitle^3", "intro_content^2", "full_content"]
KEYWORD_SEARCH_SCAN_CAP = 50000
KEYWORD_SEARCH_ID_CAP = 2000
TOPIC_DOCS_PER_ARTICLE = 25
+11 -66
View File
@@ -1,4 +1,5 @@
import logging
from datetime import date
import inse.authors.v2.service as author_service
from inse.auth.model import AccessTokenMetadata
@@ -295,33 +296,12 @@ async def get_works_by_keywords(
{
"bool": {
"should": [
{
"nested": {
"path": "keywords",
"query": {
"match_phrase": {"keywords.display_name": kw}
},
}
},
{"match_phrase": {"primary_topic.display_name": kw}},
# The literal mode: title and abstract only. Topic
# and keyword metadata is deliberately not matched
# here. Expanding a keyword to related topics is
# done in the fuzzy search.
{"match_phrase": {"title": kw}},
{"match_phrase": {"abstract": kw}},
_topics_nested_clause(
[
{"match_phrase": {"topics.display_name": kw}},
{"match_phrase": {"topics.field.display_name": kw}},
{
"match_phrase": {
"topics.subfield.display_name": kw
}
},
{
"match_phrase": {
"topics.domain.display_name": kw
}
},
]
),
],
"minimum_should_match": 1,
}
@@ -335,6 +315,9 @@ async def get_works_by_keywords(
# filters
must = [topic_clause]
# Filter out publications with future 'published' years (e.g. 2036).
must.append({"range": {"publication_year": {"lte": date.today().year}}})
if title:
must.append(_title_query(title))
if publication_year:
@@ -378,10 +361,9 @@ async def get_works_by_keywords(
track_total_hits=True,
offset=page * size,
sort=sort,
# A work matches on its topic and keyword metadata as well as on its
# text, so roughly half the hits contain the searched keyword nowhere in
# title or abstract. Highlighting tells the two apart: a fragment comes
# back only for the works that really do carry the phrase.
# Supplies the expanded row. Retrieval is title and
# abstract only, so every hit carries the searched term in one of the
# two and a fragment always comes back.
highlight={
"fields": {
"title": WHOLE_FIELD,
@@ -408,7 +390,6 @@ async def get_works_by_keywords(
snippet, snippet_field = _keyword_snippet(hit)
work.snippet = snippet
work.snippet_field = snippet_field
work.matched_via = _matched_via(source, keywords)
elements.append(work)
return WorkPageV2(elements=elements, total=total, page=page, size=size)
@@ -430,42 +411,6 @@ def _keyword_snippet(hit: dict) -> tuple[str | None, str | None]:
return None, None
def _matched_via(source: dict, keywords: list[str] | None) -> list[str] | None:
"""Names the topics and keywords a work was matched through.
Only needed when there is no snippet: those works carry the searched term in
their metadata rather than in their text, and would otherwise expand into an
empty row. Matching mirrors the `match_phrase` clauses above closely enough
for a label - it is not used for retrieval.
"""
wanted = [kw.strip().lower() for kw in (keywords or []) if kw and kw.strip()]
if not wanted:
return None
candidates: list[str] = []
primary_topic = source.get("primary_topic") or {}
candidates.append(primary_topic.get("display_name"))
for topic in source.get("topics") or []:
candidates.append(topic.get("display_name"))
for level in ("field", "subfield", "domain"):
candidates.append((topic.get(level) or {}).get("display_name"))
for keyword in source.get("keywords") or []:
candidates.append(keyword.get("display_name"))
names: list[str] = []
for name in candidates:
if not name or name in names:
continue
lowered = name.lower()
if any(kw in lowered for kw in wanted):
names.append(name)
return names or None
async def get_author_works(
raw_oa_author_id: str,
page: int,
-1
View File
@@ -68,5 +68,4 @@ export interface Work {
updated_date?: string | null;
snippet?: string | null;
snippet_field?: string | null;
matched_via?: Array<string> | null;
}
@@ -20,20 +20,6 @@
[content]="snippet.content"
[highlights]="snippet.terms" />
</article>
} @else if (work.matched_via?.length) {
<article class="panel-field panel-field--full">
<h4 class="panel-field__label">Kein Treffer im Text</h4>
<p class="panel-field__text">
Das gesuchte Keyword kommt weder im Titel noch im Abstract vor.
Die Publikation wurde über
{{ work.matched_via!.length === 1 ? 'das Thema' : 'die Themen' }}
@for (name of work.matched_via; track name; let last = $last) {
<span class="matched-topic">{{ name }}</span
>{{ last ? '' : ', ' }}
}
gefunden.
</p>
</article>
} @else {
<span class="empty-text">
Für diese Publikation liegt keine Fundstelle vor.
@@ -83,11 +83,6 @@
gap: 0.5rem;
}
.matched-topic {
color: var(--sg-text);
font-weight: 600;
}
.empty-text {
color: var(--sg-text-muted);
}