Skip to main content

Synonyms

This page is for the practitioner who wants to manage SAND search synonyms from code. All snippets below assume svi is an authenticated SVIClient.

Before this example​

Synonym lists are deployment-wide. Before you push changes in a production deployment, agree on a source of truth (often a file in version control) and prefer the sync(...) flow for safety.

The SAND server understands two encodings, both as a single comma-separated string:

  • Expansion. "bound,hop,leap,jump" makes all four terms equivalent at index time.
  • Contraction. "bound,hop,leap=>jump" rewrites the source terms to the target term.

The SDK constructs both for you; you should not need to hand-write either string.

Synonym writes are slow

Every write makes SAND rebuild the analyzers that depend on the synonym list before it answers. Measured against a live tenant on 2026-09-16, a single add took 48.0 s and a remove took 47.7 s. That outlives the SDK's 30 s client-wide default, so each write method takes its own timeout=, defaulting to 300 s. Lower it only if you know your list is small.

Construct synonyms​

from sasvi.admin import Synonym

expansion = Synonym.expansion(["bound", "hop", "leap", "jump"])
contraction = Synonym.contraction(sources=["bound", "hop", "leap"], target="jump")

print(expansion.kind, expansion.terms)
print(contraction.kind, contraction.target)

Why this layer matters:

  • The SDK validates the terms: no empty strings, no duplicates, no embedded , or =>.
  • Synonym exposes .kind, .sources and .target, so calling code never parses the encoded string itself.
  • Writing the wrong format was a silent indexing bug. The typed builder makes it impossible.

List current synonyms​

for synonym in svi.admin.synonyms.list():
print(synonym.kind, synonym.terms)

Replace the entire list​

replace(...) is the underlying SAND verb. It overwrites the whole synonym list in one call.

svi.admin.synonyms.replace([
Synonym.expansion(["car", "auto", "automobile"]),
Synonym.contraction(sources=["mister", "mr"], target="mr."),
])

svi.admin.synonyms.clear() empties the list.

sync(...) diffs the current server state against a desired list and applies the change. With dry_run=True you get the plan without writing anything.

desired = [
Synonym.expansion(["car", "auto", "automobile"]),
Synonym.contraction(sources=["mister", "mr"], target="mr."),
]
plan = svi.admin.synonyms.sync(desired, dry_run=True)
print("add:", [item.terms for item in plan.added])
print("remove:", [item.terms for item in plan.removed])

if not plan.is_noop:
svi.admin.synonyms.sync(desired)

This is the right shape for CI-driven publishing: declare desired state in a file, run a dry-run against staging, and apply on merge.

Add or remove one entry​

These helpers wrap a read-modify-write. They are handy for one-off edits, but they race with concurrent writers, because the underlying API replaces the whole list.

entry = Synonym.expansion(["truck", "lorry"])
already_there = entry.terms in {syn.terms for syn in svi.admin.synonyms.list()}
svi.admin.synonyms.add(entry)

add does nothing when the entry exists, and remove deletes it whoever added it. If you add an entry to try something out, remove it only when you added it:

if not already_there:
svi.admin.synonyms.remove(entry)

For programmatic publishing prefer sync(...); for one-off interactive edits these are fine.

A synonym list on its own changes no search result. A search finds a record through a synonym only when all four of these hold:

  1. The synonym list holds the entry. Use add, replace or sync above.
  2. Both synonym settings are on. synonymSearchEnabled and synonymDefaultSearchEnabled must both be true. On a live tenant (2026-10-20), with only synonymSearchEnabled on, every synonym-mode query found nothing; with both on, it found the synonym match.
  3. The text field has the synonym feature. Add "synonym" to the field's options.features in the entity's SAND type configuration.
  4. The query asks for synonym mode. Pass modes=["synonym"] to Search.text, Q.text or Q.safe_text. The SDK sends ["standard", "synonym"].

The SDK has no helper for the settings; send them with svi.custom.request. This changes search behaviour for every user of the tenant:

for name in ("synonymSearchEnabled", "synonymDefaultSearchEnabled"):
response = svi.custom.request(
"PUT",
f"/svi-sand/admin/settings/{name}",
json={"name": name, "value": True},
headers={"Content-Type": "application/vnd.sas.sand.setting+json"},
)
response.raise_for_status()

Add the field feature through admin.search_types. The update sends the If-Match that SAND requires:

record = svi.admin.search_types.get(name="<your-entity-type>")
body = record.raw_dict()
features = (
body["fields"]["<your-text-field>"].setdefault("options", {}).setdefault("features", ["search"])
)
if "synonym" not in features:
features.append("synonym")
svi.admin.search_types.update(name="<your-entity-type>", body=body, if_match=record.etag)

Reindex the entity type after every change to a setting, a field feature or the synonym list. SAND applies all three when it builds the index. On a live tenant (2026-10-20), a synonym added after a record was indexed found nothing, even after the record was saved again. After a scoped reindex of the type it found the record within seconds. The reindex recipe shows the scoped reindex: ensure_index(..., searchable=False), start_for(entities=[...]), then make_searchable(...).

caution

On a live tenant (2026-10-20), turning on synonymDefaultSearchEnabled or phoneticDefaultSearchEnabled made the alert service fail to build the request for a prompted disposition. The error read "The query contains syntax errors" on /svi-alert/alertDispositions/{id}/request. With both default settings off, the same request worked. Test prompted dispositions on your tenant before you turn these settings on in production.

Then search:

svi.search.types("<your-entity-type>").text("<a-synonym>", modes=["synonym"]).count()

Phonetic search works the same way. Turn on phoneticSearchEnabled and phoneticDefaultSearchEnabled, add the phonetic feature to the text field, reindex, and pass modes=["phonetic"]. On the live tenant, a query for smyth then found a record holding smith; without the mode it found nothing.

Why the SDK always adds standard​

SAND accepts a synonym or phonetic mode on its own, but on a live tenant (2026-10-20) that narrowed the search:

Query modesFields searchedStemming and wildcards
["standard", "synonym"]All searchable fields, plus synonymsYes
["synonym"]Only fields with the synonym featureYes
["standard", "phonetic"]All searchable fields, plus sound-alikesYes
["phonetic"]Only fields with the phonetic featureNo (run missed "running", john* matched nothing)

A mode on its own never found a record that standard plus that mode missed, so the SDK puts "standard" first whenever you pass modes=. For a synonym-only or phonetic-only query, pass a raw query mapping to Search.query.

The VI user interface sends "modes": ["standard", "synonym"] from its synonym search option. SAND's published OpenAPI schema does not list modes on the text query yet; see the modes= reference.

See also​