Skip to content
WP Engine Developers

Using the Synonyms API

The Synonyms API allows you to create and manage synonym rules that improve search result relevancy. Synonyms are automatically applied to all searches once configured.

Clients calling the API must include an authentication header containing a valid write-access token. See the Getting Started guide for details on obtaining your tokens from the WP Engine User Portal.

Authorization: Bearer {ACCESS_TOKEN}

There are two supported formats for defining synonym rules: equivalent (bidirectional) and explicit (one-way) mappings.

Comma-separated terms are treated as equivalent in all directions.

mutation EquivalentSynonyms {
config {
synonyms {
saveRule(synonyms: "laptop, notebook, computer") {
code
success
rule {
id
synonyms
}
}
}
}
}

Use the => operator to map multiple terms to a single canonical term. All terms on the left side of => map one-way to the term(s) on the right.

mutation ExplicitMapping {
config {
synonyms {
saveRule(synonyms: "spinach, potato => cucumber") {
code
success
rule {
id
synonyms
}
}
}
}
}

In the example above, both “spinach” and “potato” map to “cucumber”:

  • Searching for “spinach” will match documents containing “cucumber”
  • Searching for “potato” will match documents containing “cucumber”
  • Searching for “cucumber” will NOT match documents containing “spinach” or “potato”

Create a new synonym rule using the saveRule mutation. The ID is auto-generated if not provided.

mutation CreateSynonym {
config {
synonyms {
saveRule(synonyms: "laptop, notebook, computer") {
code
success
message
rule {
id
synonyms
}
}
}
}
}
Response
{
"data": {
"config": {
"synonyms": {
"saveRule": {
"code": "200",
"success": true,
"message": "Synonym rule saved successfully",
"rule": {
"id": "local-abc123",
"synonyms": "laptop, notebook, computer"
}
}
}
}
}
}

Update an existing synonym rule by providing its ID.

mutation UpdateSynonym {
config {
synonyms {
saveRule(id: "laptops", synonyms: "laptop, notebook, computer, pc") {
code
success
message
rule {
id
synonyms
}
}
}
}
}

Retrieve all synonym rules. Pagination parameters (offset and limit) are optional.

query ListSynonyms {
config {
synonyms {
rules(offset: 0, limit: 10) {
total
offset
limit
rules {
id
synonyms
}
}
}
}
}
Response
{
"data": {
"config": {
"synonyms": {
"rules": {
"total": 2,
"offset": 0,
"limit": 10,
"rules": [
{
"id": "local-d2w8az",
"synonyms": "laptop, notebook, computer"
},
{
"id": "local-p2uj1m",
"synonyms": "spinach, potato => cucumber"
}
]
}
}
}
}
}

Retrieve a specific synonym rule by its ID.

query GetSynonym {
config {
synonyms {
rule(id: "local-d2w8az") {
id
synonyms
}
}
}
}
Response
{
"data": {
"config": {
"synonyms": {
"rule": {
"id": "local-d2w8az",
"synonyms": "laptop, notebook, computer"
}
}
}
}
}
mutation DeleteSynonym {
config {
synonyms {
deleteRule(id: "local-p2uj1m") {
code
success
message
}
}
}
}
ConstraintValue
Max words per rule32
Min terms required2
Access levelFULL
Section titled “Complete GraphQL Example (Create Synonyms / Index and Search)”
mutation CreateSynonyms {
config {
synonyms {
laptop: saveRule(synonyms: "laptop, notebook, computer") {
code
success
message
rule {
id
synonyms
}
}
brands: saveRule(synonyms: "iphone, ipad => apple") {
code
success
message
rule {
id
synonyms
}
}
}
}
}
Response
{
"data": {
"config": {
"synonyms": {
"laptop": {
"code": "200",
"success": true,
"message": "Synonym rule saved successfully",
"rule": {
"id": "local-abc123",
"synonyms": "laptop, notebook, computer"
}
},
"brands": {
"code": "200",
"success": true,
"message": "Synonym rule saved successfully",
"rule": {
"id": "local-xyz789",
"synonyms": "iphone, ipad => apple"
}
}
}
}
}
}
mutation IndexProduct($input: DocumentInput!) {
index(input: $input) {
success
document {
id
data
}
}
}
Variables
{
"input": {
"data": {
"ID": 1,
"post_title": "MacBook Pro",
"post_content": "Latest laptop from Apple",
"post_type": "product",
"post_status": "publish"
}
}
}
Response
{
"data": {
"index": {
"success": true,
"document": {
"id": "product:1",
"data": {
"ID": 1,
"post_title": "MacBook Pro",
"post_content": "Latest laptop from Apple",
"post_type": "product",
"post_status": "publish"
}
}
}
}
}

Synonyms automatically apply to all searches.

Search Query
query SearchWithSynonyms {
find(query: "notebook") {
total
documents {
id
data
}
}
}
Response
{
"data": {
"find": {
"total": 1,
"documents": [
{
"id": "product:1",
"data": {
"post_title": "MacBook Pro",
"post_content": "Latest laptop from Apple"
}
}
]
}
}
}

The search for “notebook” matches the document containing “laptop” due to the synonym rule.

mutation ECommerceSynonyms {
config {
synonyms {
laptop: saveRule(synonyms: "laptop, notebook, computer") {
code
success
}
mobile: saveRule(synonyms: "phone, mobile, smartphone") {
code
success
}
brands: saveRule(synonyms: "iphone, ipad => apple") {
code
success
}
}
}
}
{
"data": {
"config": {
"synonyms": {
"saveRule": {
"code": "400",
"success": false,
"message": "Synonym rule exceeds maximum of 32 words"
}
}
}
}
}
{
"data": {
"config": {
"synonyms": {
"saveRule": {
"code": "400",
"success": false,
"message": "Synonym rule must contain at least 2 terms"
}
}
}
}
}
{
"errors": [
{
"message": "Unauthorized: Full access token required",
"extensions": { "code": "UNAUTHORIZED" }
}
]
}

Last updated: