similarQuery
'similarQuery' => "query"
Can be used in these methods:
search,
browseObjects,
searchForFacetValues,
generateSecuredApiKey,
addApiKey,
updateApiKey
search,
browse_objects,
search_for_facet_values,
generate_secured_api_key,
add_api_key,
update_api_key
search,
browseObjects,
searchForFacetValues,
generateSecuredApiKey,
addApiKey,
updateApiKey
search,
browse_objects,
search_for_facet_values,
generate_secured_api_key,
add_api_key,
update_api_key
search,
browse,
searchForFacetValues,
generateSecuredApiKey,
addAPIKey,
updateAPIKey
search,
browseObjects,
searchForFacetValues,
generateSecuredApiKey,
addApiKey,
updateApiKey
Search,
Browse,
SearchForFacetValues,
GenerateSecuredApiKeys,
AddApiKey,
UpdateApiKey
Search,
browse,
searchForFacetValues,
generateSecuredApiKey,
addApiKey,
updateApiKey
Search,
BrowseObjects,
SearchForFacetValues,
GenerateSecuredAPIKey,
AddAPIKey,
UpdateAPIKey
search,
browse index,
search into facet values,
generateSecuredApiKey,
add key,
update key
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, asimilarQuery
for a movie could use the genre, principle actors, and tags attributes. After extracting information from those categories, you might end up with asimilarQuery
that looks like “Romance Comedy Gordon-Levitt NY”.
This parameter changes search settings in several ways:
- Sets
queryType
toprefixNone
(no prefix match). Matched prefixes aren’t counted as word matches. - Sets
removeStopWords
totrue
. 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 theoptionalWords
parameter.
- Objects that match any word in
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'
]);