| 1 |
from __future__ import annotations |
| 2 |
|
| 3 |
import logging |
| 4 |
import sys |
| 5 |
import typing as t |
| 6 |
import uuid |
| 7 |
|
| 8 |
from globus_sdk import client, paging, response |
| 9 |
from globus_sdk._internal.remarshal import commajoin, strseq_listify |
| 10 |
from globus_sdk._missing import MISSING, MissingType |
| 11 |
from globus_sdk.scopes import SearchScopes |
| 12 |
|
| 13 |
from .data import SearchQueryV1, SearchScrollQuery |
| 14 |
from .errors import SearchAPIError |
| 15 |
from .response import IndexListResponse |
| 16 |
|
| 17 |
if sys.version_info >= (3, 10): |
| 18 |
from typing import TypeAlias |
| 19 |
else: |
| 20 |
from typing_extensions import TypeAlias |
| 21 |
|
| 22 |
log = logging.getLogger(__name__) |
| 23 |
|
| 24 |
_VALID_ROLE_NAMES_T: TypeAlias = t.Literal["owner", "admin", "writer"] |
| 25 |
|
| 26 |
|
| 27 |
class SearchClient(client.BaseClient): |
| 28 |
r""" |
| 29 |
Client for the Globus Search API |
| 30 |
|
| 31 |
.. sdk-sphinx-copy-params:: BaseClient |
| 32 |
|
| 33 |
This class provides helper methods for most common resources in the |
| 34 |
API, and basic ``get``, ``put``, ``post``, and ``delete`` methods |
| 35 |
from the base client that can be used to access any API resource. |
| 36 |
|
| 37 |
.. automethodlist:: globus_sdk.SearchClient |
| 38 |
""" |
| 39 |
|
| 40 |
error_class = SearchAPIError |
| 41 |
service_name = "search" |
| 42 |
scopes = SearchScopes |
| 43 |
default_scope_requirements = [SearchScopes.search] |
| 44 |
|
| 45 |
# |
| 46 |
# Index Management |
| 47 |
# |
| 48 |
|
| 49 |
def create_index( |
| 50 |
self, display_name: str, description: str |
| 51 |
) -> response.GlobusHTTPResponse: |
| 52 |
""" |
| 53 |
Create a new index. |
| 54 |
|
| 55 |
:param display_name: the name of the index |
| 56 |
:param description: a description of the index |
| 57 |
|
| 58 |
New indices default to trial status. For subscribers with a subscription ID, |
| 59 |
indices can be converted to non-trial by sending a request to [email protected] |
| 60 |
|
| 61 |
.. tab-set:: |
| 62 |
|
| 63 |
.. tab-item:: Example Usage |
| 64 |
|
| 65 |
.. code-block:: python |
| 66 |
|
| 67 |
sc = globus_sdk.SearchClient(...) |
| 68 |
r = sc.create_index( |
| 69 |
"History and Witchcraft", |
| 70 |
"Searchable information about history and witchcraft", |
| 71 |
) |
| 72 |
print(f"index ID: {r['id']}") |
| 73 |
|
| 74 |
.. tab-item:: Example Response Data |
| 75 |
|
| 76 |
.. expandtestfixture:: search.create_index |
| 77 |
|
| 78 |
.. tab-item:: API Info |
| 79 |
|
| 80 |
``POST /v1/index`` |
| 81 |
|
| 82 |
.. extdoclink:: Index Create |
| 83 |
:ref: search/reference/index_create/ |
| 84 |
""" |
| 85 |
log.debug(f"SearchClient.create_index({display_name!r}, ...)") |
| 86 |
return self.post( |
| 87 |
"/v1/index", data={"display_name": display_name, "description": description} |
| 88 |
) |
| 89 |
|
| 90 |
def update_index( |
| 91 |
self, |
| 92 |
index_id: uuid.UUID | str, |
| 93 |
*, |
| 94 |
display_name: str | MissingType = MISSING, |
| 95 |
description: str | MissingType = MISSING, |
| 96 |
) -> response.GlobusHTTPResponse: |
| 97 |
""" |
| 98 |
Update index metadata. |
| 99 |
|
| 100 |
:param index_id: the ID of the index |
| 101 |
:param display_name: the name of the index |
| 102 |
:param description: a description of the index |
| 103 |
|
| 104 |
.. tab-set:: |
| 105 |
|
| 106 |
.. tab-item:: Example Usage |
| 107 |
|
| 108 |
.. code-block:: python |
| 109 |
|
| 110 |
sc = globus_sdk.SearchClient(...) |
| 111 |
MY_INDEX_ID = ... |
| 112 |
r = sc.update_index( |
| 113 |
MY_INDEX_ID, |
| 114 |
display_name="My Awesome Index", |
| 115 |
description="Very awesome searchable data", |
| 116 |
) |
| 117 |
print(f"index ID: {r['id']}") |
| 118 |
|
| 119 |
.. tab-item:: Example Response Data |
| 120 |
|
| 121 |
.. expandtestfixture:: search.create_index |
| 122 |
|
| 123 |
.. tab-item:: API Info |
| 124 |
|
| 125 |
``PATCH /v1/index/<index_id>`` |
| 126 |
|
| 127 |
.. extdoclink:: Index Update |
| 128 |
:ref: search/reference/index_update/ |
| 129 |
""" |
| 130 |
log.debug(f"SearchClient.update_index({index_id!r}, ...)") |
| 131 |
return self.patch( |
| 132 |
f"/v1/index/{index_id}", |
| 133 |
data={"display_name": display_name, "description": description}, |
| 134 |
) |
| 135 |
|
| 136 |
def delete_index(self, index_id: uuid.UUID | str) -> response.GlobusHTTPResponse: |
| 137 |
""" |
| 138 |
Mark an index for deletion. |
| 139 |
|
| 140 |
Globus Search does not immediately delete indices. Instead, this API sets the |
| 141 |
index status to ``"delete-pending"``. |
| 142 |
Search will move pending tasks on the index to the ``CANCELLED`` state and will |
| 143 |
eventually delete the index. |
| 144 |
|
| 145 |
If the index is a trial index, it will be deleted a few minutes after being |
| 146 |
marked for deletion. |
| 147 |
If the index is non-trial, it will be kept for 30 days and will be eligible for |
| 148 |
use with the ``reopen`` API (see :meth:`~.reopen_index`) during that time. |
| 149 |
|
| 150 |
:param index_id: the ID of the index |
| 151 |
|
| 152 |
.. tab-set:: |
| 153 |
|
| 154 |
.. tab-item:: Example Usage |
| 155 |
|
| 156 |
.. code-block:: python |
| 157 |
|
| 158 |
sc = globus_sdk.SearchClient(...) |
| 159 |
sc.delete_index(index_id) |
| 160 |
|
| 161 |
.. tab-item:: Example Response Data |
| 162 |
|
| 163 |
.. expandtestfixture:: search.delete_index |
| 164 |
|
| 165 |
.. tab-item:: API Info |
| 166 |
|
| 167 |
``DELETE /v1/index/<index_id>`` |
| 168 |
|
| 169 |
.. extdoclink:: Index Delete |
| 170 |
:ref: search/reference/index_delete/ |
| 171 |
""" |
| 172 |
log.debug(f"SearchClient.delete_index({index_id!r}, ...)") |
| 173 |
return self.delete(f"/v1/index/{index_id}") |
| 174 |
|
| 175 |
def reopen_index(self, index_id: uuid.UUID | str) -> response.GlobusHTTPResponse: |
| 176 |
""" |
| 177 |
Reopen an index that has been marked for deletion, cancelling the deletion. |
| 178 |
|
| 179 |
:param index_id: the ID of the index |
| 180 |
|
| 181 |
.. tab-set:: |
| 182 |
|
| 183 |
.. tab-item:: Example Usage |
| 184 |
|
| 185 |
.. code-block:: python |
| 186 |
|
| 187 |
sc = globus_sdk.SearchClient(...) |
| 188 |
sc.reopen_index(index_id) |
| 189 |
|
| 190 |
.. tab-item:: Example Response Data |
| 191 |
|
| 192 |
.. expandtestfixture:: search.reopen_index |
| 193 |
|
| 194 |
.. tab-item:: API Info |
| 195 |
|
| 196 |
``POST /v1/index/<index_id>/reopen`` |
| 197 |
|
| 198 |
.. extdoclink:: Index Reopen |
| 199 |
:ref: search/reference/index_reopen/ |
| 200 |
""" |
| 201 |
log.debug(f"SearchClient.reopen_index({index_id!r}, ...)") |
| 202 |
return self.post(f"/v1/index/{index_id}/reopen") |
| 203 |
|
| 204 |
def get_index( |
| 205 |
self, |
| 206 |
index_id: uuid.UUID | str, |
| 207 |
*, |
| 208 |
query_params: dict[str, t.Any] | None = None, |
| 209 |
) -> response.GlobusHTTPResponse: |
| 210 |
""" |
| 211 |
Get descriptive data about a Search index, including its title and description |
| 212 |
and how much data it contains. |
| 213 |
|
| 214 |
:param index_id: the ID of the index |
| 215 |
:param query_params: additional parameters to pass as query params |
| 216 |
|
| 217 |
.. tab-set:: |
| 218 |
|
| 219 |
.. tab-item:: Example Usage |
| 220 |
|
| 221 |
.. code-block:: python |
| 222 |
|
| 223 |
sc = globus_sdk.SearchClient(...) |
| 224 |
index = sc.get_index(index_id) |
| 225 |
assert index["id"] == index_id |
| 226 |
print(index["display_name"], "(" + index_id + "):", index["description"]) |
| 227 |
|
| 228 |
.. tab-item:: API Info |
| 229 |
|
| 230 |
``GET /v1/index/<index_id>`` |
| 231 |
|
| 232 |
.. extdoclink:: Index Show |
| 233 |
:ref: search/reference/index_show/ |
| 234 |
""" # noqa: E501 |
| 235 |
log.debug(f"SearchClient.get_index({index_id})") |
| 236 |
return self.get(f"/v1/index/{index_id}", query_params=query_params) |
| 237 |
|
| 238 |
def index_list( |
| 239 |
self, |
| 240 |
filter_roles: ( |
| 241 |
_VALID_ROLE_NAMES_T | t.Iterable[_VALID_ROLE_NAMES_T] | MissingType |
| 242 |
) = MISSING, |
| 243 |
*, |
| 244 |
query_params: dict[str, t.Any] | None = None, |
| 245 |
) -> response.IterableResponse: |
| 246 |
""" |
| 247 |
Get a list of indices on which the caller has permissions. |
| 248 |
|
| 249 |
:param filter_roles: An iterable of roles to use to filter the listing. By |
| 250 |
default, all indices where the user has a role are returned. |
| 251 |
Valid values are ``owner``, ``admin``, and ``writer``. |
| 252 |
:param query_params: additional parameters to pass as query params |
| 253 |
|
| 254 |
.. tab-set:: |
| 255 |
|
| 256 |
.. tab-item:: Example Usage |
| 257 |
|
| 258 |
.. code-block:: python |
| 259 |
|
| 260 |
sc = globus_sdk.SearchClient(...) |
| 261 |
for index_doc in sc.index_list(): |
| 262 |
print(index_doc["display_name"], f"({index_doc['id']}):") |
| 263 |
print(" permissions:", ", ".join(index_doc["permissions"])) |
| 264 |
|
| 265 |
.. tab-item:: Example Response Data |
| 266 |
|
| 267 |
.. expandtestfixture:: search.index_list |
| 268 |
|
| 269 |
.. tab-item:: API Info |
| 270 |
|
| 271 |
``GET /v1/index_list`` |
| 272 |
|
| 273 |
.. extdoclink:: Index List |
| 274 |
:ref: search/reference/index_list/ |
| 275 |
""" # noqa: E501 |
| 276 |
log.debug("SearchClient.index_list()") |
| 277 |
query_params = {"filter_roles": commajoin(filter_roles), **(query_params or {})} |
| 278 |
return IndexListResponse(self.get("/v1/index_list", query_params=query_params)) |
| 279 |
|
| 280 |
# |
| 281 |
# Search queries |
| 282 |
# |
| 283 |
|
| 284 |
@paging.has_paginator( |
| 285 |
paging.HasNextPaginator, |
| 286 |
items_key="gmeta", |
| 287 |
get_page_size=lambda x: x["count"], |
| 288 |
max_total_results=10000, |
| 289 |
page_size=100, |
| 290 |
) |
| 291 |
def search( |
| 292 |
self, |
| 293 |
index_id: uuid.UUID | str, |
| 294 |
q: str, |
| 295 |
*, |
| 296 |
offset: int | MissingType = MISSING, |
| 297 |
limit: int | MissingType = MISSING, |
| 298 |
advanced: bool | MissingType = MISSING, |
| 299 |
query_params: dict[str, t.Any] | None = None, |
| 300 |
) -> response.GlobusHTTPResponse: |
| 301 |
""" |
| 302 |
Execute a simple Search Query, described by the query string ``q``. |
| 303 |
|
| 304 |
:param index_id: the ID of the index |
| 305 |
:param q: the query string |
| 306 |
:param offset: an offset for pagination |
| 307 |
:param limit: the size of a page of results |
| 308 |
:param advanced: enable 'advanced' query mode, which has sophisticated syntax |
| 309 |
but may result in BadRequest errors when used if the query is invalid |
| 310 |
:param query_params: additional parameters to pass as query params |
| 311 |
|
| 312 |
For details on query syntax, including the ``advanced`` query behavior, see |
| 313 |
the :extdoclink:`Search Query Syntax <search/query#query_syntax>` documentation. |
| 314 |
|
| 315 |
.. tab-set:: |
| 316 |
|
| 317 |
.. tab-item:: Example Usage |
| 318 |
|
| 319 |
.. code-block:: python |
| 320 |
|
| 321 |
sc = globus_sdk.SearchClient(...) |
| 322 |
result = sc.search(index_id, "query string") |
| 323 |
advanced_result = sc.search(index_id, 'author: "Ada Lovelace"', advanced=True) |
| 324 |
|
| 325 |
.. tab-item:: Paginated Usage |
| 326 |
|
| 327 |
.. paginatedusage:: search |
| 328 |
|
| 329 |
.. tab-item:: API Info |
| 330 |
|
| 331 |
``GET /v1/index/<index_id>/search`` |
| 332 |
|
| 333 |
.. extdoclink:: GET Search Query |
| 334 |
:ref: search/reference/get_query/ |
| 335 |
|
| 336 |
.. tab-item:: Example Response Data |
| 337 |
|
| 338 |
.. expandtestfixture:: search.search |
| 339 |
""" # noqa: E501 |
| 340 |
query_params = { |
| 341 |
"q": q, |
| 342 |
"offset": offset, |
| 343 |
"limit": limit, |
| 344 |
"advanced": advanced, |
| 345 |
**(query_params or {}), |
| 346 |
} |
| 347 |
log.debug(f"SearchClient.search({index_id}, ...)") |
| 348 |
return self.get(f"/v1/index/{index_id}/search", query_params=query_params) |
| 349 |
|
| 350 |
@paging.has_paginator( |
| 351 |
paging.HasNextPaginator, |
| 352 |
items_key="gmeta", |
| 353 |
get_page_size=lambda x: x["count"], |
| 354 |
max_total_results=10000, |
| 355 |
page_size=100, |
| 356 |
) |
| 357 |
def post_search( |
| 358 |
self, |
| 359 |
index_id: uuid.UUID | str, |
| 360 |
data: dict[str, t.Any] | SearchQueryV1, |
| 361 |
*, |
| 362 |
offset: int | MissingType = MISSING, |
| 363 |
limit: int | MissingType = MISSING, |
| 364 |
) -> response.GlobusHTTPResponse: |
| 365 |
""" |
| 366 |
Execute a complex Search Query, using a query document to express filters, |
| 367 |
facets, sorting, field boostring, and other behaviors. |
| 368 |
|
| 369 |
:param index_id: The index on which to search |
| 370 |
:param data: A Search Query document containing the query and any other fields |
| 371 |
:param offset: offset used in paging (overwrites any offset in ``data``) |
| 372 |
:param limit: limit the number of results (overwrites any limit in ``data``) |
| 373 |
|
| 374 |
For details on query syntax, including the ``advanced`` query behavior, see |
| 375 |
the :extdoclink:`Search Query Syntax <search/query#query_syntax>` documentation. |
| 376 |
|
| 377 |
.. tab-set:: |
| 378 |
|
| 379 |
.. tab-item:: Example Usage |
| 380 |
|
| 381 |
.. code-block:: python |
| 382 |
|
| 383 |
sc = globus_sdk.SearchClient(...) |
| 384 |
query_data = { |
| 385 |
"q": "user query", |
| 386 |
"filters": [ |
| 387 |
{ |
| 388 |
"type": "range", |
| 389 |
"field_name": "path.to.date", |
| 390 |
"values": [{"from": "*", "to": "2014-11-07"}], |
| 391 |
} |
| 392 |
], |
| 393 |
"facets": [ |
| 394 |
{ |
| 395 |
"name": "Publication Date", |
| 396 |
"field_name": "path.to.date", |
| 397 |
"type": "date_histogram", |
| 398 |
"date_interval": "year", |
| 399 |
} |
| 400 |
], |
| 401 |
"sort": [{"field_name": "path.to.date", "order": "asc"}], |
| 402 |
} |
| 403 |
search_result = sc.post_search(index_id, query_data) |
| 404 |
|
| 405 |
.. tab-item:: Paginated Usage |
| 406 |
|
| 407 |
.. paginatedusage:: post_search |
| 408 |
|
| 409 |
.. tab-item:: API Info |
| 410 |
|
| 411 |
``POST /v1/index/<index_id>/search`` |
| 412 |
|
| 413 |
.. extdoclink:: POST Search Query |
| 414 |
:ref: search/reference/post_query/ |
| 415 |
""" |
| 416 |
log.debug(f"SearchClient.post_search({index_id}, ...)") |
| 417 |
add_kwargs = {} |
| 418 |
if offset is not MISSING: |
| 419 |
add_kwargs["offset"] = offset |
| 420 |
if limit is not MISSING: |
| 421 |
add_kwargs["limit"] = limit |
| 422 |
data = {**data, **add_kwargs} |
| 423 |
return self.post(f"v1/index/{index_id}/search", data=data) |
| 424 |
|
| 425 |
@paging.has_paginator(paging.MarkerPaginator, items_key="gmeta") |
| 426 |
def scroll( |
| 427 |
self, |
| 428 |
index_id: uuid.UUID | str, |
| 429 |
data: dict[str, t.Any] | SearchScrollQuery, |
| 430 |
*, |
| 431 |
marker: str | MissingType = MISSING, |
| 432 |
) -> response.GlobusHTTPResponse: |
| 433 |
""" |
| 434 |
Scroll all data in a Search index. The paginated version of this API should |
| 435 |
typically be preferred, as it is the intended mode of usage. |
| 436 |
|
| 437 |
Note that if data is written or deleted during scrolling, it is possible for |
| 438 |
scrolling to not include results or show other unexpected behaviors. |
| 439 |
|
| 440 |
:param index_id: The index on which to search |
| 441 |
:param data: A Search Scroll Query document |
| 442 |
:param marker: marker used in paging (overwrites any marker in ``data``) |
| 443 |
|
| 444 |
For details on query syntax, including the ``advanced`` query behavior, see |
| 445 |
the :extdoclink:`Search Query Syntax <search/query#query_syntax>` documentation. |
| 446 |
|
| 447 |
.. tab-set:: |
| 448 |
|
| 449 |
.. tab-item:: Example Usage |
| 450 |
|
| 451 |
.. code-block:: python |
| 452 |
|
| 453 |
sc = globus_sdk.SearchClient(...) |
| 454 |
scroll_result = sc.scroll(index_id, {"q": "*"}) |
| 455 |
|
| 456 |
.. tab-item:: Paginated Usage |
| 457 |
|
| 458 |
.. paginatedusage:: scroll |
| 459 |
|
| 460 |
.. tab-item:: API Info |
| 461 |
|
| 462 |
``POST /v1/index/<index_id>/scroll`` |
| 463 |
|
| 464 |
.. extdoclink:: Scroll Query |
| 465 |
:ref: search/reference/scroll_query/ |
| 466 |
""" |
| 467 |
log.debug(f"SearchClient.scroll({index_id}, ...)") |
| 468 |
add_kwargs = {} |
| 469 |
if marker is not MISSING: |
| 470 |
add_kwargs["marker"] = marker |
| 471 |
data = {**data, **add_kwargs} |
| 472 |
return self.post(f"v1/index/{index_id}/scroll", data=data) |
| 473 |
|
| 474 |
# |
| 475 |
# Bulk data indexing |
| 476 |
# |
| 477 |
|
| 478 |
def ingest( |
| 479 |
self, index_id: uuid.UUID | str, data: dict[str, t.Any] |
| 480 |
) -> response.GlobusHTTPResponse: |
| 481 |
""" |
| 482 |
Write data to a Search index as an asynchronous task. |
| 483 |
The data can be provided as a single document or list of documents, but only one |
| 484 |
``task_id`` value will be included in the response. |
| 485 |
|
| 486 |
:param index_id: The index into which to write data |
| 487 |
:param data: an ingest document |
| 488 |
|
| 489 |
.. tab-set:: |
| 490 |
|
| 491 |
.. tab-item:: Example Usage |
| 492 |
|
| 493 |
.. code-block:: python |
| 494 |
|
| 495 |
sc = globus_sdk.SearchClient(...) |
| 496 |
ingest_data = { |
| 497 |
"ingest_type": "GMetaEntry", |
| 498 |
"ingest_data": { |
| 499 |
"subject": "https://example.com/foo/bar", |
| 500 |
"visible_to": ["public"], |
| 501 |
"content": {"foo/bar": "some val"}, |
| 502 |
}, |
| 503 |
} |
| 504 |
sc.ingest(index_id, ingest_data) |
| 505 |
|
| 506 |
or with multiple entries at once via a GMetaList: |
| 507 |
|
| 508 |
.. code-block:: python |
| 509 |
|
| 510 |
sc = globus_sdk.SearchClient(...) |
| 511 |
ingest_data = { |
| 512 |
"ingest_type": "GMetaList", |
| 513 |
"ingest_data": { |
| 514 |
"gmeta": [ |
| 515 |
{ |
| 516 |
"subject": "https://example.com/foo/bar", |
| 517 |
"visible_to": ["public"], |
| 518 |
"content": {"foo/bar": "some val"}, |
| 519 |
}, |
| 520 |
{ |
| 521 |
"subject": "https://example.com/foo/bar", |
| 522 |
"id": "otherentry", |
| 523 |
"visible_to": ["public"], |
| 524 |
"content": {"foo/bar": "some otherval"}, |
| 525 |
}, |
| 526 |
] |
| 527 |
}, |
| 528 |
} |
| 529 |
sc.ingest(index_id, ingest_data) |
| 530 |
|
| 531 |
.. tab-item:: API Info |
| 532 |
|
| 533 |
``POST /v1/index/<index_id>/ingest`` |
| 534 |
|
| 535 |
.. extdoclink:: Ingest |
| 536 |
:ref: search/reference/ingest/ |
| 537 |
""" |
| 538 |
log.debug(f"SearchClient.ingest({index_id}, ...)") |
| 539 |
return self.post(f"/v1/index/{index_id}/ingest", data=data) |
| 540 |
|
| 541 |
# |
| 542 |
# Bulk delete |
| 543 |
# |
| 544 |
|
| 545 |
def delete_by_query( |
| 546 |
self, index_id: uuid.UUID | str, data: dict[str, t.Any] |
| 547 |
) -> response.GlobusHTTPResponse: |
| 548 |
""" |
| 549 |
Delete data in a Search index as an asynchronous task, deleting all documents |
| 550 |
which match a given query. |
| 551 |
The query uses a restricted subset of the syntax available for complex queries, |
| 552 |
as it is not meaningful to boost, sort, or otherwise rank data in this case. |
| 553 |
|
| 554 |
A ``task_id`` value will be included in the response. |
| 555 |
|
| 556 |
:param index_id: The index in which to delete data |
| 557 |
:param data: a query document for documents to delete |
| 558 |
|
| 559 |
.. tab-set:: |
| 560 |
|
| 561 |
.. tab-item:: Example Usage |
| 562 |
|
| 563 |
.. code-block:: python |
| 564 |
|
| 565 |
sc = globus_sdk.SearchClient(...) |
| 566 |
query_data = { |
| 567 |
"q": "user query", |
| 568 |
"filters": [ |
| 569 |
{ |
| 570 |
"type": "range", |
| 571 |
"field_name": "path.to.date", |
| 572 |
"values": [{"from": "*", "to": "2014-11-07"}], |
| 573 |
} |
| 574 |
], |
| 575 |
} |
| 576 |
sc.delete_by_query(index_id, query_data) |
| 577 |
|
| 578 |
.. tab-item:: API Info |
| 579 |
|
| 580 |
``POST /v1/index/<index_id>/delete_by_query`` |
| 581 |
|
| 582 |
.. extdoclink:: Delete By Query |
| 583 |
:ref: search/reference/delete_by_query/ |
| 584 |
""" |
| 585 |
log.debug(f"SearchClient.delete_by_query({index_id}, ...)") |
| 586 |
return self.post(f"/v1/index/{index_id}/delete_by_query", data=data) |
| 587 |
|
| 588 |
def batch_delete_by_subject( |
| 589 |
self, |
| 590 |
index_id: uuid.UUID | str, |
| 591 |
subjects: t.Iterable[str], |
| 592 |
additional_params: dict[str, t.Any] | None = None, |
| 593 |
) -> response.GlobusHTTPResponse: |
| 594 |
""" |
| 595 |
Delete data in a Search index as an asynchronous task, deleting multiple |
| 596 |
documents based on their ``subject`` values. |
| 597 |
|
| 598 |
A ``task_id`` value will be included in the response. |
| 599 |
|
| 600 |
:param index_id: The index in which to delete data |
| 601 |
:param subjects: The subjects to delete, as an iterable of strings |
| 602 |
:param additional_params: Additional parameters to include in the request body |
| 603 |
|
| 604 |
.. tab-set:: |
| 605 |
|
| 606 |
.. tab-item:: Example Usage |
| 607 |
|
| 608 |
.. code-block:: python |
| 609 |
|
| 610 |
sc = globus_sdk.SearchClient(...) |
| 611 |
sc.batch_delete_by_subject( |
| 612 |
index_id, |
| 613 |
subjects=[ |
| 614 |
"very-cool-document", |
| 615 |
"less-cool-document", |
| 616 |
"document-wearing-sunglasses", |
| 617 |
], |
| 618 |
) |
| 619 |
|
| 620 |
.. tab-item:: Example Response Data |
| 621 |
|
| 622 |
.. expandtestfixture:: search.batch_delete_by_subject |
| 623 |
|
| 624 |
.. tab-item:: API Info |
| 625 |
|
| 626 |
``POST /v1/index/<index_id>/batch_delete_by_subject`` |
| 627 |
|
| 628 |
.. extdoclink:: Delete By Subject |
| 629 |
:ref: search/reference/batch_delete_by_subject/ |
| 630 |
""" |
| 631 |
log.debug(f"SearchClient.batch_delete_by_subject({index_id}, ...)") |
| 632 |
# convert the provided subjects to a list and use the "safe iter" helper to |
| 633 |
# ensure that a single string is *not* treated as an iterable of strings, |
| 634 |
# which is usually not intentional |
| 635 |
body = { |
| 636 |
"subjects": strseq_listify(subjects), |
| 637 |
**(additional_params or {}), |
| 638 |
} |
| 639 |
return self.post(f"/v1/index/{index_id}/batch_delete_by_subject", data=body) |
| 640 |
|
| 641 |
# |
| 642 |
# Subject Operations |
| 643 |
# |
| 644 |
|
| 645 |
def get_subject( |
| 646 |
self, |
| 647 |
index_id: uuid.UUID | str, |
| 648 |
subject: str, |
| 649 |
*, |
| 650 |
query_params: dict[str, t.Any] | None = None, |
| 651 |
) -> response.GlobusHTTPResponse: |
| 652 |
""" |
| 653 |
Fetch exactly one Subject document from Search, containing one or more Entries. |
| 654 |
|
| 655 |
:param index_id: the index containing this Subject |
| 656 |
:param subject: the subject string to fetch |
| 657 |
:param query_params: additional parameters to pass as query params |
| 658 |
|
| 659 |
.. tab-set:: |
| 660 |
|
| 661 |
.. tab-item:: Example Usage |
| 662 |
|
| 663 |
Fetch the data for subject ``http://example.com/abc`` from index |
| 664 |
``index_id``: |
| 665 |
|
| 666 |
.. code-block:: python |
| 667 |
|
| 668 |
sc = globus_sdk.SearchClient(...) |
| 669 |
subject_data = sc.get_subject(index_id, "http://example.com/abc") |
| 670 |
|
| 671 |
.. tab-item:: API Info |
| 672 |
|
| 673 |
``GET /v1/index/<index_id>/subject`` |
| 674 |
|
| 675 |
.. extdoclink:: Get By Subject |
| 676 |
:ref: search/reference/get_subject/ |
| 677 |
""" |
| 678 |
log.debug(f"SearchClient.get_subject({index_id}, {subject}, ...)") |
| 679 |
query_params = { |
| 680 |
"subject": subject, |
| 681 |
**(query_params or {}), |
| 682 |
} |
| 683 |
return self.get(f"/v1/index/{index_id}/subject", query_params=query_params) |
| 684 |
|
| 685 |
def delete_subject( |
| 686 |
self, |
| 687 |
index_id: uuid.UUID | str, |
| 688 |
subject: str, |
| 689 |
*, |
| 690 |
query_params: dict[str, t.Any] | None = None, |
| 691 |
) -> response.GlobusHTTPResponse: |
| 692 |
""" |
| 693 |
Delete exactly one Subject document from Search, containing one or more Entries, |
| 694 |
as an asynchronous task. |
| 695 |
|
| 696 |
A ``task_id`` value will be included in the response. |
| 697 |
|
| 698 |
:param index_id: the index in which data will be deleted |
| 699 |
:param subject: the subject string for the Subject document to delete |
| 700 |
:param query_params: additional parameters to pass as query params |
| 701 |
|
| 702 |
.. tab-set:: |
| 703 |
|
| 704 |
.. tab-item:: Example Usage |
| 705 |
|
| 706 |
Delete all data for subject ``http://example.com/abc`` from index |
| 707 |
``index_id``, even data which is not visible to the current user: |
| 708 |
|
| 709 |
.. code-block:: python |
| 710 |
|
| 711 |
sc = globus_sdk.SearchClient(...) |
| 712 |
response = sc.delete_subject(index_id, "http://example.com/abc") |
| 713 |
task_id = response["task_id"] |
| 714 |
|
| 715 |
.. tab-item:: API Info |
| 716 |
|
| 717 |
``DELETE /v1/index/<index_id>/subject`` |
| 718 |
|
| 719 |
.. extdoclink:: Delete By Subject |
| 720 |
:ref: search/reference/delete_subject/ |
| 721 |
""" |
| 722 |
log.debug(f"SearchClient.delete_subject({index_id}, {subject}, ...)") |
| 723 |
query_params = { |
| 724 |
"subject": subject, |
| 725 |
**(query_params or {}), |
| 726 |
} |
| 727 |
return self.delete(f"/v1/index/{index_id}/subject", query_params=query_params) |
| 728 |
|
| 729 |
# |
| 730 |
# Entry Operations |
| 731 |
# |
| 732 |
|
| 733 |
def get_entry( |
| 734 |
self, |
| 735 |
index_id: uuid.UUID | str, |
| 736 |
subject: str, |
| 737 |
*, |
| 738 |
entry_id: str | MissingType = MISSING, |
| 739 |
query_params: dict[str, t.Any] | None = None, |
| 740 |
) -> response.GlobusHTTPResponse: |
| 741 |
""" |
| 742 |
Fetch exactly one Entry document from Search, identified by the combination of |
| 743 |
``subject`` string and ``entry_id``, which defaults to ``null``. |
| 744 |
|
| 745 |
:param index_id: the index containing this Entry |
| 746 |
:param subject: the subject string for the Subject document containing this |
| 747 |
Entry |
| 748 |
:param entry_id: the entry_id for this Entry, which defaults to ``null`` |
| 749 |
:param query_params: additional parameters to pass as query params |
| 750 |
|
| 751 |
.. tab-set:: |
| 752 |
|
| 753 |
.. tab-item:: Example Usage |
| 754 |
|
| 755 |
Lookup the entry with a subject of ``https://example.com/foo/bar`` and |
| 756 |
a null entry_id: |
| 757 |
|
| 758 |
.. code-block:: python |
| 759 |
|
| 760 |
sc = globus_sdk.SearchClient(...) |
| 761 |
entry_data = sc.get_entry(index_id, "http://example.com/foo/bar") |
| 762 |
|
| 763 |
Lookup the entry with a subject of ``https://example.com/foo/bar`` and |
| 764 |
an entry_id of ``foo/bar``: |
| 765 |
|
| 766 |
.. code-block:: python |
| 767 |
|
| 768 |
sc = globus_sdk.SearchClient(...) |
| 769 |
entry_data = sc.get_entry(index_id, "http://example.com/foo/bar", entry_id="foo/bar") |
| 770 |
|
| 771 |
.. tab-item:: API Info |
| 772 |
|
| 773 |
``GET /v1/index/<index_id>/entry`` |
| 774 |
|
| 775 |
.. extdoclink:: Get Entry |
| 776 |
:ref: search/reference/get_entry/ |
| 777 |
""" # noqa: E501 |
| 778 |
log.debug( |
| 779 |
"SearchClient.get_entry({}, {}, {}, ...)".format( |
| 780 |
index_id, subject, entry_id |
| 781 |
) |
| 782 |
) |
| 783 |
query_params = { |
| 784 |
"entry_id": entry_id, |
| 785 |
"subject": subject, |
| 786 |
**(query_params or {}), |
| 787 |
} |
| 788 |
return self.get(f"/v1/index/{index_id}/entry", query_params=query_params) |
| 789 |
|
| 790 |
def delete_entry( |
| 791 |
self, |
| 792 |
index_id: uuid.UUID | str, |
| 793 |
subject: str, |
| 794 |
*, |
| 795 |
entry_id: str | MissingType = MISSING, |
| 796 |
query_params: dict[str, t.Any] | None = None, |
| 797 |
) -> response.GlobusHTTPResponse: |
| 798 |
""" |
| 799 |
Delete exactly one Entry document in Search as an asynchronous task. |
| 800 |
|
| 801 |
A ``task_id`` value will be included in the response. |
| 802 |
|
| 803 |
:param index_id: the index in which data will be deleted |
| 804 |
:param subject: the subject string for the Subject of the document to delete |
| 805 |
:param entry_id: the ID string for the Entry to delete |
| 806 |
:param query_params: additional parameters to pass as query params |
| 807 |
|
| 808 |
.. tab-set:: |
| 809 |
|
| 810 |
.. tab-item:: Example Usage |
| 811 |
|
| 812 |
Delete an entry with a subject of ``https://example.com/foo/bar`` and |
| 813 |
a null entry_id: |
| 814 |
|
| 815 |
.. code-block:: python |
| 816 |
|
| 817 |
sc = globus_sdk.SearchClient(...) |
| 818 |
sc.delete_entry(index_id, "https://example.com/foo/bar") |
| 819 |
|
| 820 |
Delete an entry with a subject of ``https://example.com/foo/bar`` and |
| 821 |
an entry_id of "foo/bar": |
| 822 |
|
| 823 |
.. code-block:: python |
| 824 |
|
| 825 |
sc = globus_sdk.SearchClient(...) |
| 826 |
sc.delete_entry(index_id, "https://example.com/foo/bar", entry_id="foo/bar") |
| 827 |
|
| 828 |
.. tab-item:: API Info |
| 829 |
|
| 830 |
``DELETE /v1/index/<index_id>/entry`` |
| 831 |
|
| 832 |
.. extdoclink:: Delete Entry |
| 833 |
:ref: search/reference/delete_entry/ |
| 834 |
""" # noqa: E501 |
| 835 |
log.debug( |
| 836 |
"SearchClient.delete_entry({}, {}, {}, ...)".format( |
| 837 |
index_id, subject, entry_id |
| 838 |
) |
| 839 |
) |
| 840 |
query_params = { |
| 841 |
"entry_id": entry_id, |
| 842 |
"subject": subject, |
| 843 |
**(query_params or {}), |
| 844 |
} |
| 845 |
return self.delete(f"/v1/index/{index_id}/entry", query_params=query_params) |
| 846 |
|
| 847 |
# |
| 848 |
# Task Management |
| 849 |
# |
| 850 |
|
| 851 |
def get_task( |
| 852 |
self, |
| 853 |
task_id: uuid.UUID | str, |
| 854 |
*, |
| 855 |
query_params: dict[str, t.Any] | None = None, |
| 856 |
) -> response.GlobusHTTPResponse: |
| 857 |
""" |
| 858 |
Fetch a Task document by ID, getting task details and status. |
| 859 |
|
| 860 |
:param task_id: the task ID from the original task submission |
| 861 |
:param query_params: additional parameters to pass as query params |
| 862 |
|
| 863 |
.. tab-set:: |
| 864 |
|
| 865 |
.. tab-item:: Example Usage |
| 866 |
|
| 867 |
.. code-block:: python |
| 868 |
|
| 869 |
sc = globus_sdk.SearchClient(...) |
| 870 |
task = sc.get_task(task_id) |
| 871 |
assert task["index_id"] == known_index_id |
| 872 |
print(task["task_id"], "|", task["state"]) |
| 873 |
|
| 874 |
.. tab-item:: API Info |
| 875 |
|
| 876 |
``GET /v1/task/<task_id>`` |
| 877 |
|
| 878 |
.. extdoclink:: Get Task |
| 879 |
:ref: search/reference/get_task/ |
| 880 |
""" |
| 881 |
log.debug(f"SearchClient.get_task({task_id})") |
| 882 |
return self.get(f"/v1/task/{task_id}", query_params=query_params) |
| 883 |
|
| 884 |
def get_task_list( |
| 885 |
self, |
| 886 |
index_id: uuid.UUID | str, |
| 887 |
*, |
| 888 |
query_params: dict[str, t.Any] | None = None, |
| 889 |
) -> response.GlobusHTTPResponse: |
| 890 |
""" |
| 891 |
Fetch a list of recent Task documents for an index, getting task details and |
| 892 |
status. |
| 893 |
|
| 894 |
:param index_id: the index to query |
| 895 |
:param query_params: additional parameters to pass as query params |
| 896 |
|
| 897 |
.. tab-set:: |
| 898 |
|
| 899 |
.. tab-item:: Example Usage |
| 900 |
|
| 901 |
.. code-block:: python |
| 902 |
|
| 903 |
sc = globus_sdk.SearchClient(...) |
| 904 |
task_list = sc.get_task_list(index_id) |
| 905 |
for task in task_list["tasks"]: |
| 906 |
print(task["task_id"], "|", task["state"]) |
| 907 |
|
| 908 |
.. tab-item:: API Info |
| 909 |
|
| 910 |
``GET /v1/task_list/<index_id>`` |
| 911 |
|
| 912 |
.. extdoclink:: Task List |
| 913 |
:ref: search/reference/task_list/ |
| 914 |
""" |
| 915 |
log.debug(f"SearchClient.get_task_list({index_id})") |
| 916 |
return self.get(f"/v1/task_list/{index_id}", query_params=query_params) |
| 917 |
|
| 918 |
# |
| 919 |
# Role Management |
| 920 |
# |
| 921 |
|
| 922 |
def create_role( |
| 923 |
self, |
| 924 |
index_id: uuid.UUID | str, |
| 925 |
data: dict[str, t.Any], |
| 926 |
*, |
| 927 |
query_params: dict[str, t.Any] | None = None, |
| 928 |
) -> response.GlobusHTTPResponse: |
| 929 |
""" |
| 930 |
Create a new role on an index. You must already have the ``owner`` or |
| 931 |
``admin`` role on an index to create additional roles. |
| 932 |
|
| 933 |
Roles are specified as a role name (one of ``"owner"``, ``"admin"``, or |
| 934 |
``"writer"``) and a `Principal URN |
| 935 |
<https://docs.globus.org/api/search/overview/#principal_urns>`_. |
| 936 |
|
| 937 |
:param index_id: The index on which to create the role |
| 938 |
:param data: The partial role document to use for creation |
| 939 |
:param query_params: Any additional query params to pass |
| 940 |
|
| 941 |
.. tab-set:: |
| 942 |
|
| 943 |
.. tab-item:: Example Usage |
| 944 |
|
| 945 |
.. code-block:: python |
| 946 |
|
| 947 |
identity_id = "46bd0f56-e24f-11e5-a510-131bef46955c" |
| 948 |
sc = globus_sdk.SearchClient(...) |
| 949 |
sc.create_role( |
| 950 |
index_id, |
| 951 |
{"role_name": "writer", "principal": f"urn:globus:auth:identity:{identity_id}"}, |
| 952 |
) |
| 953 |
|
| 954 |
.. tab-item:: API Info |
| 955 |
|
| 956 |
``POST /v1/index/<index_id>/role`` |
| 957 |
|
| 958 |
.. extdoclink:: Create Role |
| 959 |
:ref: search/reference/role_create/ |
| 960 |
""" # noqa: E501 |
| 961 |
log.debug("SearchClient.create_role(%s, ...)", index_id) |
| 962 |
return self.post( |
| 963 |
f"/v1/index/{index_id}/role", data=data, query_params=query_params |
| 964 |
) |
| 965 |
|
| 966 |
def get_role_list( |
| 967 |
self, |
| 968 |
index_id: uuid.UUID | str, |
| 969 |
*, |
| 970 |
query_params: dict[str, t.Any] | None = None, |
| 971 |
) -> response.GlobusHTTPResponse: |
| 972 |
""" |
| 973 |
List all roles on an index. You must have the ``owner`` or ``admin`` |
| 974 |
role on an index to list roles. |
| 975 |
|
| 976 |
:param index_id: The index on which to list roles |
| 977 |
:param query_params: Any additional query params to pass |
| 978 |
|
| 979 |
.. tab-set:: |
| 980 |
|
| 981 |
.. tab-item:: API Info |
| 982 |
|
| 983 |
``GET /v1/index/<index_id>/role_list`` |
| 984 |
|
| 985 |
.. extdoclink:: Get Role List |
| 986 |
:ref: search/reference/role_list/ |
| 987 |
""" |
| 988 |
log.debug("SearchClient.get_role_list(%s)", index_id) |
| 989 |
return self.get(f"/v1/index/{index_id}/role_list", query_params=query_params) |
| 990 |
|
| 991 |
def delete_role( |
| 992 |
self, |
| 993 |
index_id: uuid.UUID | str, |
| 994 |
role_id: str, |
| 995 |
*, |
| 996 |
query_params: dict[str, t.Any] | None = None, |
| 997 |
) -> response.GlobusHTTPResponse: |
| 998 |
""" |
| 999 |
Delete a role from an index. You must have the ``owner`` or ``admin`` |
| 1000 |
role on an index to delete roles. You cannot remove the last ``owner`` from an |
| 1001 |
index. |
| 1002 |
|
| 1003 |
:param index_id: The index from which to delete a role |
| 1004 |
:param role_id: The role to delete |
| 1005 |
:param query_params: Any additional query params to pass |
| 1006 |
|
| 1007 |
.. tab-set:: |
| 1008 |
|
| 1009 |
.. tab-item:: API Info |
| 1010 |
|
| 1011 |
``DELETE /v1/index/<index_id>/role/<role_id>`` |
| 1012 |
|
| 1013 |
.. extdoclink:: Role Delete |
| 1014 |
:ref: search/reference/role_delete/ |
| 1015 |
""" |
| 1016 |
log.debug("SearchClient.delete_role(%s, %s)", index_id, role_id) |
| 1017 |
return self.delete( |
| 1018 |
f"/v1/index/{index_id}/role/{role_id}", query_params=query_params |
| 1019 |
) |