API Reference / API Parameters / removeWordsIfNoResults
Type: string
Engine default: "none"
Parameter syntax
'removeWordsIfNoResults' => 'none'|'lastWords'|'firstWords'|'allOptional'

Can be used in these methods:

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. You can learn more about this technique, as well as caveats with non-alphanumeric characters, in our in-depth guide on removing words to improve results.

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 at most 5 words have been removed.

  • 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 at most 5 words have been removed.

  • 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'
]);
1
2
3
4
5
6
$results = $index->search('query', [
  'removeWordsIfNoResults' => 'lastWords'
  // 'removeWordsIfNoResults' => 'none'
  // 'removeWordsIfNoResults' => 'firstWords'
  // 'removeWordsIfNoResults' => 'allOptional'
]);
Did you find this page helpful?