About a week ago I write a tutorial for implementing a typeahead search with Node.js and jQuery. A typeahead is one of many great use-cases when using full text search (FTS), but it certainly isn’t the only use-case. As many of you know, I am as big of a fan of Golang as I am Node.js, so I figured it would be a good idea to see the same example, but this time using a different programming technology.

We’re going to see how to use typeahead.js, a jQuery extension, as well as Couchbase and the Go programming language to implement a type of autocomplete in our application.

If you didn’t see my previous article, don’t worry because it is a different language so a different problem. We’re going to start from scratch and see how things are done with Go. The assumption is that you have Go installed and configured as well as Couchbase installed and configured.

Preparing a Dataset and a Full Text Search Index

For this example we’re going to work with a very simple dataset, but it could be significantly more complex with minimal extra effort. Our dataset will look something like the following:

Each document in our database will represent a song. For simplicity, we’re only going to focus on the title and artist information, but the documents could contain a wide variety of information.

The next step is to create an FTS index for our documents.

The above image demonstrates what an index should look like. When it comes to the type mapping, we’re only going to be mapping the song documents. We’re also specifying that we only want the artist and title information in our index. The important part here is that we are storing the fields in the index. If we don’t specifically choose to store the field in the index, then we will have no information to populate in the results of our search.

Creating the Go Powered Full Text Search (FTS) Backend

With some data and our FTS index in place, we can start developing the backend that will execute the queries. Our backend will be a RESTful API with a single endpoint for searching. The boilerplate for this application can be seen below:

First we’re creating a native Go data structure that will eventually hold our anticipated search results. Skipping over the endpoint logic for now, we have our main function where we connect to our database and define our only endpoint. Because searching will happen via JavaScript, we need to configure cross-origin resource sharing (CORS) to allow the requests.

If you’d like to learn more about cross-origin resource sharing with Go, I wrote a previous article on the subject. However, knowing the ins and outs isn’t too important for this example.

Let’s take a look at the function that matters:

When this function is called, we extract any parameters from the request query parameters. These parameters are then used to create a new query against our full text search index. In the response we want both the artist and title fields so we must define them in our query. Upon successful execution, we loop through our search hits and construct an object that might be useful to our frontend.

If you wanted to run the Go application, http://localhost:12345/search?query=swift could potentially return results.

Designing the jQuery Frontend for Typeahead Searches Against the Database

With the backend in place, now we can focus on the frontend. The frontend will be universal regardless as to which backend language you’re going to be using.

Our goal is to create something like this:

We’re going to be using typeahead.js, jQuery, and Handlebars.js to make this part a success. However, it is very important that we use version 0.10.5 of typeahead.js due to a bug in later versions.

With the libraries downloaded, create a project structure that looks like the following:

Make sure that your versions match your file versions and not mine.

In the index.html file, we can bootstrap our code with the following:

Notice that the above code essentially just contains a form input and the importing of all our libraries. I’ve commended out where our logic for the typeahead will end up.

Let’s bring this to a close with our logic:

The first part of our typeahead is the configuration. We’re saying we want hints to show and that our logic won’t happen unless there are at least three characters in the search to save on remote calls. We won’t highlight the results, but we’ll limit them to five. To drive the logic of our typeahead, we’ll issue a request to our backend and process the response. It will be rendered based on our custom Handlebars template.

The displayKey is what we want to show after a result is clicked. In our scenario, we want to show the artists and song titles, but populate the id when clicked. This way the id can be sent in a future request if we wanted to.

Conclusion

You just saw how to create a typeahead search in jQuery with Go as the backend and Couchbase as the NoSQL database. This tutorial was a followup to a previous tutorial where I had written the FTS backend with Node.js and JavaScript.

For more information on using the Go SDK with Couchbase, check out the Couchbase Developer Portal.

Author

Posted by Nic Raboy, Developer Advocate, Couchbase

Nic Raboy is an advocate of modern web and mobile development technologies. He has experience in Java, JavaScript, Golang and a variety of frameworks such as Angular, NativeScript, and Apache Cordova. Nic writes about his development experiences related to making web and mobile development easier to understand.

Leave a reply