SearchIndex¶
API reference¶
synapseclient.models.SearchIndex
dataclass
¶
Bases: SearchIndexSynchronousProtocol, AccessControllable, DeleteMixin, GetMixin
A SearchIndex is a Synapse entity whose content is defined by a Synapse SQL
query (defining_sql). An OpenSearch index is built from the query results,
supporting full-text search, faceted search, and autocomplete.
The defining_sql must reference exactly one table-like entity. Multi-entity
JOIN/UNION queries are not supported. Optionally, a search_configuration_id
may be supplied to control the analyzer/synonym settings used when building
the index. If not specified, the configuration is resolved by walking up
the entity hierarchy.
REST API model: https://rest-docs.synapse.org/rest/org/sagebionetworks/repo/model/search/table/SearchIndex.html
| ATTRIBUTE | DESCRIPTION |
|---|---|
id |
The unique immutable ID for this entity. |
name |
The name of this entity. |
description |
The description of this entity. |
etag |
Synapse OCC etag. |
created_on |
Date this entity was created. |
modified_on |
Date this entity was last modified. |
created_by |
The ID of the user that created this entity. |
modified_by |
The ID of the user that last modified this entity. |
parent_id |
The ID of the parent entity. |
defining_sql |
The Synapse SQL statement that defines which columns and rows are indexed. |
search_configuration_id |
ID of the SearchConfiguration to apply when building this index. Optional. |
annotations |
Additional metadata associated with the entity.
TYPE:
|
activity |
Provenance for this entity. |
Create a new SearchIndex.
from synapseclient import Synapse
from synapseclient.models import SearchIndex
syn = Synapse()
syn.login()
index = SearchIndex(
name="My Search Index",
parent_id="syn12345",
# syn67890 must be a table or a view; multi-entity JOINs are not supported
defining_sql="SELECT * FROM syn67890",
)
index = index.store()
print(f"Created SearchIndex: {index.id}")
Source code in synapseclient/models/search_index.py
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 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 | |
Methods:¶
store
¶
Store metadata about a SearchIndex including the annotations. Creates
a new SearchIndex if id is not set, or updates the existing one
otherwise. defining_sql must be set before calling this.
| PARAMETER | DESCRIPTION |
|---|---|
dry_run
|
If True, will not actually store the SearchIndex but will log to the console what would be created or updated.
TYPE:
|
synapse_client
|
If not passed in and caching was not disabled by
|
| RETURNS | DESCRIPTION |
|---|---|
Self
|
Itself. |
| RAISES | DESCRIPTION |
|---|---|
ValueError
|
If |
Create a new SearchIndex.
from synapseclient import Synapse
from synapseclient.models import SearchIndex
syn = Synapse()
syn.login()
index = SearchIndex(
name="My Search Index",
parent_id="syn12345",
# syn67890 must be a table or a view;
defining_sql="SELECT * FROM syn67890",
)
index = index.store()
print(f"Created SearchIndex: {index.id}")
Source code in synapseclient/models/protocols/search_index_protocol.py
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 | |
get
¶
Get the metadata about the SearchIndex from Synapse. Either id, or
name and parent_id, must be set before calling this.
| PARAMETER | DESCRIPTION |
|---|---|
include_activity
|
If True, will include the provenance activity on the returned SearchIndex.
TYPE:
|
synapse_client
|
If not passed in and caching was not disabled by
|
| RETURNS | DESCRIPTION |
|---|---|
Self
|
Itself, populated from the Synapse response. |
Get a SearchIndex by ID.
from synapseclient import Synapse
from synapseclient.models import SearchIndex
syn = Synapse()
syn.login()
index = SearchIndex(id="syn12345").get()
print(index.name, index.defining_sql)
Source code in synapseclient/models/protocols/search_index_protocol.py
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 | |
delete
¶
Delete the SearchIndex from Synapse. id must be set before calling
this.
| PARAMETER | DESCRIPTION |
|---|---|
synapse_client
|
If not passed in and caching was not disabled by
|
Delete a SearchIndex by ID.
from synapseclient import Synapse
from synapseclient.models import SearchIndex
syn = Synapse()
syn.login()
SearchIndex(id="syn12345").delete()
Source code in synapseclient/models/protocols/search_index_protocol.py
103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 | |
query
¶
query(search_query: SearchQuery, response_parts: Optional[List[SearchQueryPart]] = None, *, job_timeout: int = 600, synapse_client: Optional[Synapse] = None) -> SearchIndexQuery
Query this search index. Unlike a SQL-backed Table, a SearchIndex is queried with the OpenSearch Query DSL carried by a SearchQuery — not with Synapse SQL. See Query for the supported clause kinds.
| PARAMETER | DESCRIPTION |
|---|---|
search_query
|
The OpenSearch
TYPE:
|
response_parts
|
Additional response parts to request beyond the default hits, such as the total hit count or the select columns.
TYPE:
|
job_timeout
|
The maximum amount of time to wait for the query job to
complete before raising a
TYPE:
|
synapse_client
|
If not passed in and caching was not disabled by
|
| RETURNS | DESCRIPTION |
|---|---|
SearchIndexQuery
|
The completed SearchIndexQuery, carrying the |
| RAISES | DESCRIPTION |
|---|---|
ValueError
|
If the |
Query an index for documents mentioning "alzheimer".
from synapseclient import Synapse
from synapseclient.models import SearchIndex, SearchQuery, SearchQueryPart
from synapseclient.models.search_dsl import Query
syn = Synapse()
syn.login()
results = SearchIndex(id="syn12345").query(
search_query=SearchQuery(
query=Query(match={"title": {"query": "alzheimer"}}),
size=10,
),
response_parts=[SearchQueryPart.TOTAL_HITS],
)
print(results.total_hits)
for hit in results.hits:
print(hit.row_id, hit.fields)
Source code in synapseclient/models/protocols/search_index_protocol.py
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 | |
autocomplete
¶
autocomplete(query: Query, source: Optional[SourceFilter] = None, *, synapse_client: Optional[Synapse] = None) -> List[SearchHit]
Run a synchronous autocomplete search against this index. The
autocomplete endpoint allowlists only prefix-style queries
(prefix,
match_phrase_prefix,
or match_bool_prefix)
and caps results at 8.
| PARAMETER | DESCRIPTION |
|---|---|
query
|
The top-level OpenSearch Query DSL
clause -- see Query;
restricted server-side to
TYPE:
|
source
|
Optional source filter selecting which columns are returned on each hit.
TYPE:
|
synapse_client
|
If not passed in and caching was not disabled by
|
| RETURNS | DESCRIPTION |
|---|---|
List[SearchHit]
|
The matching SearchHits, capped at 8. |
| RAISES | DESCRIPTION |
|---|---|
ValueError
|
If the |
Autocomplete titles beginning with "alz".
from synapseclient import Synapse
from synapseclient.models import SearchIndex
from synapseclient.models.search_dsl import Query
syn = Synapse()
syn.login()
index = SearchIndex(id="syn12345")
hits = index.autocomplete(
query=Query(match_phrase_prefix={"title": {"query": "alz"}}),
)
for hit in hits:
print(hit.row_id, hit.fields)
Source code in synapseclient/models/protocols/search_index_protocol.py
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 | |
get_permissions
¶
get_permissions(*, synapse_client: Optional[Synapse] = None) -> Permissions
Get the permissions that the caller has on an Entity.
| PARAMETER | DESCRIPTION |
|---|---|
synapse_client
|
If not passed in and caching was not disabled by
|
| RETURNS | DESCRIPTION |
|---|---|
Permissions
|
A Permissions object |
Using this function:
Getting permissions for a Synapse Entity
from synapseclient import Synapse
from synapseclient.models import File
syn = Synapse()
syn.login()
permissions = File(id="syn123").get_permissions()
Getting access types list from the Permissions object
permissions.access_types
Source code in synapseclient/models/protocols/access_control_protocol.py
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 | |
get_acl
¶
get_acl(principal_id: int = None, check_benefactor: bool = True, *, synapse_client: Optional[Synapse] = None) -> List[str]
Get the ACL that a user or group has on an Entity.
Note: If the entity does not have local sharing settings, or ACL set directly on it, this will look up the ACL on the benefactor of the entity. The benefactor is the entity that the current entity inherits its permissions from. The benefactor is usually the parent entity, but it can be any ancestor in the hierarchy. For example, a newly created Project will be its own benefactor, while a new FileEntity's benefactor will start off as its containing Project or Folder. If the entity already has local sharing settings, the benefactor would be itself.
| PARAMETER | DESCRIPTION |
|---|---|
principal_id
|
Identifier of a user or group (defaults to PUBLIC users)
TYPE:
|
check_benefactor
|
If True (default), check the benefactor for the entity to get the ACL. If False, only check the entity itself. This is useful for checking the ACL of an entity that has local sharing settings, but you want to check the ACL of the entity itself and not the benefactor it may inherit from.
TYPE:
|
synapse_client
|
If not passed in and caching was not disabled by
|
| RETURNS | DESCRIPTION |
|---|---|
List[str]
|
An array containing some combination of ['READ', 'UPDATE', 'CREATE', 'DELETE', 'DOWNLOAD', 'MODERATE', 'CHANGE_PERMISSIONS', 'CHANGE_SETTINGS'] or an empty array |
Source code in synapseclient/models/protocols/access_control_protocol.py
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 | |
set_permissions
¶
set_permissions(principal_id: int = None, access_type: List[str] = None, modify_benefactor: bool = False, warn_if_inherits: bool = True, overwrite: bool = True, *, synapse_client: Optional[Synapse] = None) -> Dict[str, Union[str, list]]
Sets permission that a user or group has on an Entity. An Entity may have its own ACL or inherit its ACL from a benefactor.
| PARAMETER | DESCRIPTION |
|---|---|
principal_id
|
Identifier of a user or group.
TYPE:
|
access_type
|
Type of permission to be granted. One or more of CREATE, READ, DOWNLOAD, UPDATE, DELETE, CHANGE_PERMISSIONS. Defaults to ['READ', 'DOWNLOAD'] |
modify_benefactor
|
Set as True when modifying a benefactor's ACL. The term 'benefactor' is used to indicate which Entity an Entity inherits its ACL from. For example, a newly created Project will be its own benefactor, while a new FileEntity's benefactor will start off as its containing Project. If the entity already has local sharing settings the benefactor would be itself. It may also be the immediate parent, somewhere in the parent tree, or the project itself.
TYPE:
|
warn_if_inherits
|
When
TYPE:
|
overwrite
|
By default this function overwrites existing permissions for the specified user. Set this flag to False to add new permissions non-destructively.
TYPE:
|
synapse_client
|
If not passed in and caching was not disabled by
|
| RETURNS | DESCRIPTION |
|---|---|
Dict[str, Union[str, list]]
|
An Access Control List object |
Setting permissions
Grant all registered users download access
from synapseclient import Synapse
from synapseclient.models import File
syn = Synapse()
syn.login()
File(id="syn123").set_permissions(principal_id=273948, access_type=['READ','DOWNLOAD'])
Grant the public view access
from synapseclient import Synapse
from synapseclient.models import File
syn = Synapse()
syn.login()
File(id="syn123").set_permissions(principal_id=273949, access_type=['READ'])
Source code in synapseclient/models/protocols/access_control_protocol.py
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 | |
delete_permissions
¶
delete_permissions(include_self: bool = True, include_container_content: bool = False, recursive: bool = False, target_entity_types: Optional[List[str]] = None, dry_run: bool = False, show_acl_details: bool = True, show_files_in_containers: bool = True, *, benefactor_tracker: Optional[BenefactorTracker] = None, synapse_client: Optional[Synapse] = None) -> None
Delete the entire Access Control List (ACL) for a given Entity. This is not scoped to a specific user or group, but rather removes all permissions associated with the Entity. After this operation, the Entity will inherit permissions from its benefactor, which is typically its parent entity or the Project it belongs to.
In order to remove permissions for a specific user or group, you
should use the set_permissions method with the access_type set to
an empty list.
By default, Entities such as FileEntity and Folder inherit their permission from their containing Project. For such Entities the Project is the Entity's 'benefactor'. This permission inheritance can be overridden by creating an ACL for the Entity. When this occurs the Entity becomes its own benefactor and all permission are determined by its own ACL.
If the ACL of an Entity is deleted, then its benefactor will automatically be set to its parent's benefactor.
Special notice for Projects: The ACL for a Project cannot be deleted, you must individually update or revoke the permissions for each user or group.
| PARAMETER | DESCRIPTION |
|---|---|
include_self
|
If True (default), delete the ACL of the current entity. If False, skip deleting the ACL of the current entity.
TYPE:
|
include_container_content
|
If True, delete ACLs from contents directly within containers (files and folders inside self). This must be set to True for recursive to have any effect. Defaults to False.
TYPE:
|
recursive
|
If True and the entity is a container (e.g., Project or Folder),
recursively process child containers. Note that this must be used with
include_container_content=True to have any effect. Setting recursive=True
with include_container_content=False will raise a ValueError.
Only works on classes that support the
TYPE:
|
target_entity_types
|
Specify which entity types to process when deleting ACLs.
Allowed values are "folder" and "file" (case-insensitive).
If None, defaults to ["folder", "file"]. This does not affect the
entity type of the current entity, which is always processed if
|
dry_run
|
If True, log the changes that would be made instead of actually performing the deletions. When enabled, all ACL deletion operations are simulated and logged at info level. Defaults to False.
TYPE:
|
show_acl_details
|
When dry_run=True, controls whether current ACL details are displayed for entities that will have their permissions changed. If True (default), shows detailed ACL information. If False, hides ACL details for cleaner output. Has no effect when dry_run=False.
TYPE:
|
show_files_in_containers
|
When dry_run=True, controls whether files within containers are displayed in the preview. If True (default), shows all files. If False, hides files when their only change is benefactor inheritance (but still shows files with local ACLs being deleted). Has no effect when dry_run=False.
TYPE:
|
benefactor_tracker
|
Optional tracker for managing benefactor relationships. Used for recursive functionality to track which entities will be affected
TYPE:
|
synapse_client
|
If not passed in and caching was not disabled by
|
| RETURNS | DESCRIPTION |
|---|---|
None
|
None |
| RAISES | DESCRIPTION |
|---|---|
ValueError
|
If the entity does not have an ID or if an invalid entity type is provided. |
SynapseHTTPError
|
If there are permission issues or if the entity already inherits permissions. |
Exception
|
For any other errors that may occur during the process. |
Note: The caller must be granted ACCESS_TYPE.CHANGE_PERMISSIONS on the Entity to call this method.
Delete permissions for a single entity
from synapseclient import Synapse
from synapseclient.models import File
syn = Synapse()
syn.login()
File(id="syn123").delete_permissions()
Delete permissions recursively for a folder and all its children
from synapseclient import Synapse
from synapseclient.models import Folder
syn = Synapse()
syn.login()
# Delete permissions for this folder only (does not affect children)
Folder(id="syn123").delete_permissions()
# Delete permissions for all files and folders directly within this folder,
# but not the folder itself
Folder(id="syn123").delete_permissions(
include_self=False,
include_container_content=True
)
# Delete permissions for all items in the entire hierarchy (folders and their files)
# Both recursive and include_container_content must be True
Folder(id="syn123").delete_permissions(
recursive=True,
include_container_content=True
)
# Delete permissions only for folder entities within this folder recursively
# and their contents
Folder(id="syn123").delete_permissions(
recursive=True,
include_container_content=True,
target_entity_types=["folder"]
)
# Delete permissions only for files within this folder and all subfolders
Folder(id="syn123").delete_permissions(
include_self=False,
recursive=True,
include_container_content=True,
target_entity_types=["file"]
)
# Dry run example: Log what would be deleted without making changes
Folder(id="syn123").delete_permissions(
recursive=True,
include_container_content=True,
dry_run=True
)
Source code in synapseclient/models/protocols/access_control_protocol.py
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 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 | |
list_acl
¶
list_acl(recursive: bool = False, include_container_content: bool = False, target_entity_types: Optional[List[str]] = None, log_tree: bool = False, *, synapse_client: Optional[Synapse] = None, _progress_bar: Optional[tqdm] = None) -> AclListResult
List the Access Control Lists (ACLs) for this entity and optionally its children.
This function returns the local sharing settings for the entity and optionally its children. It provides a mapping of all ACLs for the given container/entity.
Important Note: This function returns the LOCAL sharing settings only, not the effective permissions that each Synapse User ID/Team has on the entities. More permissive permissions could be granted via a Team that the user has access to that has permissions on the entity, or through inheritance from parent entities.
| PARAMETER | DESCRIPTION |
|---|---|
recursive
|
If True and the entity is a container (e.g., Project or Folder),
recursively process child containers. Note that this must be used with
include_container_content=True to have any effect. Setting recursive=True
with include_container_content=False will raise a ValueError.
Only works on classes that support the
TYPE:
|
include_container_content
|
If True, include ACLs from contents directly within containers (files and folders inside self). This must be set to True for recursive to have any effect. Defaults to False.
TYPE:
|
target_entity_types
|
Specify which entity types to process when listing ACLs. Allowed values are "folder" and "file" (case-insensitive). If None, defaults to ["folder", "file"]. |
log_tree
|
If True, logs the ACL results to console in ASCII tree format showing entity hierarchies and their ACL permissions in a tree-like structure. Defaults to False.
TYPE:
|
synapse_client
|
If not passed in and caching was not disabled by
|
_progress_bar
|
Internal parameter. Progress bar instance to use for updates when called recursively. Should not be used by external callers.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
AclListResult
|
An AclListResult object containing a structured representation of ACLs where: |
AclListResult
|
|
AclListResult
|
|
AclListResult
|
|
| RAISES | DESCRIPTION |
|---|---|
ValueError
|
If the entity does not have an ID or if an invalid entity type is provided. |
SynapseHTTPError
|
If there are permission issues accessing ACLs. |
Exception
|
For any other errors that may occur during the process. |
List ACLs for a single entity
from synapseclient import Synapse
from synapseclient.models import File
syn = Synapse()
syn.login()
acl_result = File(id="syn123").list_acl()
print(acl_result)
# Access entity ACLs (entity_acls is a list, not a dict)
for entity_acl in acl_result.all_entity_acls:
if entity_acl.entity_id == "syn123":
# Access individual ACL entries
for acl_entry in entity_acl.acl_entries:
if acl_entry.principal_id == "273948":
print(f"Principal 273948 has permissions: {acl_entry.permissions}")
# I can also access the ACL for the file itself
print(acl_result.entity_acl)
print(acl_result)
List ACLs recursively for a folder and all its children
from synapseclient import Synapse
from synapseclient.models import Folder
syn = Synapse()
syn.login()
acl_result = Folder(id="syn123").list_acl(
recursive=True,
include_container_content=True
)
# Access each entity's ACL (entity_acls is a list)
for entity_acl in acl_result.all_entity_acls:
print(f"Entity {entity_acl.entity_id} has ACL with {len(entity_acl.acl_entries)} principals")
# I can also access the ACL for the folder itself
print(acl_result.entity_acl)
# List ACLs for only folder entities
folder_acl_result = Folder(id="syn123").list_acl(
recursive=True,
include_container_content=True,
target_entity_types=["folder"]
)
List ACLs with ASCII tree visualization
When log_tree=True, the ACLs will be logged in a tree format. Additionally,
the ascii_tree attribute of the AclListResult will contain the ASCII tree
representation of the ACLs.
from synapseclient import Synapse
from synapseclient.models import Folder
syn = Synapse()
syn.login()
acl_result = Folder(id="syn123").list_acl(
recursive=True,
include_container_content=True,
log_tree=True, # Enable ASCII tree logging
)
# The ASCII tree representation of the ACLs will also be available
# in acl_result.ascii_tree
print(acl_result.ascii_tree)
Source code in synapseclient/models/protocols/access_control_protocol.py
316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 | |
Supporting types¶
synapseclient.models.SearchQuery
dataclass
¶
The body of an OpenSearch _search
request, narrowed to the top-level keys Synapse accepts. Each slot's
contents are pass-through OpenSearch query DSL,
typed by the TypedDict shapes in synapseclient.models.search_dsl.
Represents a Synapse SearchQuery.
Source code in synapseclient/models/search_management.py
1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 | |
Attributes¶
query
class-attribute
instance-attribute
¶
Required. The OpenSearch query DSL
clause -- see Query for every
supported clause kind. Use {"match_all": {}}
to match all documents. See also
query vs. filter context.
post_filter
class-attribute
instance-attribute
¶
Optional. Same DSL shape as query (see
Query), applied after
aggregations are computed. For the distinction between filtering hits and
filtering aggregations, see
query vs. filter context.
aggregations
class-attribute
instance-attribute
¶
aggregations: Optional[Dict[str, Aggregation]] = None
Optional. Map of caller-chosen name to
aggregation definition. The
raw aggregation result comes back on SearchIndexQuery.aggregation_results.
For the facet-counting pattern (a terms aggregation alongside
post_filter), see the
faceted search tutorial.
highlight
class-attribute
instance-attribute
¶
Optional. Adds per-field
snippet fragments
(matched terms wrapped in <em> / </em> by default) to each hit's
highlights.
collapse
class-attribute
instance-attribute
¶
collapse: Optional[FieldCollapse] = None
Optional. Groups the result list so only one hit is returned per distinct value of a field.
rescore
class-attribute
instance-attribute
¶
Optional. Re-ranks
the top hits returned by query using a secondary scoring query.
sort
class-attribute
instance-attribute
¶
Optional. Result ordering,
in native OpenSearch sort shape (a string column name,
{column: "asc|desc"}, or {column: {order: ..., mode: ..., missing: ...}})
applied in order. Only the field and _score sort kinds are accepted --
script and geo-distance sorts are rejected server-side. The pseudo-column
_score sorts by relevance. When omitted, results are sorted by relevance
descending.
source
class-attribute
instance-attribute
¶
source: Optional[SourceFilter] = None
Optional. Source filter
selecting which columns are returned on each hit. Serialized as
_source.
from_
class-attribute
instance-attribute
¶
Optional. Zero-based
pagination
offset; default 0. Ignored when search_after is supplied. Serialized as
from.
synapseclient.models.SearchQueryPart
¶
Optional response parts for a SearchQuery beyond default HITS.
These are values for the responseParts field on a SearchIndexQuery.
Source code in synapseclient/models/search_management.py
75 76 77 78 79 80 81 82 83 | |
synapseclient.models.SearchIndexQuery
dataclass
¶
Bases: AsynchronousCommunicator
An async request to query a SearchIndex's OpenSearch index.
Inherits from AsynchronousCommunicator: call send_job_and_wait_async() to
submit the job, poll the Synapse async job service, and populate response
fields (hits, total_hits, select_columns, aggregation_results,
next_search_after, offset) on this same instance.
The search_query body is an OpenSearch
_search
request -- see SearchQuery for the
allowlisted top-level keys and
Query for the
query DSL clause kinds.
Represents a Synapse SearchIndexQuery.
Run a search query.
import asyncio
from synapseclient import Synapse
from synapseclient.models import SearchIndexQuery, SearchQuery, SearchQueryPart
async def main():
Synapse().login()
query = SearchIndexQuery(
search_index_id="syn22806626",
search_query=SearchQuery(
query={"match": {"title": {"query": "alzheimer"}}},
size=10,
),
response_parts=[SearchQueryPart.HITS, SearchQueryPart.TOTAL_HITS],
)
await query.send_job_and_wait_async()
print(query.total_hits, len(query.hits))
asyncio.run(main())
Source code in synapseclient/models/search_management.py
1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 | |
Attributes¶
concrete_type
class-attribute
instance-attribute
¶
concrete_type: str = concrete_types.SEARCH_INDEX_QUERY
The Synapse concrete type identifying this async request.
search_index_id
class-attribute
instance-attribute
¶
The ID of the SearchIndex entity to query.
search_query
class-attribute
instance-attribute
¶
search_query: Optional[SearchQuery] = None
The SearchQuery (OpenSearch _search body) to execute against the index.
response_parts
class-attribute
instance-attribute
¶
response_parts: Optional[List[SearchQueryPart]] = field(default_factory=list)
Optional list of additional response parts beyond default HITS.
hits
class-attribute
instance-attribute
¶
Response: matching documents. Populated after send_job_and_wait_async().
total_hits
class-attribute
instance-attribute
¶
Response: total number of matching documents. Populated when SearchQueryPart.TOTAL_HITS is requested.
select_columns
class-attribute
instance-attribute
¶
select_columns: Optional[List[SelectColumn]] = field(default_factory=list)
Response: columns represented in each hit's fields, in SELECT-clause order. Populated when SearchQueryPart.SELECT_COLUMNS is requested.
aggregation_results
class-attribute
instance-attribute
¶
Response: the raw OpenSearch
aggregations response,
with field references rewritten back to column names. Populated whenever the
request supplied search_query.aggregations. Kept as an opaque JSON object
because its shape mirrors whichever aggregations were requested.
synapseclient.models.SearchAutocompleteRequest
dataclass
¶
Body of a synchronous autocomplete request against a SearchIndex. The
autocomplete endpoint allowlists only query (restricted to
prefix,
match_phrase_prefix,
or match_bool_prefix)
and _source.
Represents a Synapse SearchAutocompleteRequest.
Source code in synapseclient/models/search_management.py
1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 | |
Attributes¶
search_index_id
class-attribute
instance-attribute
¶
The ID of the SearchIndex entity to query.
query
class-attribute
instance-attribute
¶
Required. The top-level
Query DSL clause; restricted
server-side to prefix, match_phrase_prefix, or
match_bool_prefix.
source
class-attribute
instance-attribute
¶
source: Optional[SourceFilter] = None
Optional. Source filter;
same shape as SearchQuery.source. Serialized as _source.
synapseclient.models.SearchHit
dataclass
¶
A single matching document in a SearchQueryResults response.
Represents a Synapse SearchHit.
Source code in synapseclient/models/search_management.py
1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 | |
Attributes¶
row_id
class-attribute
instance-attribute
¶
The row ID from the source table.
row_version
class-attribute
instance-attribute
¶
The row version from the source table.
score
class-attribute
instance-attribute
¶
The relevance score for this hit.
synapseclient.models.SearchFieldValue
dataclass
¶
A name/value pair returned in a SearchHit's fields.
Represents a Synapse SearchFieldValue.
Source code in synapseclient/models/search_management.py
1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 | |
synapseclient.models.SearchHighlight
dataclass
¶
A per-field highlight payload on a SearchHit.
Represents a Synapse SearchHighlight.
Source code in synapseclient/models/search_management.py
1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 | |
Attributes¶
synapseclient.models.protocols.search_index_protocol.SearchIndexSynchronousProtocol
¶
Bases: Protocol
Protocol defining the synchronous interface for SearchIndex operations.
Source code in synapseclient/models/protocols/search_index_protocol.py
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 | |
Methods:¶
store
¶
Store metadata about a SearchIndex including the annotations. Creates
a new SearchIndex if id is not set, or updates the existing one
otherwise. defining_sql must be set before calling this.
| PARAMETER | DESCRIPTION |
|---|---|
dry_run
|
If True, will not actually store the SearchIndex but will log to the console what would be created or updated.
TYPE:
|
synapse_client
|
If not passed in and caching was not disabled by
|
| RETURNS | DESCRIPTION |
|---|---|
Self
|
Itself. |
| RAISES | DESCRIPTION |
|---|---|
ValueError
|
If |
Create a new SearchIndex.
from synapseclient import Synapse
from synapseclient.models import SearchIndex
syn = Synapse()
syn.login()
index = SearchIndex(
name="My Search Index",
parent_id="syn12345",
# syn67890 must be a table or a view;
defining_sql="SELECT * FROM syn67890",
)
index = index.store()
print(f"Created SearchIndex: {index.id}")
Source code in synapseclient/models/protocols/search_index_protocol.py
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 | |
get
¶
Get the metadata about the SearchIndex from Synapse. Either id, or
name and parent_id, must be set before calling this.
| PARAMETER | DESCRIPTION |
|---|---|
include_activity
|
If True, will include the provenance activity on the returned SearchIndex.
TYPE:
|
synapse_client
|
If not passed in and caching was not disabled by
|
| RETURNS | DESCRIPTION |
|---|---|
Self
|
Itself, populated from the Synapse response. |
Get a SearchIndex by ID.
from synapseclient import Synapse
from synapseclient.models import SearchIndex
syn = Synapse()
syn.login()
index = SearchIndex(id="syn12345").get()
print(index.name, index.defining_sql)
Source code in synapseclient/models/protocols/search_index_protocol.py
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 | |
delete
¶
Delete the SearchIndex from Synapse. id must be set before calling
this.
| PARAMETER | DESCRIPTION |
|---|---|
synapse_client
|
If not passed in and caching was not disabled by
|
Delete a SearchIndex by ID.
from synapseclient import Synapse
from synapseclient.models import SearchIndex
syn = Synapse()
syn.login()
SearchIndex(id="syn12345").delete()
Source code in synapseclient/models/protocols/search_index_protocol.py
103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 | |
query
¶
query(search_query: SearchQuery, response_parts: Optional[List[SearchQueryPart]] = None, *, job_timeout: int = 600, synapse_client: Optional[Synapse] = None) -> SearchIndexQuery
Query this search index. Unlike a SQL-backed Table, a SearchIndex is queried with the OpenSearch Query DSL carried by a SearchQuery — not with Synapse SQL. See Query for the supported clause kinds.
| PARAMETER | DESCRIPTION |
|---|---|
search_query
|
The OpenSearch
TYPE:
|
response_parts
|
Additional response parts to request beyond the default hits, such as the total hit count or the select columns.
TYPE:
|
job_timeout
|
The maximum amount of time to wait for the query job to
complete before raising a
TYPE:
|
synapse_client
|
If not passed in and caching was not disabled by
|
| RETURNS | DESCRIPTION |
|---|---|
SearchIndexQuery
|
The completed SearchIndexQuery, carrying the |
| RAISES | DESCRIPTION |
|---|---|
ValueError
|
If the |
Query an index for documents mentioning "alzheimer".
from synapseclient import Synapse
from synapseclient.models import SearchIndex, SearchQuery, SearchQueryPart
from synapseclient.models.search_dsl import Query
syn = Synapse()
syn.login()
results = SearchIndex(id="syn12345").query(
search_query=SearchQuery(
query=Query(match={"title": {"query": "alzheimer"}}),
size=10,
),
response_parts=[SearchQueryPart.TOTAL_HITS],
)
print(results.total_hits)
for hit in results.hits:
print(hit.row_id, hit.fields)
Source code in synapseclient/models/protocols/search_index_protocol.py
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 | |
autocomplete
¶
autocomplete(query: Query, source: Optional[SourceFilter] = None, *, synapse_client: Optional[Synapse] = None) -> List[SearchHit]
Run a synchronous autocomplete search against this index. The
autocomplete endpoint allowlists only prefix-style queries
(prefix,
match_phrase_prefix,
or match_bool_prefix)
and caps results at 8.
| PARAMETER | DESCRIPTION |
|---|---|
query
|
The top-level OpenSearch Query DSL
clause -- see Query;
restricted server-side to
TYPE:
|
source
|
Optional source filter selecting which columns are returned on each hit.
TYPE:
|
synapse_client
|
If not passed in and caching was not disabled by
|
| RETURNS | DESCRIPTION |
|---|---|
List[SearchHit]
|
The matching SearchHits, capped at 8. |
| RAISES | DESCRIPTION |
|---|---|
ValueError
|
If the |
Autocomplete titles beginning with "alz".
from synapseclient import Synapse
from synapseclient.models import SearchIndex
from synapseclient.models.search_dsl import Query
syn = Synapse()
syn.login()
index = SearchIndex(id="syn12345")
hits = index.autocomplete(
query=Query(match_phrase_prefix={"title": {"query": "alz"}}),
)
for hit in hits:
print(hit.row_id, hit.fields)
Source code in synapseclient/models/protocols/search_index_protocol.py
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 | |
OpenSearch query DSL¶
synapseclient.models.search_dsl.Query
¶
Bases: TypedDict
A single OpenSearch query DSL clause. Exactly one of the keys below may be set -- the set key names the clause kind. See query vs. filter context and term-level vs. full-text queries.
The field-keyed leaf clauses (match, term, range, ...) are maps whose
key is the column name and whose value is the per-field options object --
only the long form is accepted (e.g. {"match": {"title": {"query": "x"}}},
not the {"match": {"title": "x"}} shorthand). The compound clauses
(bool, dis_max, ...) nest further Query DSL clauses recursively.
Source code in synapseclient/models/search_dsl.py
498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 | |
Attributes¶
match
instance-attribute
¶
match: Dict[str, MatchFieldOptions]
A match
full-text clause. Map of column name to its match options.
match_phrase
instance-attribute
¶
match_phrase: Dict[str, MatchPhraseFieldOptions]
A match_phrase
clause. Map of column name to its phrase options.
match_phrase_prefix
instance-attribute
¶
match_phrase_prefix: Dict[str, MatchPhrasePrefixFieldOptions]
A match_phrase_prefix
clause. Map of column name to its options.
match_bool_prefix
instance-attribute
¶
match_bool_prefix: Dict[str, MatchBoolPrefixFieldOptions]
A match_bool_prefix
clause. Map of column name to its options.
term
instance-attribute
¶
term: Dict[str, TermFieldOptions]
A term
term-level clause. Map of column name to its term options.
range
instance-attribute
¶
range: Dict[str, RangeFieldOptions]
A range
term-level clause. Map of column name to its range bounds.
prefix
instance-attribute
¶
prefix: Dict[str, PrefixFieldOptions]
A prefix
term-level clause. Map of column name to its prefix options.
wildcard
instance-attribute
¶
wildcard: Dict[str, WildcardFieldOptions]
A wildcard
term-level clause. Map of column name to its wildcard options.
fuzzy
instance-attribute
¶
fuzzy: Dict[str, FuzzyFieldOptions]
A fuzzy
term-level clause. Map of column name to its fuzzy options.
terms
instance-attribute
¶
A terms
term-level clause (matches any of several exact values). Field-keyed:
{"terms": {"<column>": [v1, v2], "boost": 1.0}}. The cross-index
terms-lookup form is rejected. Untyped because the column name is itself a
key alongside the fixed option keys, which a TypedDict cannot express; the
same allowlist is enforced server-side.
simple_query_string
instance-attribute
¶
simple_query_string: SimpleQueryStringQuery
A simple_query_string
full-text clause.
bool
instance-attribute
¶
bool: BoolQuery
A bool
compound clause -- combines sub-clauses with boolean logic.
constant_score
instance-attribute
¶
constant_score: ConstantScoreQuery
A constant_score
compound clause.