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:
- LocalStorage : Application manages an app-specific index that lives within application’s data directory
- 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.
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
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.
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.
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
.
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.
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 user1
namespace.
Internal representation of Document
Each document represented as a JSON object
.
Internally IndentingStringBuilder
is used to format the document’s representation
Remove a document
The following code makes an explicit request to remove the Pet document from the database by Id.
- All the documents store in
data/data/your_app_package/files/appsearch
folder