# Synonym Sets
Breaking Change in v30
When you upgrade to v30, all existing collection-specific synonym definitions will be automatically migrated to the new synonym sets format. Your searches will continue working without any hiccups, but you have to use the new API and client methods for reading and writing to the synonym definitions.
The synonym sets feature allows you to define search terms that should be considered equivalent. For example: when you define a synonym for sneaker as shoe, searching for sneaker will now return all records with the word shoe in them, in addition to records with the word sneaker.
Typesense supports two types of synonyms within synonym sets:
One-way synonyms: Defining the words
iphoneandandroidas one-way synonyms ofsmart phonewill cause searches forsmart phoneto return documents containingiphoneorandroidor both.Multi-way synonyms: Defining the words
blazer,coatandjacketas multi-way synonyms will cause searches for any one of those words (eg:coat) to return documents containing at least one of the words in the synonym set (eg: records withblazerorcoatorjacketare returned).
Precedence
When using Synonym Sets and Overrides together, Overrides are handled first since the rules can contain instructions to replace the query. Synonym Sets will then work on the modified query.
Locale-specific synonyms
When a synonym has a locale specified, it will only be applied when searching fields with a matching locale. If no locale is specified for a synonym, it will be applied globally. This helps manage cases where the same word has different meanings across languages.
Phrase Match Queries & Filtering
Synonyms are not triggered when using Phrase Search or Filtering, by design.
So for eg, "Site Reliability" will not return results containing Infrastructure even if they are defined as multi-way synonyms, because of the double quotes around "Site Reliability" which makes it a phrase search. So only documents that contain that exact full phrase are returned, without any synonym matches.
Also, synonyms are only applied to the tokens in the q search parameter, and not to any tokens in the filter_by parameter. For eg, if you define a multi-way synonym for abc <> xyz and use filter_by: title:=abc, it will only match documents where title=abc, not title=xyz, because filtering is designed to be similar to a SQL WHERE condition to do a structured query and synonyms don't apply to filters.
# Create or update a synonym set
# Multi-way synonym
# Sample Response
# One-way synonym
# Sample Response
# Definition
PUT ${TYPESENSE_HOST}/synonym_sets/:synonymSetName
# Arguments
| Parameter | Required | Description |
|---|---|---|
| items | yes | Array of synonym items, where each item contains the synonym definitions. |
| items.id | yes | Unique identifier for the synonym item. |
| items.synonyms | yes | Array of words that should be considered as synonyms. |
| items.root | no | For 1-way synonyms, indicates the root word that words in the synonyms parameter map to. |
| items.locale | no | Locale for the synonym. If specified, the synonym will only be applied when searching a field that has a matching locale. If not specified, the synonym will be applied globally. |
| items.symbols_to_index | no | By default, special characters are dropped from synonyms. Use this attribute to specify which special characters should be indexed as is. |
After creating a synonym set, be sure to link it to a collection. Read more in the documentation on linking synonym sets with collections (opens new window).
# Retrieve a synonym set
We can retrieve a single synonym set.
# Sample Response
# Definition
GET ${TYPESENSE_HOST}/synonym_sets/:synonymSetName
# List all synonym sets
List all synonym sets.
# Sample Response
# Definition
GET ${TYPESENSE_HOST}/synonym_sets
# Delete a synonym set
Delete a synonym set.
# Sample Response
# Definition
DELETE ${TYPESENSE_HOST}/synonym_sets/:synonymSetName
# Linking synonym sets with collections
# While creating the collection
{
"name": "products",
"fields": [
{
"name": "name",
"type": "string"
}
],
"synonym_sets": ["clothing-synonyms", "tech-synonyms"]
}
# Altering an existing collection
curl "http://localhost:8108/collections/products" -X PATCH \
-H "Content-Type: application/json" \
-H "X-TYPESENSE-API-KEY: ${TYPESENSE_API_KEY}"
-d '{
"synonym_sets": ["clothing-synonyms", "tech-synonyms"]
}'
# Using synonym sets in search
Synonym sets can be used in search parameters dynamically. The search operation will look for synonyms in:
- The synonym sets specified in the search parameters
- The synonym sets linked to the collection
For example, if the collection products is linked to clothing-synonyms synonym set, the following search request will use synonyms from clothing-synonyms, tech-synonyms, and electronics-synonyms:
curl "http://localhost:8108/collections/products/documents/search?q=controller&query_by=name&synonym_sets=tech-synonyms,electronics-synonyms" -X GET \
-H "Content-Type: application/json" \
-H "X-TYPESENSE-API-KEY: ${TYPESENSE_API_KEY}"
# Upsert a synonym set item
# Definition
PUT ${TYPESENSE_HOST}/synonym_sets/:synonymSetName/items/:id
# Retrieve a synonym set item
# Definition
GET ${TYPESENSE_HOST}/synonym_sets/:synonymSetName/items/:id
# List all synonym set items
# Definition
GET ${TYPESENSE_HOST}/synonym_sets/:name/items
# Delete a synonym set item
# Definition
DELETE ${TYPESENSE_HOST}/synonym_sets/:synonymSetName/items/:id
# Migration from old synonyms API
Automatic Migration
All existing synonyms from previous versions have been automatically migrated to the new synonym sets format. Each collection's synonyms are now stored in a synonym set with the same name as the collection, postfixed by *_synonyms_index (e.g. products_synonyms_index).
# Key differences:
API Endpoints:
- Old:
/collections/{collection}/synonyms/* - New:
/synonym_sets/*
- Old:
Data Structure:
- Old: Direct synonym objects
- New: Synonym sets containing arrays of synonym items
Collection Association:
- Old: Synonyms were directly associated with collections
- New: Synonym sets are linked to collections via the
synonym_setsfield
Search Usage:
- Old: Synonyms were automatically applied based on collection association
- New: Synonym sets can be dynamically specified in search parameters in addition to collection-linked sets
# Example of migrated data:
Before (v29 and earlier):
{
"id": "coat-synonyms",
"synonyms": ["blazer", "coat", "jacket"]
}
After (v30):
{
"name": "products",
"items": [
{
"id": "coat-synonyms",
"synonyms": ["blazer", "coat", "jacket"]
}
]
}
The new API provides better organization, reusability across collections, and more flexible search-time synonym application.