|
4 | 4 | import logging
|
5 | 5 | from enum import Enum
|
6 | 6 | from functools import cache
|
7 |
| -from typing import Annotated, AsyncIterator |
| 7 | +from typing import Annotated, Any, AsyncIterator |
8 | 8 |
|
9 |
| -from elasticsearch_dsl import Q, Search |
10 | 9 | from fastapi import Depends, HTTPException, Query, Request
|
11 | 10 | from fastapi.security import HTTPBearer
|
12 | 11 | from httpx import AsyncClient, HTTPStatusError
|
@@ -202,24 +201,22 @@ def get_query_from_params(
|
202 | 201 | pattern=r"^\d{4}-(0[1-9]|1[0-2])-(0[1-9]|[12]\d|3[01])$",
|
203 | 202 | ),
|
204 | 203 | ] = None,
|
205 |
| -) -> dict[str, str] | None: |
| 204 | +) -> dict[str, dict[str, list[dict[str, Any]]]] | None: |
206 | 205 | """Get the query parameters and generate an ES query for filtering."""
|
207 |
| - search = Search() |
| 206 | + query: dict[str, dict[str, list[dict[str, Any]]]] = {"bool": {"must": []}} |
208 | 207 | if article_types:
|
209 |
| - search = search.query(Q("terms", article_type=article_types)) |
| 208 | + query["bool"]["must"].append({"terms": {"article_type": article_types}}) |
210 | 209 | if authors:
|
211 |
| - search = search.query(Q("terms", authors=authors)) |
| 210 | + query["bool"]["must"].append({"terms": {"authors.keyword": authors}}) |
212 | 211 | if journals:
|
213 |
| - search = search.query(Q("terms", journal=journals)) |
| 212 | + query["bool"]["must"].append({"terms": {"journal": journals}}) |
214 | 213 | if date_from:
|
215 |
| - search = search.query(Q("range", date={"gte": date_from})) |
| 214 | + query["bool"]["must"].append({"range": {"date": {"gte": date_from}}}) |
216 | 215 | if date_to:
|
217 |
| - search = search.query(Q("range", date={"lte": date_to})) |
| 216 | + query["bool"]["must"].append({"range": {"date": {"lte": date_to}}}) |
218 | 217 |
|
219 |
| - logger.info( |
220 |
| - f"Searching the database with the query {json.dumps(search.to_dict())}." |
221 |
| - ) |
222 |
| - return None if not search.to_dict() else search.to_dict()["query"] |
| 218 | + logger.info(f"Searching the database with the query {json.dumps(query)}.") |
| 219 | + return None if not query["bool"]["must"] else query |
223 | 220 |
|
224 | 221 |
|
225 | 222 | class ErrorCode(Enum):
|
|
0 commit comments