# Print output for @column tags ?> AppSearchSession - Android SDK | Android Developers

Most visited

Recently visited

AppSearchSession

public final class AppSearchSession
extends Object implements Closeable

java.lang.Object
   ↳ android.app.appsearch.AppSearchSession


Provides a connection to a single AppSearch database.

An AppSearchSession instance provides access to database operations such as setting a schema, adding documents, and searching.

This class is thread safe.

See also:

Summary

Public methods

void close()

Closes the AppSearchSession to persist all schema and document updates, additions, and deletes to disk.

void getByDocumentId(GetByDocumentIdRequest request, Executor executor, BatchResultCallback<StringGenericDocument> callback)

Gets GenericDocument objects by document IDs in a namespace from the AppSearchSession database.

void getNamespaces(Executor executor, Consumer<AppSearchResult<Set<String>>> callback)

Retrieves the set of all namespaces in the current database with at least one document.

void getSchema(Executor executor, Consumer<AppSearchResult<GetSchemaResponse>> callback)

Retrieves the schema most recently successfully provided to setSchema(SetSchemaRequest, Executor, Executor, Consumer>).

void getStorageInfo(Executor executor, Consumer<AppSearchResult<StorageInfo>> callback)

Gets the storage info for this AppSearchSession database.

void put(PutDocumentsRequest request, Executor executor, BatchResultCallback<StringVoid> callback)

Indexes documents into the AppSearchSession database.

void remove(RemoveByDocumentIdRequest request, Executor executor, BatchResultCallback<StringVoid> callback)

Removes GenericDocument objects by document IDs in a namespace from the AppSearchSession database.

void remove(String queryExpression, SearchSpec searchSpec, Executor executor, Consumer<AppSearchResult<Void>> callback)

Removes GenericDocuments from the index by Query.

void reportUsage(ReportUsageRequest request, Executor executor, Consumer<AppSearchResult<Void>> callback)

Reports usage of a particular document by namespace and ID.

SearchResults search(String queryExpression, SearchSpec searchSpec)

Retrieves documents from the open AppSearchSession that match a given query string and type of search provided.

void setSchema(SetSchemaRequest request, Executor workExecutor, Executor callbackExecutor, Consumer<AppSearchResult<SetSchemaResponse>> callback)

Sets the schema that represents the organizational structure of data within the AppSearch database.

Inherited methods

Public methods

close

public void close ()

Closes the AppSearchSession to persist all schema and document updates, additions, and deletes to disk.

getByDocumentId

public void getByDocumentId (GetByDocumentIdRequest request, 
                Executor executor, 
                BatchResultCallback<StringGenericDocument> callback)

Gets GenericDocument objects by document IDs in a namespace from the AppSearchSession database.

Parameters
request GetByDocumentIdRequest: a request containing a namespace and IDs to get documents for. This value cannot be null.

executor Executor: Executor on which to invoke the callback. This value cannot be null. Callback and listener events are dispatched through this Executor, providing an easy way to control which thread is used. To dispatch events through the main thread of your application, you can use Context.getMainExecutor(). To dispatch events through a shared thread pool, you can use AsyncTask#THREAD_POOL_EXECUTOR.

callback BatchResultCallback: Callback to receive the pending result of performing this operation. The keys of the returned AppSearchBatchResult are the input IDs. The values are the returned GenericDocuments on success, or a failed AppSearchResult otherwise. IDs that are not found will return a failed AppSearchResult with a result code of AppSearchResult#RESULT_NOT_FOUND. If an unexpected internal error occurs in the AppSearch service, BatchResultCallback#onSystemError will be invoked with a Throwable. This value cannot be null.

getNamespaces

public void getNamespaces (Executor executor, 
                Consumer<AppSearchResult<Set<String>>> callback)

Retrieves the set of all namespaces in the current database with at least one document.

Parameters
executor Executor: Executor on which to invoke the callback. This value cannot be null. Callback and listener events are dispatched through this Executor, providing an easy way to control which thread is used. To dispatch events through the main thread of your application, you can use Context.getMainExecutor(). To dispatch events through a shared thread pool, you can use AsyncTask#THREAD_POOL_EXECUTOR.

callback Consumer: Callback to receive the namespaces. This value cannot be null.

getSchema

public void getSchema (Executor executor, 
                Consumer<AppSearchResult<GetSchemaResponse>> callback)

Retrieves the schema most recently successfully provided to setSchema(SetSchemaRequest, Executor, Executor, Consumer>).

Parameters
executor Executor: Executor on which to invoke the callback. This value cannot be null. Callback and listener events are dispatched through this Executor, providing an easy way to control which thread is used. To dispatch events through the main thread of your application, you can use Context.getMainExecutor(). To dispatch events through a shared thread pool, you can use AsyncTask#THREAD_POOL_EXECUTOR.

callback Consumer: Callback to receive the pending results of schema. This value cannot be null.

getStorageInfo

public void getStorageInfo (Executor executor, 
                Consumer<AppSearchResult<StorageInfo>> callback)

Gets the storage info for this AppSearchSession database.

This may take time proportional to the number of documents and may be inefficient to call repeatedly.

Parameters
executor Executor: Executor on which to invoke the callback. This value cannot be null. Callback and listener events are dispatched through this Executor, providing an easy way to control which thread is used. To dispatch events through the main thread of your application, you can use Context.getMainExecutor(). To dispatch events through a shared thread pool, you can use AsyncTask#THREAD_POOL_EXECUTOR.

callback Consumer: Callback to receive the storage info. This value cannot be null.

put

public void put (PutDocumentsRequest request, 
                Executor executor, 
                BatchResultCallback<StringVoid> callback)

Indexes documents into the AppSearchSession database.

Each GenericDocument object must have a schemaType field set to an AppSearchSchema type that has been previously registered by calling the setSchema(SetSchemaRequest, Executor, Executor, Consumer>) method.

Parameters
request PutDocumentsRequest: containing documents to be indexed. This value cannot be null.

executor Executor: Executor on which to invoke the callback. This value cannot be null. Callback and listener events are dispatched through this Executor, providing an easy way to control which thread is used. To dispatch events through the main thread of your application, you can use Context.getMainExecutor(). To dispatch events through a shared thread pool, you can use AsyncTask#THREAD_POOL_EXECUTOR.

callback BatchResultCallback: Callback to receive pending result of performing this operation. The keys of the returned AppSearchBatchResult are the IDs of the input documents. The values are null if they were successfully indexed, or a failed AppSearchResult otherwise. If an unexpected internal error occurs in the AppSearch service, BatchResultCallback#onSystemError will be invoked with a Throwable. This value cannot be null.

remove

public void remove (RemoveByDocumentIdRequest request, 
                Executor executor, 
                BatchResultCallback<StringVoid> callback)

Removes GenericDocument objects by document IDs in a namespace from the AppSearchSession database.

Removed documents will no longer be surfaced by search(String, SearchSpec) or getByDocumentId(GetByDocumentIdRequest, Executor, BatchResultCallback) calls.

Once the database crosses the document count or byte usage threshold, removed documents will be deleted from disk.

Parameters
request RemoveByDocumentIdRequest: RemoveByDocumentIdRequest with IDs in a namespace to remove from the index. This value cannot be null.

executor Executor: Executor on which to invoke the callback. This value cannot be null. Callback and listener events are dispatched through this Executor, providing an easy way to control which thread is used. To dispatch events through the main thread of your application, you can use Context.getMainExecutor(). To dispatch events through a shared thread pool, you can use AsyncTask#THREAD_POOL_EXECUTOR.

callback BatchResultCallback: Callback to receive the pending result of performing this operation. The keys of the returned AppSearchBatchResult are the input document IDs. The values are null on success, or a failed AppSearchResult otherwise. IDs that are not found will return a failed AppSearchResult with a result code of AppSearchResult#RESULT_NOT_FOUND. If an unexpected internal error occurs in the AppSearch service, BatchResultCallback#onSystemError will be invoked with a Throwable. This value cannot be null.

remove

public void remove (String queryExpression, 
                SearchSpec searchSpec, 
                Executor executor, 
                Consumer<AppSearchResult<Void>> callback)

Removes GenericDocuments from the index by Query. Documents will be removed if they match the queryExpression in given namespaces and schemaTypes which is set via SearchSpec.Builder.addFilterNamespaces(String...) and SearchSpec.Builder#addFilterSchemas.

An empty queryExpression matches all documents.

An empty set of namespaces or schemaTypes matches all namespaces or schemaTypes in the current database.

Parameters
queryExpression String: Query String to search. This value cannot be null.

searchSpec SearchSpec: Spec containing schemaTypes, namespaces and query expression indicates how document will be removed. All specific about how to scoring, ordering, snippeting and resulting will be ignored. This value cannot be null.

executor Executor: Executor on which to invoke the callback. This value cannot be null. Callback and listener events are dispatched through this Executor, providing an easy way to control which thread is used. To dispatch events through the main thread of your application, you can use Context.getMainExecutor(). To dispatch events through a shared thread pool, you can use AsyncTask#THREAD_POOL_EXECUTOR.

callback Consumer: Callback to receive errors resulting from removing the documents. If the operation succeeds, the callback will be invoked with null. This value cannot be null.

reportUsage

public void reportUsage (ReportUsageRequest request, 
                Executor executor, 
                Consumer<AppSearchResult<Void>> callback)

Reports usage of a particular document by namespace and ID.

A usage report represents an event in which a user interacted with or viewed a document.

For each call to reportUsage(ReportUsageRequest, Executor, Consumer>), AppSearch updates usage count and usage recency metrics for that particular document. These metrics are used for ordering search(String, SearchSpec) results by the SearchSpec#RANKING_STRATEGY_USAGE_COUNT and SearchSpec.RANKING_STRATEGY_USAGE_LAST_USED_TIMESTAMP ranking strategies.

Reporting usage of a document is optional.

Parameters
request ReportUsageRequest: The usage reporting request. This value cannot be null.

executor Executor: Executor on which to invoke the callback. This value cannot be null. Callback and listener events are dispatched through this Executor, providing an easy way to control which thread is used. To dispatch events through the main thread of your application, you can use Context.getMainExecutor(). To dispatch events through a shared thread pool, you can use AsyncTask#THREAD_POOL_EXECUTOR.

callback Consumer: Callback to receive errors. If the operation succeeds, the callback will be invoked with null. This value cannot be null.

search

public SearchResults search (String queryExpression, 
                SearchSpec searchSpec)

Retrieves documents from the open AppSearchSession that match a given query string and type of search provided.

Query strings can be empty, contain one term with no operators, or contain multiple terms and operators.

For query strings that are empty, all documents that match the SearchSpec will be returned.

For query strings with a single term and no operators, documents that match the provided query string and SearchSpec will be returned.

The following operators are supported:

  • AND (implicit)

    AND is an operator that matches documents that contain all provided terms.

    NOTE: A space between terms is treated as an "AND" operator. Explicitly including "AND" in a query string will treat "AND" as a term, returning documents that also contain "AND".

    Example: "apple AND banana" matches documents that contain the terms "apple", "and", "banana".

    Example: "apple banana" matches documents that contain both "apple" and "banana".

    Example: "apple banana cherry" matches documents that contain "apple", "banana", and "cherry".

  • OR

    OR is an operator that matches documents that contain any provided term.

    Example: "apple OR banana" matches documents that contain either "apple" or "banana".

    Example: "apple OR banana OR cherry" matches documents that contain any of "apple", "banana", or "cherry".

  • Exclusion (-)

    Exclusion (-) is an operator that matches documents that do not contain the provided term.

    Example: "-apple" matches documents that do not contain "apple".

  • Grouped Terms

    For queries that require multiple operators and terms, terms can be grouped into subqueries. Subqueries are contained within an open "(" and close ")" parenthesis.

    Example: "(donut OR bagel) (coffee OR tea)" matches documents that contain either "donut" or "bagel" and either "coffee" or "tea".

  • Property Restricts

    For queries that require a term to match a specific AppSearchSchema property of a document, a ":" must be included between the property name and the term.

    Example: "subject:important" matches documents that contain the term "important" in the "subject" property.

Additional search specifications, such as filtering by AppSearchSchema type or adding projection, can be set by calling the corresponding SearchSpec.Builder setter.

This method is lightweight. The heavy work will be done in SearchResults.getNextPage(Executor, Consumer>>).

Parameters
queryExpression String: query string to search. This value cannot be null.

searchSpec SearchSpec: spec for setting document filters, adding projection, setting term match type, etc. This value cannot be null.

Returns
SearchResults a SearchResults object for retrieved matched documents. This value cannot be null.

setSchema

public void setSchema (SetSchemaRequest request, 
                Executor workExecutor, 
                Executor callbackExecutor, 
                Consumer<AppSearchResult<SetSchemaResponse>> callback)

Sets the schema that represents the organizational structure of data within the AppSearch database.

Upon creating an AppSearchSession, setSchema(SetSchemaRequest, Executor, Executor, Consumer>) should be called. If the schema needs to be updated, or it has not been previously set, then the provided schema will be saved and persisted to disk. Otherwise, setSchema(SetSchemaRequest, Executor, Executor, Consumer>) is handled efficiently as a no-op call.

Parameters
request SetSchemaRequest: the schema to set or update the AppSearch database to. This value cannot be null.

workExecutor Executor: Executor on which to schedule heavy client-side background work such as transforming documents. This value cannot be null.

callbackExecutor Executor: Executor on which to invoke the callback. This value cannot be null. Callback and listener events are dispatched through this Executor, providing an easy way to control which thread is used. To dispatch events through the main thread of your application, you can use Context.getMainExecutor(). To dispatch events through a shared thread pool, you can use AsyncTask#THREAD_POOL_EXECUTOR.

callback Consumer: Callback to receive errors resulting from setting the schema. If the operation succeeds, the callback will be invoked with null. This value cannot be null.