You are currently browsing legacy 4.0 version of documentation. Click here to switch to the newest 5.1 version.

We can help you with migration to the latest RavenDB

Contact Us Now
see on GitHub

Querying: Suggestions

RavenDB has an indexing mechanism built upon the Lucene engine which has a great suggestions feature. This capability allows a significant improvement of search functionalities enhancing the overall user experience of the application.

Let's consider an example where the users have the option to look for products by their name. The index and query would appear as follows:

public class Products_ByName extends AbstractIndexCreationTask {
    public Products_ByName() {
        map = "from product in docs.Products " +
            "select new " +
            "{ " +
            "  product.Name " +
            "} ";

        index("Name", FieldIndexing.SEARCH); // (optional) splitting name into multiple tokens
        suggestion("Name");// configuring suggestions

Product product = session
    .query(Product.class, Products_ByName.class)
    .search("Name", "chaig")

If our database has Northwind samples deployed then it will not return any results. However, we can ask RavenDB for help:

Map<String, SuggestionResult> suggestionResult = session
    .query(Product.class, Products_ByName.class)
    .suggestUsing(builder -> builder.byField("Name", "chaig"))

System.out.println("Did you mean?");

for (String suggestion : suggestionResult.get("Name").getSuggestions()) {
    System.out.println("\t" + suggestion);
from index 'Products/ByName' 
select suggest('Name', 'chaig')

It will produce these suggestions:

Did you mean?

Client API

You can read more about suggestions in our Client API article.

Suggest Over Multiple Words

RavenDB allows you to perform a suggestion query over multiple words.

SuggestionOptions options = new SuggestionOptions();

Map<String, SuggestionResult> resultsByMultipleWords = session
    .query(Product.class, Products_ByName.class)
    .suggestUsing(builder ->
        builder.byField("Name", new String[]{"chaig", "tof"})

System.out.println("Did you mean?");

for (String suggestion : resultsByMultipleWords.get("Name").getSuggestions()) {
    System.out.println("\t" + suggestion);

This will produce the following results:

Did you mean?


Increased indexing time

Indexes with turned on suggestions tend to use a lot more CPU power than other indexes. This can impact indexing speed (querying is not impacted).