API Reference / API Parameters / similarQuery
Type: string
Engine default: "" (returns all records)
Parameter syntax
'similarQuery' => "query"

Can be used in these methods:

About this parameter

Overrides the query parameter and performs a more generic search to find “similar” results.

Usage notes

  • similarQuery should be constructed from the tags and keywords of the object for which you are trying to find related results.
  • similarQuery isn’t automatically generated. You need to select which keywords you think would be helpful. For example, a similarQuery for a movie could use the genre, principle actors, and tags attributes. After extracting information from those categories, you might end up with a similarQuery that looks like “Romance Comedy Gordon-Levitt NY”.

This parameter changes search settings in several ways:

  • Sets queryType to prefixNone (no prefix match). Matched prefixes aren’t counted as word matches.
  • Sets removeStopWords to true. Stop words like ‘the’, ‘a’, and ‘an’ are removed.
  • Sets words to be the first ranking criteria. The number of exactly matching words is the first ranking criterion.
  • Treats all remaining words (all non-stop words) as optionalWords.
    • Objects that match any word in similarQuery will be returned (OR rather than AND)
    • Since these optional words can yield a high volume of results, you should add filters to your similar searches (as seen in the example).
    • Since similarQuery strings are usually quite long, they’re likely to invoke the caveat behavior of the optionalWords parameter.

Examples

Search with similarQuery

This example demonstrates the construction of a similarQuery from a returned movie object. The following JSON object represents the movie “Fargo”:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
{
  "title": "Fargo",
  "year": 1996,
  "director": "Joel Coen",
  "cast": [
    "Frances McDormand",
    "William H. Macy",
    "Steve Buscemi",
    "Peter Stormare",
    "Harve Presnell"
  ],
  "genres": [
    "Comedy",
    "Drama",
    "Crime"
  ],
  "tags": [
    "black-comedy",
    "americana",
    "coen-brothers",
  ]
  "objectID": "97327292",
  "budget": 7000000
}

From this object, use similarQuery to extract words from genres, cast, and director as a long query string: similarQuery = “Comedy Drama Crime McDormand Macy Buscemi Stormare Presnell Coen” This returns many results because all words are optional. To ensure you only get the best results, filter on release dates within five years of Fargo’s (1991 to 2001): filter = “year:1991 TO 2001”

The following code snippet puts this all together:

1
2
3
4
$results = $index->search('', [
  'similarQuery' => 'Comedy Drama Crime McDormand Macy Buscemi Stormare Presnell Coen',
  'filters' => 'year:1991 TO 2001'
]);
Did you find this page helpful?