removeWordsIfNoResults
'removeWordsIfNoResults' => 'none'|'lastWords'|'firstWords'|'allOptional'
Can be used in these methods:
search,
setSettings,
browseObjects,
searchForFacetValues,
generateSecuredApiKey,
addApiKey,
updateApiKey
search,
set_settings,
browse,
search_for_facet_values,
generate_secured_api_key,
add_api_key,
update_api_key
search,
setSettings,
browse,
searchForFacetValues,
generateSecuredApiKey,
addApiKey,
updateApiKey
search,
set_settings,
browse_objects,
search_for_facet_values,
generate_secured_api_key,
add_api_key,
update_api_key
search,
setSettings,
browse,
searchForFacetValues
search,
setSettings,
browseObjects,
searchForFacetValues,
generateSecuredApiKey,
addApiKey,
updateApiKey
searchAsync,
setSettingsAsync,
browseAsync,
searchForFacetValues
Search,
SetSettings,
Browse,
SearchForFacetValues,
GenerateSecuredApiKey,
AddApiKey,
UpdateApiKey
Search,
setSettings,
browse,
searchForFacetValues,
generateSecuredApiKey,
addApiKey,
updateApiKey
Search,
SetSettings,
BrowseObjects,
SearchForFacetValues,
GenerateSecuredAPIKey,
AddAPIKey,
UpdateAPIKey
search,
setSettings,
browse index,
search into facet values,
generateSecuredApiKey,
add key,
update key
About this parameter
Selects a strategy to remove words from the query when it doesn’t match any hits.
The goal is to avoid empty results by progressively loosening the query until hits are matched.
Options:
-
none: No specific processing is done when a query does not return any results (default behavior).
-
lastWords: When a query does not return any results, treat the last word as optional. The process is repeated with words N-1, N-2, etc. until there are results, or the beginning of the query string has been reached.
-
firstWords: When a query does not return any results, treat the first word as optional. The process is repeated with words 2, 3, etc. until there are results, or the end of the query string has been reached.
-
allOptional: When a query does not return any results, make a second attempt treating all words as optional. This is equivalent to transforming the implicit AND operator applied between query words to an OR.
Examples
Set default strategy to remove words from the query
1
2
3
4
5
6
$index->setSettings([
'removeWordsIfNoResults' => 'none'
// 'removeWordsIfNoResults' => 'lastWords'
// 'removeWordsIfNoResults' => 'firstWords'
// 'removeWordsIfNoResults' => 'allOptional'
]);
Override default strategy to remove words from the query for the current search
1
2
3
4
5
6
$results = $index->search('query', [
'removeWordsIfNoResults' => 'lastWords'
// 'removeWordsIfNoResults' => 'none'
// 'removeWordsIfNoResults' => 'firstWords'
// 'removeWordsIfNoResults' => 'allOptional'
]);