Icon searching white

Query Expansion

Last updated 01 August 2017

What is query expansion?

Query expansion is the progressive loosening of query constraints to include more results when none are initially found.

For example, imagine an online smartphone shop that sold a limited inventory of iPhones in only 16GB and 32GB varieties. Users searching for “iphone 5 64gb” would see no results. This is not ideal behavior — it would be far better to show users some iPhone 5 results instead of a blank page. That’s where query expansion comes in.

Remove Words If No Results

This parameter helps you make an initial query less and less specific until results are found. The right value to choose for a given use case may depend on the language searched as well as usage patterns.

You can choose one of these four behaviors: none (default), lastWords, firstWords, allOptional.

none

This is the engine’s default behavior — no additional processing is done when a query returns no results.

lastWords

This value treats a query’s last word as optional, and if there are still no results, it repeats the operation until either there are results, or the beginning of the query string has been reached.

For example, imagine a search for sparkly blue iPhone cases:

  • The first query is sparkly blue iPhone cases.
  • The second query is sparkly blue iPhone.
  • The third query is sparkly blue.
  • The fourth query is sparkly.

firstWords

This value treats a query’s first word as optional, and repeats the operation until either there are results, or the end of the query string has been reached.

  • The first query is sparkly blue iPhone cases.
  • The second query is blue iPhone cases.
  • The third query is iPhone cases.
  • The fourth query is cases.

It’s important to consider typical search patterns when deciding between firstWords and lastWords. For example, firstWords would be more suitable than lastWords in queries like above. However, this isn’t always the case. For example, let’s take a look at the query “iphone 5 32gb.”

Expanding “iphone 5 32gb” with firstWords

"iphone 5 32gb" ➡️ "5 32gb" ➡️ "32gb"

Here, the most relevant part of the query is actually at the front; discarding those words makes the query irrelevant. Compare to the use of lastWords:

Expanding “iphone 5 32gb” with lastWords

"iphone 5 32gb" ➡️ "iphone 5" ➡️ "iphone"

This is much better — as we strip away detailed descriptors, we expand our result set without making the query irrelevant.

allOptional

If there are no results for the initial query, allOptional specifies a second search in which all words are treated as optional. This is essentially changing the implicit AND operator between words to OR:

"blue AND iPhone AND cases" ➡️ "blue OR iPhone OR cases"

In an e-commerce shop that sold a wide range of products, using allOptional as above would return a far wider range of results — blue towels, books on iPhone development, and camera cases. This breadth would come at the expense of relevance, so it’s best to use this parameter cautiously.

Advanced Syntax Overview

When Algolia’s advanced syntax is enabled, search queries are treated differently in two significant ways. The behaviors below are both enabled by Advanced Syntax; they cannot be enabled individually.

Phrase Queries

Terms enclosed in quotes must match exactly. For example,

sparkly blue "iPhone case"

would return only records containing the exact string, “iPhone case.” However, “sparkly” and “blue” could occur anywhere in the record; their location and order are not constrained.

Note that typo tolerance is disabled inside the quotes.

Prohibit Operator

The prohibit operator is simply a minus sign, prepended to a term in the search query that should be excluded from results. For example,

search -engine

will only match records that contain “search” and do not contain “engine.”

What’s Next

Continue building your Algolia knowledge with these concepts:

© Algolia - Privacy Policy