diff --git a/service/src/main/inse/persons/logic.py b/service/src/main/inse/persons/logic.py index a4dc5c0f..199548fa 100644 --- a/service/src/main/inse/persons/logic.py +++ b/service/src/main/inse/persons/logic.py @@ -19,7 +19,7 @@ from inse.models.authors_list_response import AuthorsListResponse from inse import util from inse.integrations import elasticsearch from inse.models.meta import Meta -from inse.persons.query_builder import build_authors_search +from inse.persons.query_builder import build_authors_search, build_authors_search_rescored from inse.persons.model import CustomAuthor, CustomAuthorsListResponse, PersonSearchRequest logger = logging.getLogger(__name__) INDEX_NAME="openalex-authors" @@ -122,30 +122,6 @@ def get_person_by_id(author_id: str, include: Optional[str] = None) -> AuthorsLi return AuthorsListResponse(results=persons) - -def get_person_by_id(author_id: str, include: Optional[str] = None) -> AuthorsListResponse: - body = { - "_source":include.split(",") if include else True, - } - - try: - hit = elasticsearch.get( - index="openalex-authors", - body=body, - doc_id=author_id - ) - except NotFoundError: - raise HTTPException( - status_code=404, detail=f"Person with id {author_id} not found" - ) - - except ApiError as e: - raise HTTPException(status_code=500, detail=f"Elasticsearch error: {str(e)}") - persons = [util.get_persons_data(hit)] - - return AuthorsListResponse(results=persons) - - def search_persons_by_name( author_name: str, limit: Optional[int] = 10, page_offset: Optional[int] = 0, include: Optional[str] = None ) -> AuthorsListResponse: @@ -358,6 +334,8 @@ def _enrich_author_data(author_data) -> Optional[CustomAuthor]: def advanced_search_persons( 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, @@ -367,9 +345,11 @@ def advanced_search_persons( filter_year: Optional[int] = None, ) -> CustomAuthorsListResponse: try: - query_body = build_authors_search( + query_body = build_authors_search_rescored( query=query, institution_name=institution_name, + sort_by=sort_by, + sort_order=sort_order, topics=topics, page=page, page_size=page_size, diff --git a/service/src/main/inse/persons/service.py b/service/src/main/inse/persons/service.py index c442b578..5f04bd02 100644 --- a/service/src/main/inse/persons/service.py +++ b/service/src/main/inse/persons/service.py @@ -190,6 +190,12 @@ def search_persons_list( institution_name: Annotated[ Optional[str], Query(alias="institutionName", description="Filter by institution name") ] = None, + sort_by: Annotated[ + Optional[str], Query(alias="sortBy", description="Value to sort by") + ] = None, + sort_order: Annotated[ + Optional[str], Query(alias="sortOrder", description="asc or desc?") + ] = None, topic: Annotated[ Optional[List[str]], Query(description="Filter by topic display names") ] = None, @@ -224,6 +230,8 @@ def search_persons_list( return logic.advanced_search_persons( query=query, institution_name=institution_name, + sort_by=sort_by, + sort_order=sort_order, topics=topic, page=page, page_size=page_size, diff --git a/webapp/src/app/api/api/persons.service.ts b/webapp/src/app/api/api/persons.service.ts index b93da20a..7efacfa9 100644 --- a/webapp/src/app/api/api/persons.service.ts +++ b/webapp/src/app/api/api/persons.service.ts @@ -723,6 +723,8 @@ export class PersonsService extends BaseService implements PersonsServiceInterfa * @endpoint get /api/inse-service/persons/search-list * @param query Search query (e.g., Arman AND Arzani) * @param institutionName Filter by institution name + * @param sortBy Value to sort by + * @param sortOrder asc or desc? * @param topic Filter by topic display names * @param page Page number (1-indexed) * @param pageSize Results per page @@ -733,16 +735,20 @@ export class PersonsService extends BaseService implements PersonsServiceInterfa * @param observe set whether or not to return the data Observable as the body, response or events. defaults to returning the body. * @param reportProgress flag to report request and response progress. */ - public searchPersonsList(query?: string, institutionName?: string, topic?: Array, page?: number, pageSize?: number, include?: string, strict?: boolean, filterMode?: string, filterYear?: number, observe?: 'body', reportProgress?: boolean, options?: {httpHeaderAccept?: 'application/json', context?: HttpContext, transferCache?: boolean}): Observable; - public searchPersonsList(query?: string, institutionName?: string, topic?: Array, page?: number, pageSize?: number, include?: string, strict?: boolean, filterMode?: string, filterYear?: number, observe?: 'response', reportProgress?: boolean, options?: {httpHeaderAccept?: 'application/json', context?: HttpContext, transferCache?: boolean}): Observable>; - public searchPersonsList(query?: string, institutionName?: string, topic?: Array, page?: number, pageSize?: number, include?: string, strict?: boolean, filterMode?: string, filterYear?: number, observe?: 'events', reportProgress?: boolean, options?: {httpHeaderAccept?: 'application/json', context?: HttpContext, transferCache?: boolean}): Observable>; - public searchPersonsList(query?: string, institutionName?: string, topic?: Array, page?: number, pageSize?: number, include?: string, strict?: boolean, filterMode?: string, filterYear?: number, observe: any = 'body', reportProgress: boolean = false, options?: {httpHeaderAccept?: 'application/json', context?: HttpContext, transferCache?: boolean}): Observable { + public searchPersonsList(query?: string, institutionName?: string, sortBy?: string, sortOrder?: string, topic?: Array, page?: number, pageSize?: number, include?: string, strict?: boolean, filterMode?: string, filterYear?: number, observe?: 'body', reportProgress?: boolean, options?: {httpHeaderAccept?: 'application/json', context?: HttpContext, transferCache?: boolean}): Observable; + public searchPersonsList(query?: string, institutionName?: string, sortBy?: string, sortOrder?: string, topic?: Array, page?: number, pageSize?: number, include?: string, strict?: boolean, filterMode?: string, filterYear?: number, observe?: 'response', reportProgress?: boolean, options?: {httpHeaderAccept?: 'application/json', context?: HttpContext, transferCache?: boolean}): Observable>; + public searchPersonsList(query?: string, institutionName?: string, sortBy?: string, sortOrder?: string, topic?: Array, page?: number, pageSize?: number, include?: string, strict?: boolean, filterMode?: string, filterYear?: number, observe?: 'events', reportProgress?: boolean, options?: {httpHeaderAccept?: 'application/json', context?: HttpContext, transferCache?: boolean}): Observable>; + public searchPersonsList(query?: string, institutionName?: string, sortBy?: string, sortOrder?: string, topic?: Array, page?: number, pageSize?: number, include?: string, strict?: boolean, filterMode?: string, filterYear?: number, observe: any = 'body', reportProgress: boolean = false, options?: {httpHeaderAccept?: 'application/json', context?: HttpContext, transferCache?: boolean}): Observable { let localVarQueryParameters = new HttpParams({encoder: this.encoder}); localVarQueryParameters = this.addToHttpParams(localVarQueryParameters, query, 'query'); localVarQueryParameters = this.addToHttpParams(localVarQueryParameters, institutionName, 'institutionName'); + localVarQueryParameters = this.addToHttpParams(localVarQueryParameters, + sortBy, 'sortBy'); + localVarQueryParameters = this.addToHttpParams(localVarQueryParameters, + sortOrder, 'sortOrder'); if (topic) { topic.forEach((element) => { localVarQueryParameters = this.addToHttpParams(localVarQueryParameters, @@ -808,6 +814,8 @@ export class PersonsService extends BaseService implements PersonsServiceInterfa * @endpoint get /api/inse-service/persons/search-list * @param query Search query (e.g., Arman AND Arzani) * @param institutionName Filter by institution name + * @param sortBy Value to sort by + * @param sortOrder asc or desc? * @param topic Filter by topic display names * @param page Page number (1-indexed) * @param pageSize Results per page @@ -818,16 +826,20 @@ export class PersonsService extends BaseService implements PersonsServiceInterfa * @param observe set whether or not to return the data Observable as the body, response or events. defaults to returning the body. * @param reportProgress flag to report request and response progress. */ - public searchPersonsList_5(query?: string, institutionName?: string, topic?: Array, page?: number, pageSize?: number, include?: string, strict?: boolean, filterMode?: string, filterYear?: number, observe?: 'body', reportProgress?: boolean, options?: {httpHeaderAccept?: 'application/json', context?: HttpContext, transferCache?: boolean}): Observable; - public searchPersonsList_5(query?: string, institutionName?: string, topic?: Array, page?: number, pageSize?: number, include?: string, strict?: boolean, filterMode?: string, filterYear?: number, observe?: 'response', reportProgress?: boolean, options?: {httpHeaderAccept?: 'application/json', context?: HttpContext, transferCache?: boolean}): Observable>; - public searchPersonsList_5(query?: string, institutionName?: string, topic?: Array, page?: number, pageSize?: number, include?: string, strict?: boolean, filterMode?: string, filterYear?: number, observe?: 'events', reportProgress?: boolean, options?: {httpHeaderAccept?: 'application/json', context?: HttpContext, transferCache?: boolean}): Observable>; - public searchPersonsList_5(query?: string, institutionName?: string, topic?: Array, page?: number, pageSize?: number, include?: string, strict?: boolean, filterMode?: string, filterYear?: number, observe: any = 'body', reportProgress: boolean = false, options?: {httpHeaderAccept?: 'application/json', context?: HttpContext, transferCache?: boolean}): Observable { + public searchPersonsList_5(query?: string, institutionName?: string, sortBy?: string, sortOrder?: string, topic?: Array, page?: number, pageSize?: number, include?: string, strict?: boolean, filterMode?: string, filterYear?: number, observe?: 'body', reportProgress?: boolean, options?: {httpHeaderAccept?: 'application/json', context?: HttpContext, transferCache?: boolean}): Observable; + public searchPersonsList_5(query?: string, institutionName?: string, sortBy?: string, sortOrder?: string, topic?: Array, page?: number, pageSize?: number, include?: string, strict?: boolean, filterMode?: string, filterYear?: number, observe?: 'response', reportProgress?: boolean, options?: {httpHeaderAccept?: 'application/json', context?: HttpContext, transferCache?: boolean}): Observable>; + public searchPersonsList_5(query?: string, institutionName?: string, sortBy?: string, sortOrder?: string, topic?: Array, page?: number, pageSize?: number, include?: string, strict?: boolean, filterMode?: string, filterYear?: number, observe?: 'events', reportProgress?: boolean, options?: {httpHeaderAccept?: 'application/json', context?: HttpContext, transferCache?: boolean}): Observable>; + public searchPersonsList_5(query?: string, institutionName?: string, sortBy?: string, sortOrder?: string, topic?: Array, page?: number, pageSize?: number, include?: string, strict?: boolean, filterMode?: string, filterYear?: number, observe: any = 'body', reportProgress: boolean = false, options?: {httpHeaderAccept?: 'application/json', context?: HttpContext, transferCache?: boolean}): Observable { let localVarQueryParameters = new HttpParams({encoder: this.encoder}); localVarQueryParameters = this.addToHttpParams(localVarQueryParameters, query, 'query'); localVarQueryParameters = this.addToHttpParams(localVarQueryParameters, institutionName, 'institutionName'); + localVarQueryParameters = this.addToHttpParams(localVarQueryParameters, + sortBy, 'sortBy'); + localVarQueryParameters = this.addToHttpParams(localVarQueryParameters, + sortOrder, 'sortOrder'); if (topic) { topic.forEach((element) => { localVarQueryParameters = this.addToHttpParams(localVarQueryParameters, diff --git a/webapp/src/app/api/api/persons.serviceInterface.ts b/webapp/src/app/api/api/persons.serviceInterface.ts index 0c634b4d..2b68be3a 100644 --- a/webapp/src/app/api/api/persons.serviceInterface.ts +++ b/webapp/src/app/api/api/persons.serviceInterface.ts @@ -130,6 +130,8 @@ export interface PersonsServiceInterface { * @endpoint get /api/inse-service/persons/search-list * @param query Search query (e.g., Arman AND Arzani) * @param institutionName Filter by institution name + * @param sortBy Value to sort by + * @param sortOrder asc or desc? * @param topic Filter by topic display names * @param page Page number (1-indexed) * @param pageSize Results per page @@ -138,7 +140,7 @@ export interface PersonsServiceInterface { * @param filterMode Filter mode: \'in\' or \'from\' * @param filterYear Filter year */ - searchPersonsList(query?: string, institutionName?: string, topic?: Array, page?: number, pageSize?: number, include?: string, strict?: boolean, filterMode?: string, filterYear?: number, extraHttpRequestParams?: any): Observable; + searchPersonsList(query?: string, institutionName?: string, sortBy?: string, sortOrder?: string, topic?: Array, page?: number, pageSize?: number, include?: string, strict?: boolean, filterMode?: string, filterYear?: number, extraHttpRequestParams?: any): Observable; /** * Searches for persons with advanced filters (Topic & Institution) and pagination. @@ -146,6 +148,8 @@ export interface PersonsServiceInterface { * @endpoint get /api/inse-service/persons/search-list * @param query Search query (e.g., Arman AND Arzani) * @param institutionName Filter by institution name + * @param sortBy Value to sort by + * @param sortOrder asc or desc? * @param topic Filter by topic display names * @param page Page number (1-indexed) * @param pageSize Results per page @@ -154,6 +158,6 @@ export interface PersonsServiceInterface { * @param filterMode Filter mode: \'in\' or \'from\' * @param filterYear Filter year */ - searchPersonsList_5(query?: string, institutionName?: string, topic?: Array, page?: number, pageSize?: number, include?: string, strict?: boolean, filterMode?: string, filterYear?: number, extraHttpRequestParams?: any): Observable; + searchPersonsList_5(query?: string, institutionName?: string, sortBy?: string, sortOrder?: string, topic?: Array, page?: number, pageSize?: number, include?: string, strict?: boolean, filterMode?: string, filterYear?: number, extraHttpRequestParams?: any): Observable; } diff --git a/webapp/src/app/app.routes.ts b/webapp/src/app/app.routes.ts index 19a5df19..f3912e15 100644 --- a/webapp/src/app/app.routes.ts +++ b/webapp/src/app/app.routes.ts @@ -35,6 +35,8 @@ export const routes: Routes = [ 'page', 'pageSize', 'institutionName', + 'sortBy', + 'sortOrder', 'yearFilterEnabled', 'filterMode', 'filterYear', diff --git a/webapp/src/app/modules/person-list/person-list.component.html b/webapp/src/app/modules/person-list/person-list.component.html index 789862a8..e34d6123 100644 --- a/webapp/src/app/modules/person-list/person-list.component.html +++ b/webapp/src/app/modules/person-list/person-list.component.html @@ -50,10 +50,15 @@
Sortierung:
- + + + + + +
@@ -66,9 +71,9 @@ Sortiert nach Publikationen und Zitierungen--> - @if (sortedPersons.length) { + @if (getPersons().length) {
- @for (person of sortedPersons; track person.id) { + @for (person of getPersons(); track person.id) { diff --git a/webapp/src/app/modules/person-list/person-list.component.ts b/webapp/src/app/modules/person-list/person-list.component.ts index b1afc0f0..1cee49fc 100644 --- a/webapp/src/app/modules/person-list/person-list.component.ts +++ b/webapp/src/app/modules/person-list/person-list.component.ts @@ -12,14 +12,30 @@ import { MatButtonModule } from '@angular/material/button'; import { MatCheckboxModule } from '@angular/material/checkbox'; import { MatRadioModule } from '@angular/material/radio'; -import { CustomAuthor, DehydratedInstitution, PersonsService, InstitutionsService } from '../../api'; +import { + CustomAuthor, + DehydratedInstitution, + PersonsService, + InstitutionsService, +} from '../../api'; import { TopicsService } from '../../api/api/topics.service'; import { TopicHierarchyComponent } from '../topic-hierarchy/topic-hierarchy.component'; import { PersonCardComponent } from '../person-card/person-card.component'; import { SharedStateService } from '../../shared/services/shared-state.service'; import { PaginationService } from '../../shared/services/pagination.service'; -import { finalize, debounceTime, distinctUntilChanged, switchMap, map, of, catchError } from 'rxjs'; -import { MatAutocompleteModule, MatAutocompleteSelectedEvent } from '@angular/material/autocomplete'; +import { + finalize, + debounceTime, + distinctUntilChanged, + switchMap, + map, + of, + catchError, +} from 'rxjs'; +import { + MatAutocompleteModule, + MatAutocompleteSelectedEvent, +} from '@angular/material/autocomplete'; import { MatSnackBar } from '@angular/material/snack-bar'; import { SkTextDirective } from '../../custom-directives/sk-text.directive'; import { @@ -71,6 +87,8 @@ export class PersonListComponent { readonly institutionOptions = signal([]); readonly institutionLookupLoading = signal(false); + defaultOrderValue = 'desc'; + // Selected filter states selectedTopics = new Set(); searchValue = signal(null); @@ -82,7 +100,13 @@ export class PersonListComponent { appliedFilterYear = new Date().getFullYear() - 1; // Forms - sortControl = new FormControl('works', { nonNullable: true }); + sortValueControl = new FormControl(null, { + nonNullable: true, + }); + sortOrderControl = new FormControl( + { value: this.defaultOrderValue, disabled: this.isRelevanceSelected() }, + { nonNullable: true }, + ); topicSearchControl = new FormControl('', { nonNullable: true }); institutionControl = new FormControl('', { nonNullable: true }); yearFilterEnabledControl = new FormControl(false, { nonNullable: true }); @@ -138,6 +162,8 @@ export class PersonListComponent { ? Number.parseInt(params['pageSize'], 10) : 10; const institutionName = params['institutionName'] || ''; + const sortBy = params['sortBy'] || null + const sortOrder = params['sortOrder'] || this.defaultOrderValue; const yearFilterEnabled = params['yearFilterEnabled'] === 'true'; const filterMode = params['filterMode'] === 'in' || params['filterMode'] === 'from' @@ -149,16 +175,21 @@ export class PersonListComponent { const topics = this.parseQueryParamArray(params['topic']); + // restore values from query params this.selectedTopics = new Set(topics); this.currentPage = page; this.paginationService.selection = pageSize; + this.sortValueControl.setValue(sortBy); + this.sortOrderControl.setValue(sortOrder); // Sync search box & institution input if (this.searchValue() !== query) { this.searchValue.set(query || null); } if (this.institutionControl.value !== institutionName) { - this.institutionControl.setValue(institutionName, { emitEvent: false }); + this.institutionControl.setValue(institutionName, { + emitEvent: false, + }); this.updateYearFilterState(institutionName); } if (this.yearFilterEnabledControl.value !== yearFilterEnabled) { @@ -186,20 +217,24 @@ export class PersonListComponent { this.total = persons.meta?.count; this.loadingState.set(false); }); + // listen for sorting selectors changes + this.sortValueControl.valueChanges.subscribe((value) => { + if (!value) { + this.sortOrderControl.setValue(this.defaultOrderValue) + this.sortOrderControl.disable(); + } else { + this.sortOrderControl.enable(); + } + this.applySidebarFilters(); + }); + this.sortOrderControl.valueChanges.subscribe(() => { + this.applySidebarFilters(); + }); } // Getters for filtered results - get sortedPersons(): CustomAuthor[] { - const sorted = [...this.persons]; - const val = this.sortControl.value; - if (val === 'works') { - return sorted.sort((a, b) => (b.works_count ?? 0) - (a.works_count ?? 0)); - } else if (val === 'citations') { - return sorted.sort( - (a, b) => (b.cited_by_count ?? 0) - (a.cited_by_count ?? 0), - ); - } - return sorted; + getPersons(): CustomAuthor[] { + return this.persons; } get loading(): boolean { @@ -211,6 +246,10 @@ export class PersonListComponent { return this.selectedTopics.has(topic); } + isRelevanceSelected(): boolean { + return !this.sortValueControl.value; + } + toggleTopic(topic: string): void { if (this.selectedTopics.has(topic)) { this.selectedTopics.delete(topic); @@ -280,6 +319,8 @@ export class PersonListComponent { ? Array.from(this.selectedTopics) : null, institutionName: this.institutionControl.value || null, + sortBy: this.sortValueControl.value || null, + sortOrder: this.sortOrderControl.value || null, yearFilterEnabled: this.institutionControl.value && this.yearFilterEnabledControl.value ? 'true' @@ -317,8 +358,6 @@ export class PersonListComponent { return `${start}-${end} von ${this.total} Personen`; } - - // Bookmarks toggleBookmark(person: CustomAuthor): void { if (!person.id) return; @@ -353,12 +392,10 @@ export class PersonListComponent { this.searchValue.set(null); this.router.navigate([], { queryParams: { query: null, page: 1 }, - queryParamsHandling:"merge", + queryParamsHandling: 'merge', }); } - - private setupInstitutionAutocomplete(): void { this.institutionControl.valueChanges .pipe( @@ -386,7 +423,8 @@ export class PersonListComponent { } private searchInstitutions(query: string) { - return this.institutionsService.autocompleteInstitutions(query, undefined, undefined, 10) + return this.institutionsService + .autocompleteInstitutions(query, undefined, undefined, 10) .pipe(map((response) => response.results ?? [])); } diff --git a/webapp/src/app/resolvers/person-list.resolver.ts b/webapp/src/app/resolvers/person-list.resolver.ts index a5503d67..dcf8994e 100644 --- a/webapp/src/app/resolvers/person-list.resolver.ts +++ b/webapp/src/app/resolvers/person-list.resolver.ts @@ -26,6 +26,8 @@ export const personListResolver: ResolveFn = ( : 10; const institutionName = params.get('institutionName') || undefined; + const sortBy= params.get('sortBy') || undefined; + const sortOrder= params.get('sortOrder') || undefined; const topicsParam = params.getAll('topic'); @@ -47,6 +49,8 @@ export const personListResolver: ResolveFn = ( return personsService.searchPersonsList( query, institutionName, + sortBy, + sortOrder, topics, page, pageSize,