From search to bound objects
A SearchHit is a lightweight view: id, type, label, projected field values, raw
payload. To call methods on a hit (refresh, update, disposition, walk relationships),
hydrate it into a bound Document or Alert.
import sasvi
client = sasvi.SVIClient.from_env()
results = client.search.types("alerts").text("invoice").summary(limit=20)
for hit in results:
bound = hit.hydrate()
if bound is None:
continue # hit referenced a deleted document
bound.update_fields(reviewed_by="analyst.alpha")
hit.hydrate() returns an Alert when hit.entity_type_name is "alerts" and a
Document otherwise. It costs one extra get() per call.
Async fanout for many hits
hit.hydrate() is synchronous, so calling it inside an event loop blocks it. Use
hydrate_all_async() on the page. It reads eight hits at a time, returns them in hit
order with None for a deleted object, and raises the first failure only after every
read has finished:
results = (
await client.search
.types("alerts", "Customer")
.text("invoice")
.summary_async(
limit=200,
)
)
bounds = await results.hydrate_all_async()
await hit.hydrate_async() resolves one hit.
For a different bound or per-hit error handling, run your own fan-out. See Async fanout pattern.
Skip hydration when fields are enough
If you only need the projected fields, work directly with the SearchHit:
results = (
client.search
.types("alerts")
.text("invoice")
.fields(
"display_label",
"current_score",
"queue_id",
entity_name="alerts",
)
.summary(limit=200)
)
for hit in results:
print(hit["display_label"], hit["current_score"], hit["queue_id"])
SearchHit is Mapping-shaped, so hit["x"], hit.get("x", default), "x" in hit,
iter(hit), and len(hit) all work.
Convert to a stable reference
ref = hit.to_ref()
# SearchResultRef(entity_type_name='alerts', document_id='alert-1', label='...')
SearchResultRef is frozen, hashable, and compares equal by
(entity_type_name, document_id), which makes it a good dict key when deduplicating
across several searches. Assigning to either of those fields raises, so a stored key
cannot become unreachable.
SearchHit hashes the same pair and works as a key too. Its entity_type_name and
document_id are read-only for the same reason; label, fields and raw are not.
Go further
- Search guide. The fluent builder.
- Search DSL reference. Every
QandFoperator.