Add Autocomplete to Your Application

  • Capella Operational
  • how-to
March 23, 2025
+ 12
Use autocomplete to add suggestions for a user’s Search query as they type in your application.

After you create and configure a Search index that supports autocomplete, configure your application to return results from the Search Service.

Prerequisites

  • You have the Search Service enabled on a node in your cluster. For more information about how to change Services on your cluster, see Modify a Paid Cluster.

  • You have a bucket with scopes and collections in your cluster. For more information, see Manage Buckets.

  • You have the hostname for the node in your cluster that’s running the Search Service.

    Go to your cluster settings and click Nodes to view node hostnames.

  • You have created a compatible Search index. For more information, see Configure an Autocomplete Search Index.

  • You have created cluster access credentials that have Read/Write permissions on the bucket and scope where you created your Search index. For more information, see Configure Cluster Access Credentials.

Procedure

To add autocomplete with the Search Service to your application:

  1. Test that your Search index was configured correctly by running a Search query from the Capella UI with 2-8 characters in the Search field.

    For example, with the travel-sample bucket, you could enter the strings Be, Bea, Beau, and Beauf to find a document with the text Beaufort Hotel.

  2. Configure your application to return results from the Search Service.

    The following examples simulate a user typing an input string and return search results for each partial string:

    csharp
    using Couchbase; using Couchbase.Search.Queries.Simple; await using var cluster = await Cluster.ConnectAsync(new ClusterOptions { ConnectionString = "CB_HOSTNAME", UserName = "CB_USERNAME", Password = "CB_PASSWORD", Buckets = new List<string>{"travel-sample"} }.ApplyProfile("wan-development")); var searchString = "Beaufort Hotel"; for (var i = 2; i <= 8; i++) { var lettersEntered = searchString.Substring(0, i); Console.WriteLine($"Input <{lettersEntered}>, len: {lettersEntered.Length}"); await FtsMatchPrefix(lettersEntered); } async Task FtsMatchPrefix(string letters) { try { var results = await cluster.SearchQueryAsync("e-ngram-2-8", new QueryStringQuery(letters), options => { options.Limit(8); options.Fields("name"); }); results.Hits.ToList().ForEach(row => { Console.WriteLine($"{row.Id}, {row.Fields}"); }); Console.WriteLine(results); } catch (Exception e) { Console.WriteLine(e); } }

Next Steps

After you add autocomplete to your application, to improve your search results, you can: