11import { AggregationArgs } from "../../types/commons" ;
22
3- const getUrl = ( query : string , groupBy : string ) =>
4- `https://api.openalex.org/works?page=1&[email protected] &filter=title_and_abstract.search:${ query } &group_by=${ groupBy } ` ; 3+ import { getOpenalexUrl } from "./config" ;
54
65const groupBys = [
76 "publication_year" ,
87 "type" ,
98 "authorships.countries" ,
109 "authorships.author.id" ,
11- // "authorships.author.id###authorships.countries:countries/fr",
1210 "authorships.institutions.lineage" ,
1311 "primary_topic.id" ,
1412 // "keywords.id",
1513 "primary_location.source.id" ,
1614 "grants.funder" ,
17- "authorships.institutions.continent" ,
15+ // "authorships.institutions.continent",
1816] ;
1917
2018export async function fetchOpenAlexAggregations ( { query, filters = [ ] } : AggregationArgs ) : Promise < any > {
21- console . log ( filters )
19+ console . log ( "fetchOpenAlexAggregations" , query , filters )
2220 const urls = groupBys . map ( ( groupBy ) => {
23- // if (groupBy.split("###").length > 1) {
24- // return getUrl(`${query},${groupBy.split("###")[1]}`, groupBy.split("###")[0]);
25- // }
26- if ( filters ) {
21+ if ( filters . length ) {
2722 /* @ts -expect-error unknown */
28- const [ yearMin , yearMax ] = [ filters ?. [ 0 ] ?. range ?. year ?. gte , filters ?. [ 0 ] ?. range ?. year ?. lte ]
29- if ( ! yearMin || ! yearMax ) return getUrl ( query , groupBy ) ;
30- return getUrl ( `${ query } ,publication_year:${ yearMin } -${ yearMax } ` , groupBy ) ;
23+ const yearRange : any = filters . find ( ( el : any ) => el ?. range ?. year ) ?. range ?. year
24+ const [ yearMin , yearMax ] = [ yearRange ?. gte , yearRange ?. lte ]
25+ const types = filters
26+ ?. find ( ( el ) => el ?. terms ?. [ "type.keyword" ] )
27+ ?. terms [ "type.keyword" ]
28+ ?. map ( ( type : string ) => `types/${ type } ` ) ;
29+
30+ const typesFilter = types ?. length > 0 ? `type:${ types . join ( "|" ) } ` : "" ;
31+ const yearsFilter = ( yearMin && yearMax ) ? `publication_year:${ parseInt ( yearMin ) } -${ parseInt ( yearMax ) } ` : "" ;
32+ const finalQuery = [ query , typesFilter , yearsFilter ] . filter ( Boolean ) . join ( "," )
33+ console . log ( "finalQuery" , finalQuery )
34+ return getOpenalexUrl ( finalQuery , groupBy ) ;
3135 }
32- return getUrl ( query , groupBy ) ;
36+ return getOpenalexUrl ( query , groupBy ) ;
3337 } ) ;
3438
3539 const responses = await Promise . all ( urls . map ( ( url ) => fetch ( url ) ) ) ;
@@ -39,13 +43,12 @@ export async function fetchOpenAlexAggregations({ query, filters = [] }: Aggrega
3943 publicationType ,
4044 authorshipsCountries ,
4145 authorshipsAllAuthors ,
42- // authorshipsFrenchAuthors,
4346 authorshipsAuthorsInstitutions ,
4447 primaryTopic ,
4548 // keywords,
4649 primaryLocationUrl ,
4750 grantsFunder ,
48- authorshipsAuthorsInstitutionsContinents ,
51+ // authorshipsAuthorsInstitutionsContinents,
4952 ] = results . map ( ( result ) => {
5053 const agg = result . group_by ;
5154
@@ -75,6 +78,6 @@ export async function fetchOpenAlexAggregations({ query, filters = [] }: Aggrega
7578 // keywords,
7679 primaryLocationUrl,
7780 grantsFunder,
78- authorshipsAuthorsInstitutionsContinents,
81+ // authorshipsAuthorsInstitutionsContinents,
7982 } ;
8083}
0 commit comments