Boost up on-device search using AppSearch library

Nav Singh
3 min readJul 24, 2021

--

https://developer.android.com/images/search/appsearch_overview_diagram.png

In this article, We will learn about AppSearch library which provides Document based storage solution.

AppSearch

  • High-performance on-device search library for managing locally stored, structured data.
  • It contains APIs for indexing data and retrieving data using full-text search.

With the help of AppSearch applications can offer custom in-app search capabilities, allowing users to search for content even while offline.

Major features

  • Mobile-first and fast storage implementation with low I/O use
  • Multi-language support
  • Highly efficient indexing and querying over large data sets
  • Relevance ranking and usage scoring

Platform Storage vs Local Storage

AppSearch offers two storage solutions:

  1. LocalStorage : Application manages an app-specific index that lives within application’s data directory
  2. PlatformStorage : Application contributes to a system-wide central index. Data access within the central index is restricted to data that application has contributed and data that has been explicitly shared with application by another application.
Features provided by both the solutions(https://developer.android.com/guide/topics/search/appsearch#platform-storage)

Let’s see it in action

  • Setup

Add the following dependencies into your module’s build.gradle file

Check the latest version of library here

build.gradle

Document class

  • It describes the data to insert into the database.
  • @Document annotation is used to mark a class as a document class .
  • You can use instances of the this class to put documents in and retrieve documents from the database.
Pet document class

Open a database

The following code creates a new database with the name pets_app and gets a ListenableFuture for an AppSearchSession, which represents the connection to the database and provides the APIs for database operations.

Open database code

Set a schema

You must set a schema before you can perform any operation on the database.

The database schema consists of different types of structured data, referred to as “schema types.”

The following code sets the schema by providing the document class as a schema type.

Set Schema type

Put a document

Once a schema type is successfully added, you can add documents of that type to the database.

The following code builds a document of schema type Pet using the Put document request builder.

It sets the document namespace user1 to represent an arbitrary user of this sample.

The document is then inserted into the database and a listener is attached to process the result of the put operation.

Put pet document code

Search a document

We can search documents that are indexed using the search operations.

The following code performs queries for the term “Aishley” over the database for documents that belong to the user1namespace.

Search pet

Internal representation of Document

Each document represented as a JSON object.

Internally IndentingStringBuilder is used to format the document’s representation

Internal document representation

Remove a document

The following code makes an explicit request to remove the Pet document from the database by Id.

Remove pet
  • All the documents store in data/data/your_app_package/files/appsearch folder
Storage location of appSearch content

😊😊 👏👏👏👏 HAPPY CODING 👏👏👏👏 😊😊

--

--

Nav Singh
Nav Singh

Written by Nav Singh

Google Developer Expert for Android | Mobile Software Engineer at Manulife | Organizer at GDG Montreal

Responses (1)