🎉 Try the public beta of the new docs site at algolia.com/doc-beta! 🎉
API client / Methods / Synonyms
Required API Key: any key with the settings ACL
Method signature
$index->searchSynonyms(string query)

$index->searchSynonyms(string query, [
  'page' => integer,
  'type' => string,
  'hitsPerPage' => integer,
])

We released a new version of the PHP API client in public beta. Read the beta documentation for more information.

We released a new version of the JavaScript API client in public beta. Read the beta documentation for more information.

We released a new version of the Java API client in public beta. Read the beta documentation for more information.

You’re currently reading the JavaScript API client v4 documentation. Check the migration guide to learn how to upgrade from v3 to v4. You can still access the v3 documentation.

You’re currently reading the Ruby API client v2 documentation. Check the migration guide to learn how to upgrade from v1 to v2. You can still access the v1 documentation.

About this method

Get all synonyms that match a query.

Examples

Read the Algolia CLI documentation for more information.

1
2
3
4
5
6
7
8
// Searching for "street" in synonyms and one-way synonyms;
// fetch the second page with 10 hits per page

$results = $index->searchSynonyms("street", [
  'type' => 'synonym, oneWaySynonym',
  'page' => 1,
  'hitsPerPage' => 10
]);

Parameters

Parameter Description
query
type: string
Required

The search query to find synonyms. Use an empty query to browse all the synonyms of an index.

type
type: string|list
default: ""
Optional

Restrict the search to a specific type of synonym. Use an empty string to search all types (default behavior). Multiple types can be specified using a comma-separated list or an array. The allowed types are:

  • synonym
  • oneWaySynonym
  • altCorrection1 or altCorrection2
  • placeholder
page
type: integer
default: 0
Optional

The page to fetch when browsing through several pages of results. This value is zero-based.

hitsPerPage
type: string
default: 100
Required

The number of synonyms to return for each call.

Response

This section shows the JSON response returned by the API. Each API client encapsulates this response inside objects specific to the programming language, so that the actual response might be different. You can view the response by using the getLogs method. Don’t rely on the order of attributes in the response, as JSON doesn’t guarantee the ordering of keys in objects.

JSON format

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
{
  "hits":[
    {
      "type":"synonym",
      "synonyms": [
        "car",
        "vehicle"
      ],
      "objectID":"1513249039298",
      "_highlightResult":{
        "type":{
          "value":"synonym",
          "matchLevel":"none",
          "matchedWords": []
        },
        "synonyms":[
          {
            "value":"<b>c<\/b>ar",
            "matchLevel":"full",
            "fullyHighlighted":false,
            "matchedWords": [
              "c"
            ]
          },
          {
            "value":"vehicle",
            "matchLevel":"none",
            "matchedWords": []
          }
        ]
      }
    }
  ],
  "nbHits":1
}
Field Description
hits
list

A list of hits, where each hit contains the synonym object and a _highlightResult attribute (which contains the highlighted synonym object).

nbHits
integer

Number of hits.

hits ➔ synonym object

Field Description
objectID
string
Required for only some languages

Must contain the same value as the objectId above.

type
string
Required

There are 4 synonym types. The parameter can be one of the following values:

  • synonym
  • oneWaySynonym
  • altCorrection1 or altCorrection2
  • placeholder
synonyms
list
Required if type=synonym or type=oneWaySynonym

A list of synonyms

input
string
Required if type=oneWaySynonym

Defines the synonym. A word or expression, used as the basis for the array of synonyms.

word
string
Required if type=altCorrection1 or type=altCorrection2

A single word, used as the basis for the below array of corrections.

corrections
list
Required if type=altCorrection1 or type=altCorrection2

An list of corrections of the word.

placeholder
string
Required if type=placeholder

A single word, used as the basis for the below array of replacements.

replacements
list
Required if type=placeholder

An list of replacements of the placeholder.

hits ➔ _highlightResult

Field Description
value
string

Markup text with occurrences highlighted. The tags used for highlighting are specified via highlightPreTag and highlightPostTag.

matchLevel
string

Indicates how well the attribute matched the search query. Can be:

  • none (0)
  • partial some)
  • full (all)

The matching relates to the words in the query string not in the searched text of the records.

By “meaningful” we mean: if stop words are removed, they are not taken into account. So if you match everything but stop words (and removeStopWords is enabled), then it’s a full match.

This has nothing to do with prefixes, plurals, synonyms, or typos. No matter how “accurately” a word matches, if it matches, it counts as one.

matchedWords
list

List of words from the query that matched the object.

fullyHighlighted
boolean

Whether the entire attribute value is highlighted.

Did you find this page helpful?