4
4
import time
5
5
from typing import Annotated , Any
6
6
7
- from fastapi import APIRouter , Depends , HTTPException , Query , Request
7
+ from fastapi import APIRouter , Depends , HTTPException , Query
8
8
from fastapi_pagination import Page , paginate
9
9
from httpx import AsyncClient
10
10
@@ -177,9 +177,23 @@ async def retrieval(
177
177
178
178
@router .get ("/article_count" )
179
179
async def article_count (
180
- request : Request ,
181
180
ds_client : Annotated [AsyncBaseSearch , Depends (get_ds_client )],
181
+ filter_query : Annotated [dict [str , Any ], Depends (get_query_from_params )],
182
182
settings : Annotated [Settings , Depends (get_settings )],
183
+ topics : Annotated [
184
+ list [str ] | None ,
185
+ Query (
186
+ description = "Keyword to be matched in text. AND matching (e.g. for TOPICS)."
187
+ ),
188
+ ] = None ,
189
+ regions : Annotated [
190
+ list [str ] | None ,
191
+ Query (
192
+ description = (
193
+ "Keyword to be matched in text. OR matching (e.g. for BRAIN_REGIONS)."
194
+ )
195
+ ),
196
+ ] = None ,
183
197
) -> ArticleCountResponse :
184
198
"""Article count based on keyword matching.
185
199
\f
@@ -200,28 +214,41 @@ async def article_count(
200
214
start = time .time ()
201
215
logger .info ("Finding unique articles matching the query ..." )
202
216
203
- params = request .query_params
204
- topics = params .getlist ("topics" )
205
- regions = params .getlist ("regions" )
206
-
207
- if topics or regions :
217
+ if not topics and not regions :
218
+ raise HTTPException (
219
+ status_code = 422 , detail = "Please provide at least one region or topic."
220
+ )
221
+ else :
222
+ # Match the keywords on abstract + title.
223
+ keywords = ([topic .split (" " ) for topic in topics ] or []) + (
224
+ [region .split (" " ) for region in regions ] or []
225
+ )
208
226
query = {
209
227
"query" : {
210
228
"bool" : {
211
229
"must" : [
212
230
{
213
231
"multi_match" : {
214
- "query" : " " . join ( topics ) + " " + " " . join ( regions ) ,
232
+ "query" : wo ,
215
233
"fields" : ["title" , "text" ],
216
234
}
217
- },
218
- {"term" : {"section" : "Abstract" }},
235
+ }
236
+ # {
237
+ # "multi_match": {
238
+ # "query": " ".join(regions),
239
+ # "fields": ["title", "text"],
240
+ # }
241
+ # },
242
+ for words in keywords
243
+ for wo in words
219
244
]
220
245
}
221
246
}
222
247
}
223
- else :
224
- query = {"query" : {"match_all" : {}}}
248
+ query ["query" ]["bool" ]["must" ].append ({"term" : {"section" : "Abstract" }})
249
+ # If further filters, append them
250
+ if filter_query :
251
+ query ["query" ]["bool" ]["must" ].append (filter_query ["bool" ]["must" ])
225
252
226
253
# Aggregation query.
227
254
aggs = {
@@ -268,11 +295,24 @@ async def article_count(
268
295
},
269
296
)
270
297
async def article_listing (
271
- request : Request ,
272
298
ds_client : Annotated [AsyncBaseSearch , Depends (get_ds_client )],
273
299
httpx_client : Annotated [AsyncClient , Depends (get_httpx_client )],
274
300
filter_query : Annotated [dict [str , Any ], Depends (get_query_from_params )],
275
301
settings : Annotated [Settings , Depends (get_settings )],
302
+ topics : Annotated [
303
+ list [str ] | None ,
304
+ Query (
305
+ description = "Keyword to be matched in text. AND matching (e.g. for TOPICS)."
306
+ ),
307
+ ] = None ,
308
+ regions : Annotated [
309
+ list [str ] | None ,
310
+ Query (
311
+ description = (
312
+ "Keyword to be matched in text. OR matching (e.g. for BRAIN_REGIONS)."
313
+ )
314
+ ),
315
+ ] = None ,
276
316
number_results : Annotated [
277
317
int | None ,
278
318
Query (description = "Number of results to return. Max 10 000." , ge = 1 , le = 10_000 ),
@@ -313,28 +353,35 @@ async def article_listing(
313
353
start = time .time ()
314
354
logger .info ("Finding unique articles matching the query ..." )
315
355
316
- params = request .query_params
317
- topics = params .getlist ("topics" )
318
- regions = params .getlist ("regions" )
319
-
320
- if topics or regions :
356
+ if not topics and not regions :
357
+ raise HTTPException (
358
+ status_code = 422 , detail = "Please provide at least one region or topic."
359
+ )
360
+ else :
361
+ keywords = ([topic .split (" " ) for topic in topics ] or []) + (
362
+ [region .split (" " ) for region in regions ] or []
363
+ )
321
364
query = {
322
365
"query" : {
323
366
"bool" : {
324
367
"must" : [
325
368
{
326
369
"multi_match" : {
327
- "query" : " " . join ( topics ) + " " + " " . join ( regions ) ,
370
+ "query" : wo ,
328
371
"fields" : ["title" , "text" ],
329
372
}
330
- },
331
- {"term" : {"section" : "Abstract" }},
373
+ }
374
+ for words in keywords
375
+ for wo in words
332
376
]
333
377
}
334
378
}
335
379
}
336
- else :
337
- query = {"query" : {"match_all" : {}}}
380
+ query ["query" ]["bool" ]["must" ].append ({"term" : {"section" : "Abstract" }})
381
+
382
+ # If further filters, append them
383
+ if filter_query :
384
+ query ["query" ]["bool" ]["must" ].append (filter_query ["bool" ]["must" ])
338
385
339
386
aggs : dict [str , Any ] = {
340
387
"relevant_ids" : {
0 commit comments