Open In App

How to Use AppSearch in Android 13?

Last Updated : 26 Jan, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

AppSearch is an efficient on-device search solution for managing locally stored, structured data is called AppSearch. It includes APIs for indexing data and full-text data retrieval. Applications can use AppSearch to provide unique in-app search features, enabling users to conduct offline content searches. Comparing AppSearch with SQLite, AppSearch provides better latency for indexing and searching over huge datasets due to lesser I/O utilization. While SQLite mixes the results from several tables, AppSearch supports single queries, which simplifies cross-type queries.

Example of AppSearch

Let’s use the example of the GeeksforGeeks application that keeps track of users enrolled in courses and enables them to quickly search for them to find the advantages of AppSearch. Users enjoy the app with new articles in various languages from around the world, and AppSearch has native support for indexing and querying. The application only forwards the request to AppSearch when a user searches for a course by title or author name to swiftly and effectively get matching articles available. Users can start reading the article right away because of the way the application presents the results.

Image #1: Understanding the App Search Architecture.

Image #1: Understanding the App Search Architecture.

Features of AppSearch

  • Large-scale indexing and querying that is extremely effective
  • Support for multiple languages, including Spanish and English
  • Quick implementation of mobile-first storage with minimal I/O usage
  • Usefulness evaluation and relevance ranking

How to add App-Search in your app?

It is fairly easy to add App Search in your Android Application to do it, simply:

Step #1: Add the necessary dependencies to your application’s build.gradle file in order to use AppSearch

implementation "androidx.appsearch:appsearch:1.0"
implementation "androidx.appsearch:appsearch-local-storage:1.0"
implementation "androidx.appsearch:appsearch-platform-storage:1.0"

Step #2: Enable your MainActivity

To add your in-app search functionality, add the below code to your app’s MainActivity file.

Java




if (BuildCompat.isAtLeastS()) {
    // Add this to your Android App's main file
    gfgSearchApp.setFuture(PlatformStorage.createSearchSession(
            new PlatformStorage.SearchContext.Builder(mContext, GFG_DATABASE)
                    .build()));
} else {
    gfgSearchApp.setFuture(LocalStorage.createSearchSession(
            new LocalStorage.SearchContext.Builder(mContext, GFG_DATABASE)
                    .build()));
}


Key Points to Remember:

  1. Data source and session: A group of documents that adhere to the database structure is an AppSearch database. By supplying a database name and its application context, client apps establish databases. Only the application that built the database can open it. A session is returned after opening a database so that you can interact with it. The session is the starting point for calling the AppSearch APIs, and it is active until the client application closes it.
  2. Schema and types of schema: The data organization within an AppSearch database is represented by a schema. The schema is made up of schema types that stand in for particular kinds of data. Properties with a name, a data type, and a cardinality make up schema types. Documents of that schema type can be created and added to the database when a schema type has been added to the database schema.
  3. Search By entering a query: It is possible to search across indexed documents. If a document satisfies another search parameter or contains the terms in the query, it is added to the results of the search. Results are ranked and organized according to their score. Pages that you can retrieve one after the other indicate the search results.

How is App Search Different from Local Storage?

LocalStorage and PlatformStorage are the two storage options that AppSearch provides. Your application manages an app-specific index that resides in your application data directory using LocalStorage. Your program contributes to a system-wide central index using PlatformStorage. Only the data that your application has supplied and the data that another application has officially shared with you are accessible within the central index.

Opening the Database:

Before working with documents, a database needs to be created. The code that follows establishes a brand-new database called notes app and obtains a ListenableFuture for an AppSearchSession, which stands for the connection to the database and offers APIs for database operations.

Java




Context gfgContext = getApplicationContext();
  
// Add this to enable in app search functionality.
ListenableFuture<AppSearchSession> gfgActivitySearch
    = LocalStorage.createSearchSession(
        new LocalStorage.SearchContext
            .Builder(context,
                     /*GFG_DATABASE=*/"gfg_course_app")
            .build());


Setting up the Schema:

Before you can add documents to the database and retrieve them from it, a schema must first be established. The “schema kinds”—different sorts of structured data that make up the database schema—are The document class used as a schema type in the code that follows to set the schema.

Java




SetSchemaRequest gfgSchemaRequest
    = new SetSchemaRequest.Builder()
          .addDocumentClasses(Courses.class)
          .build();
  
// Add this to your Android App's 
// main activity to open the schema.
ListenableFuture<SetSchemaResponse> gfgSchemaFuture
    = Futures.transformAsync(
        gfgSessionFuture,
        session
        -> session.setSchema(gfgFeatureSession),
        gfgContext);


By adding these lines, you will be able to activate App Search in your android application, and with the power of Android 13, you will be able to perform your indexed search within seconds. You will also then be able to add extended functionality to your android application and get new users as they will feel excited to try this out!

Conclusion

With the introduction of App search for Android 12 and Android 13, developers can now integrate not just their app but also the features and documents found within it. If you only use search to find apps, this is kind of a mixed blessing. Additionally, it can be a little busy having results jumbled in with contacts and other data. However, this now has a new layer that can be useful.



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads