docs-site/content/0.24.0/api/synonyms.md
The synonyms feature allows you to define search terms that should be considered equivalent. For eg: 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:
One-way synonyms: Defining the words iphone and android as one-way synonyms of smart phone will cause searches for smart phone to return documents containing iphone or android or both.
Multi-way synonyms: Defining the words blazer, coat and jacket as 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 with blazer or coat or jacket are returned).
synonym = {
"synonyms": ["blazer", "coat", "jacket"]
}
// Creates/updates a synonym called `coat-synonyms` in the `products` collection
client.collections('products').synonyms().upsert('coat-synonyms', synonym)
$synonym = [
"synonyms" => ["blazer", "coat", "jacket"]
];
# Creates/updates a synonym called `coat-synonyms` in the `products` collection
$client->collections['products']->synonyms->upsert('coat-synonyms', $synonym);
synonym = {
"synonyms": ["blazer", "coat", "jacket"]
}
# Creates/updates a synonym called `coat-synonyms` in the `products` collection
client.collections['products'].synonyms.upsert('coat-synonyms', synonym)
synonym = {
"synonyms" => ["blazer", "coat", "jacket"]
}
# Creates/updates a synonym called `coat-synonyms` in the `products` collection
client.collections['products'].synonyms.upsert('coat-synonyms', synonym)
final synonym = {
"synonyms": ["blazer", "coat", "jacket"]
};
// Creates/updates a synonym called `coat-synonyms` in the `products` collection
await client.collection('products').synonyms.upsert('coat-synonyms', synonym);
SearchSynonymSchema synonym = new SearchSynonymSchema();
synonym.addSynonymsItem("blazer").addSynonymsItem("coat").addSynonymsItem("jacket");
// Creates/updates a synonym called `coat-synonyms` in the `products` collection
client.collections("products").synonyms().upsert("coat-synonyms", synonym);
let synonymSchema = SearchSynonymSchema(synonyms: ["blazer", "coat", "jacket"])
// Creates/updates a synonym called `coat-synonyms` in the `products` collection
let (searchSynonym, response) = try await client.collection(name: "products").synonyms().upsert(id: "coat-synonyms", synonymSchema)
curl "http://localhost:8108/collections/products/synonyms/coat-synonyms" -X PUT \
-H "Content-Type: application/json" \
-H "X-TYPESENSE-API-KEY: ${TYPESENSE_API_KEY}" -d '{
"synonyms": ["blazer", "coat", "jacket"]
}'
{
"id": "coat-synonyms",
"synonyms": ["blazer", "coat", "jacket"]
}
synonym = {
"root": "smart phone",
"synonyms": ["iphone", "android"]
}
// Creates/updates a synonym called `smart-phone-synonyms` in the `products` collection
client.collections('products').synonyms().upsert('smart-phone-synonyms', synonym)
$synonym = [
'root' => 'smart phone',
'synonyms' => ['iphone', 'android'],
];
// Creates/updates a synonym called `smart-phone-synonyms` in the `products` collection
$client->collections['products']->synonyms->upsert('smart-phone-synonyms', $synonym);
synonym = {
"root": "smart phone",
"synonyms": ["iphone", "android"]
}
# Creates/updates a synonym called `smart-phone-synonyms` in the `products` collection
client.collections('products').synonyms.upsert('smart-phone-synonyms', synonym)
synonym = {
"root": "smart phone",
"synonyms": ["iphone", "android"]
}
# Creates/updates a synonym called `smart-phone-synonyms` in the `products` collection
client.collections('products').synonyms.upsert('smart-phone-synonyms', synonym)
final synonym = {
"root": "smart phone",
"synonyms": ["iphone", "android"]
};
// Creates/updates a synonym called `smart-phone-synonyms` in the `products` collection
await client.collection('products').synonyms.upsert('smart-phone-synonyms', synonym);
SearchSynonymSchema synonym = new SearchSynonymSchema();
synonym.addSynonymsItem("iphone").addSynonymsItem("android");
synonym.root("smart phone");
// Creates/updates a synonym called `smart-phone-synonyms` in the `products` collection
client.collections("products").synonyms().upsert("smart-phone-synonyms", synonym);
let synonymSchema = SearchSynonymSchema(
root: "smart phone",
synonyms: ["iphone", "android"]
)
// Creates/updates a synonym called `smart-phone-synonyms` in the `products` collection
let (searchSynonym, response) = try await client.collection(name: "products").synonyms().upsert(id: "smart-phone-synonyms", synonymSchema)
curl "http://localhost:8108/collections/products/synonyms/smart-phone-synonyms" -X PUT \
-H "Content-Type: application/json" \
-H "X-TYPESENSE-API-KEY: ${TYPESENSE_API_KEY}" -d '{
"root": "smart phone",
"synonyms": ["iphone", "android"]
}'
{
"id":"smart-phone-synonyms",
"root":"smart phone",
"synonyms": ["iphone", "android"],
"locale": "",
"symbols_to_index": []
}
PUT ${TYPESENSE_HOST}/collections/:collection/synonyms/:id
| Parameter | Required | Description |
|---|---|---|
| synonyms | yes | Array of words that should be considered as synonyms. |
| root | no | For 1-way synonyms, indicates the root word that words in the synonyms parameter map to. |
| locale | no | Locale for the synonym, leave blank to use the standard tokenizer. |
| 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. |
We can retrieve a single synonym.
<Tabs :tabs="['JavaScript','PHP','Python','Ruby','Dart','Java','Swift','Shell']"> <template v-slot:JavaScript>client.collections('products').synonyms('coat-synonyms').retrieve()
$client->collections['products']->synonyms['coat-synonyms']->retrieve();
client.collections('products').synonyms['coat-synonyms'].retrieve
client.collections('products').synonyms['coat-synonyms'].retrieve
await client.collection('products').synonym('coat-synonyms').retrieve();
SearchSynonym searchSynonym = client.collections("products").synonyms("coat-synonyms").retrieve();
let (searchSynonym, response) = try await client.collection(name: "products").synonyms().retrieve(id: "coat-synonyms")
curl -H "X-TYPESENSE-API-KEY: ${TYPESENSE_API_KEY}" "http://localhost:8108/collections/products/synonyms"
{
"id": "coat-synonyms",
"root":"",
"synonyms": ["blazer", "coat", "jacket"]
}
GET ${TYPESENSE_HOST}/collections/:collection/synonyms/:id
List all synonyms associated with a given collection.
<Tabs :tabs="['JavaScript','PHP','Python','Ruby','Dart','Java','Swift','Shell']"> <template v-slot:JavaScript>client.collections('products').synonyms().retrieve()
$client->collections['products']->synonyms->retrieve();
client.collections['products'].synonyms.retrieve()
client.collections['products'].synonyms.retrieve
await client.collection('products').synonyms.retrieve();
SearchSynonymsResponse searchSynonymsResponse = client.collections("products").synonyms().retrieve();
let (searchSynonyms, response) = try await client.collection(name: "products").synonyms().retrieve()
curl -H "X-TYPESENSE-API-KEY: ${TYPESENSE_API_KEY}" \
"http://localhost:8108/collections/products/synonyms"
{
"synonyms": [
{
"id": "coat-synonyms",
"root": "",
"synonyms": ["blazer", "coat", "jacket"]
}
]
}
GET ${TYPESENSE_HOST}/collections/:collection/synonyms
Delete a synonym associated with a collection.
<Tabs :tabs="['JavaScript','PHP','Python','Ruby','Dart','Java','Swift','Shell']"> <template v-slot:JavaScript>client.collections('products').synonyms('coat-synonyms').delete()
$client->collections['products']->synonyms['coat-synonyms']->delete();
client.collections['products'].synonyms['coat-synonyms'].delete()
client.collections['products'].synonyms['coat-synonyms'].delete
await client.collection('products').synonym('coat-synonyms').delete();
SearchSynonym searchSynonym = client.collections("products").synonyms("coat-synonyms").delete();
let (searchSynonym, response) = try await client.collection(name: "products").synonyms().delete(id: "coat-synonyms")
curl "http://localhost:8108/collections/products/synonyms/coat-synonyms" -X DELETE \
-H "X-TYPESENSE-API-KEY: ${TYPESENSE_API_KEY}"
{
"id": "coat-synonyms"
}
DELETE ${TYPESENSE_HOST}/collections/:collection/synonyms/:id