From b6604fca95896016b02125cedcf51addc40e67e4 Mon Sep 17 00:00:00 2001 From: Dan Saadati Date: Thu, 18 Feb 2021 19:56:49 +0000 Subject: [PATCH] Update AppSearchManager class header with description and examples Bug: 148046169 Test: N/A Relnote: N/A Change-Id: I86c73be48a6f70d027cb7604712959277022174e --- .../app/appsearch/AppSearchManager.java | 85 ++++++++++++++++++- 1 file changed, 81 insertions(+), 4 deletions(-) diff --git a/apex/appsearch/framework/java/android/app/appsearch/AppSearchManager.java b/apex/appsearch/framework/java/android/app/appsearch/AppSearchManager.java index 69d4e536597fd..6a5975ef7ff7a 100644 --- a/apex/appsearch/framework/java/android/app/appsearch/AppSearchManager.java +++ b/apex/appsearch/framework/java/android/app/appsearch/AppSearchManager.java @@ -36,12 +36,89 @@ import java.util.concurrent.Executor; import java.util.function.Consumer; /** - * This class provides access to the centralized AppSearch index maintained by the system. + * Provides access to the centralized AppSearch index maintained by the system. * - *

Apps can index structured text documents with AppSearch, which can then be retrieved through - * the query API. + *

AppSearch is a search library for managing structured data featuring: + *

+ * + *

Applications create a database by opening an {@link AppSearchSession}. + * + *

Example: + *

+ * AppSearchManager appSearchManager = context.getSystemService(AppSearchManager.class);
+ *
+ * AppSearchManager.SearchContext searchContext = new AppSearchManager.SearchContext.Builder().
+ *    setDatabaseName(dbName).build());
+ * appSearchManager.createSearchSession(searchContext, mExecutor, appSearchSessionResult -> {
+ *      mAppSearchSession = appSearchSessionResult.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 {@link AppSearchSession#setSchema}. The schema + * is composed of a collection of {@link 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();
+ * mAppSearchSession.set(request, mExecutor, appSearchResult -> {
+ *      if (appSearchResult.isSuccess()) {
+ *           //Schema has been successfully set.
+ *      }
+ * });
+ * + *

The basic unit of data in AppSearch is represented as a {@link GenericDocument} object, + * containing a URI, 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. A URI identifies a single document within a namespace. The combination + * of URI and namespace uniquely identifies a {@link GenericDocument} in the database. + * + *

Once the schema has been set, {@link GenericDocument} objects can be put into the database + * and indexed by calling {@link AppSearchSession#put}. + * + *

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<>(URI, EMAIL_SCHEMA_TYPE)
+ *     .setNamespace(NAMESPACE)
+ *     .setPropertyString(“subject”, EMAIL_SUBJECT)
+ *     .setScore(EMAIL_SCORE)
+ *     .build();
+ *
+ * PutDocumentsRequest request = new PutDocumentsRequest.Builder().addGenericDocuments(email)
+ *     .build();
+ * mAppSearchSession.put(request, mExecutor, appSearchBatchResult -> {
+ *      if (appSearchBatchResult.isSuccess()) {
+ *           //All documents have been successfully indexed.
+ *      }
+ * });
+ * + *

Searching within the database is done by calling {@link AppSearchSession#search} and providing + * the query string to search for, as well as a {@link SearchSpec}. + * + *

Alternatively, {@link AppSearchSession#getByUri} can be called to retrieve documents by URI + * and namespace. + * + *

Document removal is done either by time-to-live expiration, or explicitly calling a remove + * operation. Remove operations can be done by URI and namespace via + * {@link AppSearchSession#remove(RemoveByUriRequest, Executor, BatchResultCallback)}, + * or by query via {@link AppSearchSession#remove(String, SearchSpec, Executor, Consumer)}. */ -// TODO(b/148046169): This class header needs a detailed example/tutorial. @SystemService(Context.APP_SEARCH_SERVICE) public class AppSearchManager { /**