API Reference / API Parameters / removeWordsIfNoResults
Feb. 26, 2019

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.

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

Did you find this page helpful?