AppSearchManager
Stay organized with collections
Save and categorize content based on your preferences.
Provides access to the centralized AppSearch index maintained by the system.
AppSearch is an offline, on-device search library for managing structured data
featuring:
Applications create a database by opening an AppSearchClient
.
Example:
AppSearchManager appSearchManager = context.getSystemService(AppSearchManager.class);
AppSearchManager.SearchContext searchContext = new AppSearchManager.SearchContext.Builder().
setDatabaseName(dbName).build());
appSearchManager.createSearchSession(searchContext, mExecutor, AppSearchClientResult -> {
mAppSearchClient = AppSearchClientResult.getResultValue();
});
After opening the session, a schema must be set in order to define the organizational
structure of data. The schema is set by calling AppSearchClient.setSchema(SetSchemaRequest, String)
. The schema is composed of a
collection of AppSearchSchema
objects, each of which defines a unique type of data.
Example:
AppSearchSchema emailSchemaType = new AppSearchSchema.Builder("Email")
.addProperty(new StringPropertyConfig.Builder("subject")
.setCardinality(PropertyConfig.CARDINALITY_OPTIONAL)
.setIndexingType(PropertyConfig.INDEXING_TYPE_PREFIXES)
.setTokenizerType(PropertyConfig.TOKENIZER_TYPE_PLAIN)
.build()
).build();
SetSchemaRequest request = new SetSchemaRequest.Builder().addSchema(emailSchemaType).build();
mAppSearchClient.set(request, mExecutor, appSearchResult -> {
if (appSearchResult.isSuccess()) {
// Schema has been successfully set.
}
});
The basic unit of data in AppSearch is represented as a GenericDocument
object, containing an ID, namespace, time-to-live, score, and properties. A namespace
organizes a logical group of documents. For example, a namespace can be created to group
documents on a per-account basis. An ID identifies a single document within a namespace. The
combination of namespace and ID uniquely identifies a GenericDocument
in the database.
Once the schema has been set, GenericDocument
objects can be put into the database and indexed by calling AppSearchClient.put(PutDocumentsRequest, String)
.
Example:
// Although for this example we use GenericDocument directly, we recommend extending
// GenericDocument to create specific types (i.e. Email) with specific setters/getters.
GenericDocument email = new GenericDocument.Builder<>(NAMESPACE, ID, EMAIL_SCHEMA_TYPE)
.setPropertyString(“subject”, EMAIL_SUBJECT)
.setScore(EMAIL_SCORE)
.build();
PutDocumentsRequest request = new PutDocumentsRequest.Builder().addGenericDocuments(email)
.build();
mAppSearchClient.put(request, mExecutor, appSearchBatchResult -> {
if (appSearchBatchResult.isSuccess()) {
// All documents have been successfully indexed.
}
});
Searching within the database is done by calling AppSearchClient.search(String, SearchSpec, String)
and providing the query string
to search for, as well as a SearchSpec
.
Alternatively, AppSearchClient.getByDocumentId(GetByDocumentIdRequest, String)
can be called to
retrieve documents by namespace and ID.
Document removal is done either by time-to-live expiration, or explicitly calling a remove
operation. Remove operations can be done by namespace and ID via AppSearchClient.remove(RemoveByDocumentIdRequest, String)
, or by query via AppSearchClient.remove(String, SearchSpec, String)
.
Nested Class Summary
class
Contains information about how to create the
search session.
Inherited Method Summary
From class java.lang.Object
Except as otherwise noted, the content of this page is licensed under the Creative Commons Attribution 4.0 License
, and code samples are licensed under the Apache 2.0 License
. For details, see the Google Developers Site Policies
. Java is a registered trademark of Oracle and/or its affiliates.
Last updated 2024-10-31 UTC.
[[["Easy to understand","easyToUnderstand","thumb-up"],["Solved my problem","solvedMyProblem","thumb-up"],["Other","otherUp","thumb-up"]],[["Missing the information I need","missingTheInformationINeed","thumb-down"],["Too complicated / too many steps","tooComplicatedTooManySteps","thumb-down"],["Out of date","outOfDate","thumb-down"],["Samples / code issue","samplesCodeIssue","thumb-down"],["Other","otherDown","thumb-down"]],["Last updated 2024-10-31 UTC."],[[["\u003cp\u003e\u003ccode\u003eAppSearchManager\u003c/code\u003e provides an on-device, offline search library for managing structured data using full-text search.\u003c/p\u003e\n"],["\u003cp\u003eApplications can grant read-access permission to other applications and control data visibility on System UI surfaces.\u003c/p\u003e\n"],["\u003cp\u003eData is organized with schemas, documents are indexed by namespace and ID, and retrieval is done via search or direct document ID.\u003c/p\u003e\n"],["\u003cp\u003e\u003ccode\u003eAppSearchClient\u003c/code\u003e is used to interact with the AppSearch database for operations like setting schemas, indexing documents, searching, and removing data.\u003c/p\u003e\n"],["\u003cp\u003eDocuments can be removed explicitly or by time-to-live expiration, providing flexibility in data management.\u003c/p\u003e\n"]]],["AppSearchManager facilitates on-device data management and search. Key actions include: creating a database via `AppSearchClient`, defining data structure with `AppSearchSchema` in `SetSchemaRequest`, and indexing data using `GenericDocument` via `PutDocumentsRequest`. Data visibility to other apps and system UI is configurable. Search is performed via queries in `SearchSpec`, or by document ID. Documents can be removed by time-to-live, ID, or query.\n"],null,["# AppSearchManager\n\npublic class **AppSearchManager** extends [Object](//developer.android.com/reference/java/lang/Object.html) \nProvides access to the centralized AppSearch index maintained by the system.\n\nAppSearch is an offline, on-device search library for managing structured data\nfeaturing:\n\n- APIs to index and retrieve data via full-text search.\n- An API for applications to explicitly grant read-access permission of their data to other applications. **See: [SetSchemaRequest.Builder.setSchemaTypeVisibilityForPackage(String, boolean,\n PackageIdentifier)](/android/reference/com/google/android/gms/appsearch/SetSchemaRequest.Builder#setSchemaTypeVisibilityForPackage(java.lang.String,%20boolean,%20com.google.android.gms.appsearch.PackageIdentifier))**\n- An API for applications to opt into or out of having their data displayed on System UI surfaces by the System-designated global querier. **See: [SetSchemaRequest.Builder.setSchemaTypeDisplayedBySystem(String,\n boolean)](/android/reference/com/google/android/gms/appsearch/SetSchemaRequest.Builder#setSchemaTypeDisplayedBySystem(java.lang.String,%20boolean))**\n\nApplications create a database by opening an [AppSearchClient](/android/reference/com/google/android/gms/appsearch/AppSearchClient).\n\nExample: \n\n```\n AppSearchManager appSearchManager = context.getSystemService(AppSearchManager.class);\n\n AppSearchManager.SearchContext searchContext = new AppSearchManager.SearchContext.Builder().\n setDatabaseName(dbName).build());\n appSearchManager.createSearchSession(searchContext, mExecutor, AppSearchClientResult -\u003e {\n mAppSearchClient = AppSearchClientResult.getResultValue();\n });\n```\n\nAfter opening the session, a schema must be set in order to define the organizational\nstructure of data. The schema is set by calling [AppSearchClient.setSchema(SetSchemaRequest, String)](/android/reference/com/google/android/gms/appsearch/AppSearchClient#setSchema(com.google.android.gms.appsearch.SetSchemaRequest,%20java.lang.String)). The schema is composed of a\ncollection of [AppSearchSchema](/android/reference/com/google/android/gms/appsearch/AppSearchSchema)\nobjects, each of which defines a unique type of data.\n\nExample: \n\n```\n AppSearchSchema emailSchemaType = new AppSearchSchema.Builder(\"Email\")\n .addProperty(new StringPropertyConfig.Builder(\"subject\")\n .setCardinality(PropertyConfig.CARDINALITY_OPTIONAL)\n .setIndexingType(PropertyConfig.INDEXING_TYPE_PREFIXES)\n .setTokenizerType(PropertyConfig.TOKENIZER_TYPE_PLAIN)\n .build()\n ).build();\n\n SetSchemaRequest request = new SetSchemaRequest.Builder().addSchema(emailSchemaType).build();\n mAppSearchClient.set(request, mExecutor, appSearchResult -\u003e {\n if (appSearchResult.isSuccess()) {\n // Schema has been successfully set.\n }\n });\n```\n\nThe basic unit of data in AppSearch is represented as a [GenericDocument](/android/reference/com/google/android/gms/appsearch/GenericDocument)\nobject, containing an ID, namespace, time-to-live, score, and properties. A namespace\norganizes a logical group of documents. For example, a namespace can be created to group\ndocuments on a per-account basis. An ID identifies a single document within a namespace. The\ncombination of namespace and ID uniquely identifies a [GenericDocument](/android/reference/com/google/android/gms/appsearch/GenericDocument)\nin the database.\n\nOnce the schema has been set, [GenericDocument](/android/reference/com/google/android/gms/appsearch/GenericDocument)\nobjects can be put into the database and indexed by calling [AppSearchClient.put(PutDocumentsRequest, String)](/android/reference/com/google/android/gms/appsearch/AppSearchClient#put(com.google.android.gms.appsearch.PutDocumentsRequest,%20java.lang.String)).\n\nExample: \n\n```\n // Although for this example we use GenericDocument directly, we recommend extending\n // GenericDocument to create specific types (i.e. Email) with specific setters/getters.\n GenericDocument email = new GenericDocument.Builder\u003c\u003e(NAMESPACE, ID, EMAIL_SCHEMA_TYPE)\n .setPropertyString(“subject”, EMAIL_SUBJECT)\n .setScore(EMAIL_SCORE)\n .build();\n\n PutDocumentsRequest request = new PutDocumentsRequest.Builder().addGenericDocuments(email)\n .build();\n mAppSearchClient.put(request, mExecutor, appSearchBatchResult -\u003e {\n if (appSearchBatchResult.isSuccess()) {\n // All documents have been successfully indexed.\n }\n });\n```\n\nSearching within the database is done by calling [AppSearchClient.search(String, SearchSpec, String)](/android/reference/com/google/android/gms/appsearch/AppSearchClient#search(java.lang.String,%20com.google.android.gms.appsearch.SearchSpec,%20java.lang.String)) and providing the query string\nto search for, as well as a [SearchSpec](/android/reference/com/google/android/gms/appsearch/SearchSpec).\n\nAlternatively, [AppSearchClient.getByDocumentId(GetByDocumentIdRequest, String)](/android/reference/com/google/android/gms/appsearch/AppSearchClient#getByDocumentId(com.google.android.gms.appsearch.GetByDocumentIdRequest,%20java.lang.String)) can be called to\nretrieve documents by namespace and ID.\n\nDocument removal is done either by time-to-live expiration, or explicitly calling a remove\noperation. Remove operations can be done by namespace and ID via [AppSearchClient.remove(RemoveByDocumentIdRequest, String)](/android/reference/com/google/android/gms/appsearch/AppSearchClient#remove(com.google.android.gms.appsearch.RemoveByDocumentIdRequest,%20java.lang.String)), or by query via\n[AppSearchClient.remove(String, SearchSpec, String)](/android/reference/com/google/android/gms/appsearch/AppSearchClient#remove(java.lang.String,%20com.google.android.gms.appsearch.SearchSpec,%20java.lang.String)). \n\n### Nested Class Summary\n\n|-------|---|---|--------------------------------------------------------------|\n| class | [AppSearchManager.SearchContext](/android/reference/com/google/android/gms/appsearch/AppSearchManager.SearchContext) || Contains information about how to create the search session. |\n\n### Inherited Method Summary\n\nFrom class java.lang.Object \n\n|----------------------------------------------------------------------------|--------------------------------------------------------------------------------|\n| [Object](//developer.android.com/reference/java/lang/Object.html) | clone() |\n| boolean | equals([Object](//developer.android.com/reference/java/lang/Object.html) arg0) |\n| void | finalize() |\n| final [Class](//developer.android.com/reference/java/lang/Class.html)\\\u003c?\\\u003e | getClass() |\n| int | hashCode() |\n| final void | notify() |\n| final void | notifyAll() |\n| [String](//developer.android.com/reference/java/lang/String.html) | toString() |\n| final void | wait(long arg0, int arg1) |\n| final void | wait(long arg0) |\n| final void | wait() |"]]