-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathsearch_api_impl.py
More file actions
252 lines (239 loc) · 8.7 KB
/
search_api_impl.py
File metadata and controls
252 lines (239 loc) · 8.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
from typing import List
from sqlalchemy import func, select
from sqlalchemy.orm import Query, Session
from sqlalchemy.dialects.postgresql import array
from shared.database.database import Database, with_db_session
from shared.database.sql_functions.unaccent import unaccent
from shared.database_gen.sqlacodegen_models import t_feedsearch
from shared.db_models.search_feed_item_result_impl import SearchFeedItemResultImpl
from feeds_gen.apis.search_api_base import BaseSearchApi
from feeds_gen.models.search_feeds200_response import SearchFeeds200Response
from middleware.request_context import is_user_email_restricted
from sqlalchemy import or_
feed_search_columns = [column for column in t_feedsearch.columns if column.name != "document"]
class SearchApiImpl(BaseSearchApi):
"""
This class represents the implementation of the `/search` endpoints.
"""
@staticmethod
def get_parsed_search_tsquery(search_query: str) -> str:
"""
Parse the search query to be used in the database query.
The resulting query will be in the form of `search_query:*` if the search query is not empty.
Spaces are trimmed from the search query.
"""
parsed_query = f"{search_query.strip()}:*" if search_query and len(search_query.strip()) > 0 else ""
return func.plainto_tsquery("english", unaccent(parsed_query))
@staticmethod
def add_search_query_filters(
query,
search_query,
data_type,
feed_id,
status,
is_official,
features,
version,
license_ids,
license_is_spdx,
license_tags,
) -> Query:
"""
Add filters to the search query.
Filter values are trimmed and converted to lowercase.
The search query is also converted to its unaccented version.
"""
query = query.filter(
or_(
t_feedsearch.c.operational_status == "published",
not is_user_email_restricted(),
)
)
if feed_id:
query = query.where(t_feedsearch.c.feed_stable_id == feed_id.strip().lower())
if data_type:
data_types = [dt.strip().lower() for dt in data_type.split(",")]
if data_types:
query = query.where(t_feedsearch.c.data_type.in_(data_types))
if status:
status_list = [s.strip().lower() for s in status[0].split(",") if s]
if status_list:
query = query.where(t_feedsearch.c.status.in_([s.strip().lower() for s in status_list]))
if is_official is not None:
if is_official:
query = query.where(t_feedsearch.c.official.is_(True))
else:
query = query.where(or_(t_feedsearch.c.official.is_(False), t_feedsearch.c.official.is_(None)))
if version:
versions_list = [v.strip().lower() for v in version.split(",") if v]
if versions_list:
query = query.where(t_feedsearch.c.versions.op("?|")(array(versions_list)))
if search_query and len(search_query.strip()) > 0:
query = query.filter(
t_feedsearch.c.document.op("@@")(SearchApiImpl.get_parsed_search_tsquery(search_query))
)
if license_ids:
license_ids_list = [lid.strip() for lid in license_ids.split(",") if len(lid.strip()) > 0]
if len(license_ids_list) > 0:
query = query.where(t_feedsearch.c.license_id.in_(license_ids_list))
if license_is_spdx is not None:
if license_is_spdx:
query = query.where(t_feedsearch.c.license_is_spdx.is_(True))
else:
query = query.where(
or_(t_feedsearch.c.license_is_spdx.is_(False), t_feedsearch.c.license_is_spdx.is_(None))
)
if license_tags:
tag_ids_list = [tid.strip() for tid in license_tags.split(",") if len(tid.strip()) > 0]
if len(tag_ids_list) > 0:
# license_tags is a text[] column – use the @> (contains) operator
# so that ALL requested tags must be present (AND semantics).
query = query.where(
t_feedsearch.c.license_tags.op("@>")(
func.cast(array(tag_ids_list), t_feedsearch.c.license_tags.type)
)
)
# Add feature filter with OR logic
if features:
features_list = [s.strip() for s in features[0].split(",") if s]
if features_list:
query = query.filter(
t_feedsearch.c.latest_dataset_features.op("&&")(features_list)
) # overlap: Test if elements are a superset of the elements of the argument array expression.
return query
@staticmethod
def create_count_search_query(
status: List[str],
feed_id: str,
data_type: str,
is_official: bool,
features,
version: str,
search_query: str,
license_ids: str,
license_is_spdx: bool,
license_tags: str,
) -> Query:
"""
Create a search query for the database.
"""
query = select(func.count(t_feedsearch.c.feed_id))
return SearchApiImpl.add_search_query_filters(
query,
search_query,
data_type,
feed_id,
status,
is_official,
features,
version,
license_ids,
license_is_spdx,
license_tags,
)
@staticmethod
def create_search_query(
status: List[str],
feed_id: str,
data_type: str,
is_official: bool,
search_query: str,
features: List[str],
version: str,
license_ids: str,
license_is_spdx: bool,
license_tags: str,
) -> Query:
"""
Create a search query for the database.
"""
# TODO: Add sorting and keep the rank sorting by default
rank_expression = func.ts_rank(
t_feedsearch.c.document, SearchApiImpl.get_parsed_search_tsquery(search_query)
).label("rank")
query = select(
rank_expression,
*feed_search_columns,
)
query = SearchApiImpl.add_search_query_filters(
query,
search_query,
data_type,
feed_id,
status,
is_official,
features,
version,
license_ids,
license_is_spdx,
license_tags,
)
# If search query is provided, use it as secondary sort after timestamp
if search_query and len(search_query.strip()) > 0:
return query.order_by(
t_feedsearch.c.created_at.desc(), # Primary sort: newest first
rank_expression.desc(), # Secondary sort: relevance
)
else:
return query.order_by(t_feedsearch.c.created_at.desc())
@with_db_session
def search_feeds(
self,
limit: int,
offset: int,
status: List[str],
feed_id: str,
data_type: str,
is_official: bool,
version: str,
search_query: str,
feature: List[str],
license_ids: str,
license_is_spdx: bool,
license_tags: str,
db_session: "Session",
) -> SearchFeeds200Response:
"""Search feeds using full-text search on feed, location and provider's information."""
query = self.create_search_query(
status,
feed_id,
data_type,
is_official,
search_query,
feature,
version,
license_ids,
license_is_spdx,
license_tags,
)
feed_rows = Database().select(
session=db_session,
query=query,
limit=limit,
offset=offset,
)
feed_total_count = Database().select(
session=db_session,
query=self.create_count_search_query(
status,
feed_id,
data_type,
is_official,
feature,
version,
search_query,
license_ids,
license_is_spdx,
license_tags,
),
)
if feed_rows is None or feed_total_count is None:
return SearchFeeds200Response(
results=[],
total=0,
)
results = list(map(lambda feed: SearchFeedItemResultImpl.from_orm(feed), feed_rows))
return SearchFeeds200Response(
results=results,
total=feed_total_count[0][0] if feed_total_count and feed_total_count[0] else 0,
)