This multipart blog will help you build an end-to-end mobile application using the industry-leading features of Couchbase Lite. State of the art end-to-end, scalable and production-grade applications include the following features:

    1. An embedded database – storing data locally reduces network trips to a centralized database for every activity on the device. This goes a long way in improving the user experience.
    2. Synchronization with other peer mobile devices and with a centralized server. 
    3. Advanced analytic capabilities, full-text search and machine learning on the device.

Since there’s a lot to cover, we’ve split this project into three parts. Part 1 showcases the process for building a mobile application that leverages Couchbase Lite and uses it as an embedded database to store data. Part 2 (this post) shows how to run Full-Text Search (FTS) on the device. And part 3 will showcase the process of syncing the data with peers and a centralized Couchbase server.

What is Couchbase Mobile? 

Couchbase is an award-winning distributed NoSQL cloud database. It delivers unmatched versatility, performance, scalability, and financial value across cloud, on-premises, hybrid, distributed cloud, and edge computing deployments.

Mobile/edge deployments are an essential part of the digital ecosystem that Couchbase Mobile addresses. Couchbase Mobile includes:

    1. Embedded Couchbase Lite database for edge devices
    2. High-performance Sync Gateway for peer to peer and centralized server synchronization
    3. Edge data centers powered by Couchbase Server can be deployed on the cloud, on-prem or in local edge data centers.
Couchbase Mobile architectural diagramMobile application overview

The code for this demo application is available in my Couchbase Mobile GitHub repository. Download the file Rateit.zip and extract the files to your local folder.

Alternatively, you can follow the instructions in this blog to build the app from scratch.

The app’s functionality is divided into three parts 

    1. Send rating requests
    2. Receive rating requests from others
    3. View the rated request returned from others

To get a deeper understanding of the various components of the app, please refer to part 1, where we send/receive rating requests.

Adding Full-Text search

In this example, we add a simple search button and the code to add an inverted index (used by search systems) that takes user input and displays relevant results.

Building a mobile NoSQL app - screenshot with full-text search added

Getting Started: Review CBLite Documentation

Please review the documentation to understand the various components of a Couchbase Lite database application. I create a brand new database for this app when the application is invoked for the first time. Alternatively, the app can also use a pre-built database.

The steps to embed the database are found in the Couchbase Lite documentation. Review each of the sections to learn to:

    • create and open a database
    • write a document into the embedded database
    • write queries to retrieve the document
    • expire or delete a document
    • build Full-Text Search indexes 
    • make an FTS search expression
    • create a query incorporating the search expression
App building prerequisites

This app has been built for Android devices using the following software and hardware prerequisites that you need to be aware of:

    1. You need to understand the basics of building apps with Android Studio and programming in Java.
    2. This demo app and related screenshots come from building the app on a macOS computer.
    3. We use (freely available) Android Studio 2020.3 to build mobile apps for Android devices. 
    4. Couchbase Lite version 3.0.0-beta02 is used. The required libraries will be embedded in the code and there is no need to download the software manually to your desktop.

Understanding the layout

Layout files are XML files that describe the mobile application screen. For example, activity_main.xml has the SEND TO, MESSAGE, SUBJECT fields in addition to the two listviews that show received and incoming requests. 

It also defines which function to invoke when the CLICK TO SEND RATING REQUEST, RECEIVED RATING REQUESTS and INCOMING RATING REQUESTS buttons are pressed. The XML code can be manually written or auto-generated by setting the attributes in the attributes section of the designer. Here, we add a search input to the center of the layout:

Building an app with Couchbase Lite on Android with text search

Reviewing the code 

There is one code file that we update to add the search logic to the application, Mainactivity.java.

Locate Mainactivity.java under the java -> com.example.rateit folder and paste the following code inside the class MainAcivity. The new code is added to the earlier example in part 1 – it is found on line 8 and lines 12-27 below:

The receivedata functions get activated when RECEIVED RATING REQUESTS is clicked. It displays all the rating requests received by executing a query against the local database to get the records. 

It lets you put in a rating and creates a JSON record with the rating. When the RATE button is clicked, it writes the request back to the database with a document type of rated. If the DELETE button is clicked, the record is deleted by setting the document’s time to live (TTL) to expire.

The new code sections function as follows:

    1. We first add an FTS index if the database file does not exist, indicating first-time entry into the app.
    2. Build the search expression if the user enters a search string
    3. Build the query using the search expression and run it

Wiring up the layout

The following code needs to be added to activity_main.xml. It adds the search button and its label:

Compiling the code 

Once all the code has been updated, click on Build → ReBuild Project. Once it succeeds, click on Build → Run.

You will be asked to choose the emulator the first time you run the app, e.g., select the NEXUS 5X API 25 like I did. Once the run is complete, the main page will look like this:

Building a mobile NoSQL app - screenshot with full-text search added

You have now successfully deployed the code into the emulator of your choice. When the user enters a part of the URL string, the relevant results are displayed.

Next steps

This is the second part of the blog series, Building a Mobile Application with Couchbase, which focuses on a complete end-to-end mobile solution. The first part showed how to create an app that uses an embedded Couchbase Lite database to store, retrieve and delete documents stored locally on the device. 

Part 2 added the ability to do text-based searches, and the third and final part of this series will add peer-to-peer syncing and syncing to a centralized database.

The primary resources referenced in this post included:

 

Author

Posted by Sandhya Krishnamurthy, Senior Solutions Engineer, Couchbase

Sandhya Krishnamurthy is a technologist with a strong database development background and pre-sales experience. She is a part-time artist, part-time singer and full-time mom.

Leave a reply