Bulk operations
This page is for the practitioner who needs to upsert many documents or relationship links in
one request, or submit a server-side async job. All snippets below assume svi is an
authenticated SVIClient.
Before this example
The values in this page are placeholders, not universal VI names. Before you adapt a snippet, use Discover your environment to find:
- a real entity type name for
"<your-entity-type>" - real document ids and field names for the records you upsert
- the real relationship type name behind
"<your-relationship-type>"
Bulk upsert documents
Use svi.documents.bulk_upsert when you have many documents to create or update in one call. Each
input can be a plain mapping or a BaseVIEntity instance.
A row without a top-level id creates a document:
results = svi.documents.bulk_upsert([
{
"objectTypeName": "<your-entity-type>",
"fieldValues": {"<your-status-field>": "open"},
},
])
for row in results:
if row.succeeded:
print("ok", row.operation, row.id)
else:
print("failed", row.id, row.error)
What the call guarantees:
- the SDK sends one request and returns one result row per input
- per-row failures surface as rows with a non-
None.error; the call does not raise - the result rows expose
id,entity_type,operation,error,succeeded - a failure of the request itself raises
BulkRequestError, which keeps what was already written
Recovering from a failed request
With chunk_size, each chunk is its own request, so a failure partway through has
already written the earlier chunks. BulkRequestError carries that state:
from sasvi.errors import BulkRequestError
try:
results = svi.documents.bulk_upsert(documents, chunk_size=250)
except BulkRequestError as exc:
written = [row.id for row in exc.completed] # confirmed by the server
resume_from = exc.input_offset # first unconfirmed input
if exc.outcome_unknown:
# The failed request may still have been applied: read those inputs
# back before resending, because creates are not idempotent.
...
else:
results = svi.documents.bulk_upsert(documents[resume_from:], chunk_size=250)
outcome_unknown is True for a timeout, a 5xx, an unreadable response, or a
response that answered with fewer rows than the chunk it was sent.
Updating in bulk is a read-modify-write
A row with a top-level id is an update, and the server is strict about its
shape. fieldValues must carry the document's existing audit fields back:
created_at_dttm, last_updated_at_dttm, version, and the entity's primary
key. Read the document first and mutate its own values:
document = svi.documents.get(
entity_type_name="<your-entity-type>",
document_id="<your-document-id>",
)
field_values = dict(document.fields)
field_values["<your-status-field>"] = "closed"
results = svi.documents.bulk_upsert([
{
"objectTypeName": "<your-entity-type>",
"id": "<your-document-id>",
"fieldValues": field_values,
},
])
Two ways to get this wrong, and what the server says:
- sending only the fields you want to change fails that row with
DH6916("null" is not a valid timestamp value), because the audit fields are missing - putting the primary key in
fieldValueswithout a top-levelidis read as an insert and fails withDH5118(the auto-generated key must be null on insert)
If you prefer fail-fast semantics, call raise_on_errors after the bulk call:
from sasvi.documents import raise_on_errors
raise_on_errors(svi.documents.bulk_upsert([...]))
Masked documents
Pass masked=True when the items are masked-document representations. The SDK sends the
appropriate Content-Item-Type header.
svi.documents.bulk_upsert(masked_items, masked=True)
Bulk upsert relationship links
Use svi.documents.bulk_upsert_links for the matching mass-link path. Each item is a link in the
Data Hub DocumentLink shape. The SDK adds "@type": "DocumentLink" when an item lacks it.
results = svi.documents.bulk_upsert_links([
{
"relationshipTypeName": "<your-relationship-type>",
"fromObjectTypeName": "<your-entity-type>",
"fromObjectId": "<your-from-id>",
"toObjectTypeName": "<your-other-entity-type>",
"toObjectId": "<your-to-id>",
},
])
print(results[0].succeeded, results[0].id)
An item with no id creates a link and needs all five of those fields. An item with an id
updates that link. Optional fieldValues set the relationship's own fields.
The link result rows expose id, from_entity_type, from_id, to_entity_type, to_id,
relationship_type_name, operation, error, and succeeded. The result rows use different
key names from the request: send fromObjectTypeName, not fromEntityType. A create item that
uses the result-row names is rejected before anything is sent.
Reindex after a bulk link load
Links written through /links/bulk are stored in Data Hub straight away, but they may not reach
the search graph. Until a reindex runs, svi.search and the VI network view and workspaces can
show the linked records with no edges. The records themselves were indexed on time; only the
links were missing. After a bulk link load, run a scoped index of the relationship types you
loaded and their endpoint entities:
job = svi.admin.indexing.start_for(
entities=["<your-entity-type>", "<your-other-entity-type>"],
relationships=[("<your-relationship-type>", "<your-entity-type>")], # (name, from entity)
)
print(job.id, job.status)
When the job has finished, check one linked record with svi.search and around_graph() (see
the Search guide): its edges should now appear. The re-index recipe covers
scoped and full jobs.
Submit an async job
Use svi.documents.submit_async_job when the work belongs to a long-running server-side task that
the Visual Investigator service executes asynchronously.
job = svi.documents.submit_async_job(
parameters={"<your-param-key>": "<your-param-value>"},
name="<your-job-name>",
description="nightly recalculation",
tags=["bulk"],
)
print(job.id, job.status)
The returned JobHandle exposes id, name, status, message, started_by, started_at,
and ended_at populated from the submission response.
For search-index jobs (SEARCH_INDEX_LOADER) use svi.admin.indexing instead: it builds the
typed job from an IndexingRequest, keeps lists and booleans as JSON lists and booleans on the
wire, and pairs the job with the SAND index lifecycle. See
Search indexing and the
re-index recipe. submit_async_job stays the generic
escape hatch for any other job type; note that it JSON-encodes non-string parameter values.
Status polling and cancel have no published OpenAPI specification (they live on the VI job
monitor and indexer services), so the SDK exposes no refresh() on JobHandle and calls
neither endpoint. Follow job progress on the Visual Investigator admin Jobs page.
See also
- The Configuration management page covers partial-failure handling and the trade-offs between single-call upserts and per-record loops.
- The Documents reference lists the full method signatures.