# AppSearchManager

Added in[API level 31](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)  
Summary:[Nested Classes](https://developer.android.com/reference/android/app/appsearch/AppSearchManager#nestedclasses)\|[Methods](https://developer.android.com/reference/android/app/appsearch/AppSearchManager#pubmethods)\|[Inherited Methods](https://developer.android.com/reference/android/app/appsearch/AppSearchManager#inhmethods)  

# AppSearchManager

*** ** * ** ***

[Kotlin](https://developer.android.com/reference/kotlin/android/app/appsearch/AppSearchManager "View this page in Kotlin")\|Java

`
public


class
AppSearchManager
`  
`

extends `[Object](https://developer.android.com/reference/java/lang/Object)`


``


`

|---|----------------------------------------|
| [java.lang.Object](https://developer.android.com/reference/java/lang/Object) ||
| ↳ | android.app.appsearch.AppSearchManager |

<br />

*** ** * ** ***

Provides access to the centralized AppSearch index maintained by the system.

AppSearch is an offline, on-device search library for managing structured data featuring:

- APIs to index and retrieve data via full-text search.
- An API for applications to explicitly grant read-access permission of their data to other applications.**See:[SetSchemaRequest.Builder.setSchemaTypeVisibilityForPackage(String, boolean, PackageIdentifier)](https://developer.android.com/reference/android/app/appsearch/SetSchemaRequest.Builder#setSchemaTypeVisibilityForPackage(java.lang.String,%20boolean,%20android.app.appsearch.PackageIdentifier))**
- 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, boolean)](https://developer.android.com/reference/android/app/appsearch/SetSchemaRequest.Builder#setSchemaTypeDisplayedBySystem(java.lang.String,%20boolean))**

Applications create a database by opening an[AppSearchSession](https://developer.android.com/reference/android/app/appsearch/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[AppSearchSession.setSchema](https://developer.android.com/reference/android/app/appsearch/AppSearchSession#setSchema(android.app.appsearch.SetSchemaRequest,%20java.util.concurrent.Executor,%20java.util.concurrent.Executor,%20java.util.function.Consumer<android.app.appsearch.AppSearchResult<android.app.appsearch.SetSchemaResponse>>)). The schema is composed of a collection of[AppSearchSchema](https://developer.android.com/reference/android/app/appsearch/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[GenericDocument](https://developer.android.com/reference/android/app/appsearch/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](https://developer.android.com/reference/android/app/appsearch/GenericDocument)in the database.

Once the schema has been set,[GenericDocument](https://developer.android.com/reference/android/app/appsearch/GenericDocument)objects can be put into the database and indexed by calling[AppSearchSession.put](https://developer.android.com/reference/android/app/appsearch/AppSearchSession#put(android.app.appsearch.PutDocumentsRequest,%20java.util.concurrent.Executor,%20android.app.appsearch.BatchResultCallback<java.lang.String,java.lang.Void>)).

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(\u201csubject\u201d, 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[AppSearchSession.search](https://developer.android.com/reference/android/app/appsearch/AppSearchSession#search(java.lang.String,%20android.app.appsearch.SearchSpec))and providing the query string to search for, as well as a[SearchSpec](https://developer.android.com/reference/android/app/appsearch/SearchSpec).

Alternatively,[AppSearchSession.getByDocumentId](https://developer.android.com/reference/android/app/appsearch/AppSearchSession#getByDocumentId(android.app.appsearch.GetByDocumentIdRequest,%20java.util.concurrent.Executor,%20android.app.appsearch.BatchResultCallback<java.lang.String,android.app.appsearch.GenericDocument>))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[AppSearchSession.remove(android.app.appsearch.RemoveByDocumentIdRequest, java.util.concurrent.Executor, android.app.appsearch.BatchResultCallback)](https://developer.android.com/reference/android/app/appsearch/AppSearchSession#remove(android.app.appsearch.RemoveByDocumentIdRequest,%20java.util.concurrent.Executor,%20android.app.appsearch.BatchResultCallback<java.lang.String,java.lang.Void>)), or by query via[AppSearchSession.remove(String, SearchSpec, Executor, Consumer)](https://developer.android.com/reference/android/app/appsearch/AppSearchSession#remove(java.lang.String,%20android.app.appsearch.SearchSpec,%20java.util.concurrent.Executor,%20java.util.function.Consumer<android.app.appsearch.AppSearchResult<java.lang.Void>>)).

## Summary

|                                                                                          ### Nested classes                                                                                           ||
|----------|---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| ` class` | [AppSearchManager.SearchContext](https://developer.android.com/reference/android/app/appsearch/AppSearchManager.SearchContext) Contains information about how to create the search session. |

|                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    ### Public methods                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    ||
|---------|---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| ` void` | ` `[createEnterpriseGlobalSearchSession](https://developer.android.com/reference/android/app/appsearch/AppSearchManager#createEnterpriseGlobalSearchSession(java.util.concurrent.Executor,%20java.util.function.Consumer<android.app.appsearch.AppSearchResult<android.app.appsearch.EnterpriseGlobalSearchSession>>))`(`[Executor](https://developer.android.com/reference/java/util/concurrent/Executor)` executor, `[Consumer](https://developer.android.com/reference/java/util/function/Consumer)`<`[AppSearchResult](https://developer.android.com/reference/android/app/appsearch/AppSearchResult)`<`[EnterpriseGlobalSearchSession](https://developer.android.com/reference/android/app/appsearch/EnterpriseGlobalSearchSession)`>> callback) ` Creates a new[EnterpriseGlobalSearchSession](https://developer.android.com/reference/android/app/appsearch/EnterpriseGlobalSearchSession) EnterpriseGlobalSearchSession queries data from the user\\u2019s work profile, allowing apps running on the personal profile to access a limited subset of work profile data. |
| ` void` | ` `[createGlobalSearchSession](https://developer.android.com/reference/android/app/appsearch/AppSearchManager#createGlobalSearchSession(java.util.concurrent.Executor,%20java.util.function.Consumer<android.app.appsearch.AppSearchResult<android.app.appsearch.GlobalSearchSession>>))`(`[Executor](https://developer.android.com/reference/java/util/concurrent/Executor)` executor, `[Consumer](https://developer.android.com/reference/java/util/function/Consumer)`<`[AppSearchResult](https://developer.android.com/reference/android/app/appsearch/AppSearchResult)`<`[GlobalSearchSession](https://developer.android.com/reference/android/app/appsearch/GlobalSearchSession)`>> callback) ` Creates a new[GlobalSearchSession](https://developer.android.com/reference/android/app/appsearch/GlobalSearchSession).                                                                                                                                                                                                                                                    |
| ` void` | ` `[createSearchSession](https://developer.android.com/reference/android/app/appsearch/AppSearchManager#createSearchSession(android.app.appsearch.AppSearchManager.SearchContext,%20java.util.concurrent.Executor,%20java.util.function.Consumer<android.app.appsearch.AppSearchResult<android.app.appsearch.AppSearchSession>>))`(`[AppSearchManager.SearchContext](https://developer.android.com/reference/android/app/appsearch/AppSearchManager.SearchContext)` searchContext, `[Executor](https://developer.android.com/reference/java/util/concurrent/Executor)` executor, `[Consumer](https://developer.android.com/reference/java/util/function/Consumer)`<`[AppSearchResult](https://developer.android.com/reference/android/app/appsearch/AppSearchResult)`<`[AppSearchSession](https://developer.android.com/reference/android/app/appsearch/AppSearchSession)`>> callback) ` Creates a new[AppSearchSession](https://developer.android.com/reference/android/app/appsearch/AppSearchSession).                                                                       |

| ### Inherited methods |
|-----------------------|---|
| From class` `[java.lang.Object](https://developer.android.com/reference/java/lang/Object)` ` |--------------------------------------------------------------------------------|-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------| | ` `[Object](https://developer.android.com/reference/java/lang/Object)          | ` `[clone](https://developer.android.com/reference/java/lang/Object#clone())`() ` Creates and returns a copy of this object.                                                                                                                                                      | | ` boolean`                                                                     | ` `[equals](https://developer.android.com/reference/java/lang/Object#equals(java.lang.Object))`(`[Object](https://developer.android.com/reference/java/lang/Object)` obj) ` Indicates whether some other object is "equal to" this one.                                           | | ` void`                                                                        | ` `[finalize](https://developer.android.com/reference/java/lang/Object#finalize())`() ` Called by the garbage collector on an object when garbage collection determines that there are no more references to the object.                                                          | | ` final `[Class](https://developer.android.com/reference/java/lang/Class)`<?>` | ` `[getClass](https://developer.android.com/reference/java/lang/Object#getClass())`() ` Returns the runtime class of this`Object`.                                                                                                                                                | | ` int`                                                                         | ` `[hashCode](https://developer.android.com/reference/java/lang/Object#hashCode())`() ` Returns a hash code value for the object.                                                                                                                                                 | | ` final void`                                                                  | ` `[notify](https://developer.android.com/reference/java/lang/Object#notify())`() ` Wakes up a single thread that is waiting on this object's monitor.                                                                                                                            | | ` final void`                                                                  | ` `[notifyAll](https://developer.android.com/reference/java/lang/Object#notifyAll())`() ` Wakes up all threads that are waiting on this object's monitor.                                                                                                                         | | ` `[String](https://developer.android.com/reference/java/lang/String)          | ` `[toString](https://developer.android.com/reference/java/lang/Object#toString())`() ` Returns a string representation of the object.                                                                                                                                            | | ` final void`                                                                  | ` `[wait](https://developer.android.com/reference/java/lang/Object#wait(long,%20int))`(long timeoutMillis, int nanos) ` Causes the current thread to wait until it is awakened, typically by being*notified* or*interrupted*, or until a certain amount of real time has elapsed. | | ` final void`                                                                  | ` `[wait](https://developer.android.com/reference/java/lang/Object#wait(long))`(long timeoutMillis) ` Causes the current thread to wait until it is awakened, typically by being*notified* or*interrupted*, or until a certain amount of real time has elapsed.                   | | ` final void`                                                                  | ` `[wait](https://developer.android.com/reference/java/lang/Object#wait())`() ` Causes the current thread to wait until it is awakened, typically by being*notified* or*interrupted*.                                                                                             | ||

## Public methods

### createEnterpriseGlobalSearchSession

Added in[API level 35](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)  
Also in[T Extensions 13](https://developer.android.com/sdkExtensions)  

```
public void createEnterpriseGlobalSearchSession (Executor executor, 
                Consumer<AppSearchResult<EnterpriseGlobalSearchSession>> callback)
```

Creates a new[EnterpriseGlobalSearchSession](https://developer.android.com/reference/android/app/appsearch/EnterpriseGlobalSearchSession)

EnterpriseGlobalSearchSession queries data from the user\\u2019s work profile, allowing apps running on the personal profile to access a limited subset of work profile data. Enterprise access must be explicitly enabled on schemas, and schemas may also specify additional permissions required for enterprise access.

This process requires an AppSearch native indexing file system. If it's not created, the initialization process will create one under the user's credential encrypted directory.

<br />

|                                                                                                                                                                                                                                                                                                                    Parameters                                                                                                                                                                                                                                                                                                                     ||
|------------|---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| `executor` | `Executor`: Executor on which to invoke the callback. This value cannot be`null`. Callback and listener events are dispatched through this[Executor](https://developer.android.com/reference/java/util/concurrent/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()](https://developer.android.com/reference/android/content/Context#getMainExecutor()). Otherwise, provide an[Executor](https://developer.android.com/reference/java/util/concurrent/Executor)that dispatches to an appropriate thread. <br /> |
| `callback` | `Consumer`: The[AppSearchResult](https://developer.android.com/reference/android/app/appsearch/AppSearchResult)\<[EnterpriseGlobalSearchSession](https://developer.android.com/reference/android/app/appsearch/EnterpriseGlobalSearchSession)\> of performing this operation. Or a[AppSearchResult](https://developer.android.com/reference/android/app/appsearch/AppSearchResult)with failure reason code and error information. This value cannot be`null`. <br />                                                                                                                                                                  |

### createGlobalSearchSession

Added in[API level 31](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)  

```
public void createGlobalSearchSession (Executor executor, 
                Consumer<AppSearchResult<GlobalSearchSession>> callback)
```

Creates a new[GlobalSearchSession](https://developer.android.com/reference/android/app/appsearch/GlobalSearchSession).

This process requires an AppSearch native indexing file system. If it's not created, the initialization process will create one under the user's credential encrypted directory.

<br />

|                                                                                                                                                                                                                                                                                                                    Parameters                                                                                                                                                                                                                                                                                                                     ||
|------------|---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| `executor` | `Executor`: Executor on which to invoke the callback. This value cannot be`null`. Callback and listener events are dispatched through this[Executor](https://developer.android.com/reference/java/util/concurrent/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()](https://developer.android.com/reference/android/content/Context#getMainExecutor()). Otherwise, provide an[Executor](https://developer.android.com/reference/java/util/concurrent/Executor)that dispatches to an appropriate thread. <br /> |
| `callback` | `Consumer`: The[AppSearchResult](https://developer.android.com/reference/android/app/appsearch/AppSearchResult)\<[GlobalSearchSession](https://developer.android.com/reference/android/app/appsearch/GlobalSearchSession)\> of performing this operation. Or a[AppSearchResult](https://developer.android.com/reference/android/app/appsearch/AppSearchResult)with failure reason code and error information. This value cannot be`null`. <br />                                                                                                                                                                                      |

### createSearchSession

Added in[API level 31](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)  

```
public void createSearchSession (AppSearchManager.SearchContext searchContext, 
                Executor executor, 
                Consumer<AppSearchResult<AppSearchSession>> callback)
```

Creates a new[AppSearchSession](https://developer.android.com/reference/android/app/appsearch/AppSearchSession).

This process requires an AppSearch native indexing file system. If it's not created, the initialization process will create one under the user's credential encrypted directory.

<br />

|                                                                                                                                                                                                                                                                                                                       Parameters                                                                                                                                                                                                                                                                                                                       ||
|-----------------|---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| `searchContext` | `AppSearchManager.SearchContext`: The[SearchContext](https://developer.android.com/reference/android/app/appsearch/AppSearchManager.SearchContext)contains all information to create a new[AppSearchSession](https://developer.android.com/reference/android/app/appsearch/AppSearchSession)This value cannot be`null`. <br />                                                                                                                                                                                                                                                                                                        |
| `executor`      | `Executor`: Executor on which to invoke the callback. This value cannot be`null`. Callback and listener events are dispatched through this[Executor](https://developer.android.com/reference/java/util/concurrent/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()](https://developer.android.com/reference/android/content/Context#getMainExecutor()). Otherwise, provide an[Executor](https://developer.android.com/reference/java/util/concurrent/Executor)that dispatches to an appropriate thread. <br /> |
| `callback`      | `Consumer`: The[AppSearchResult](https://developer.android.com/reference/android/app/appsearch/AppSearchResult)\<[AppSearchSession](https://developer.android.com/reference/android/app/appsearch/AppSearchSession)\> of performing this operation. Or a[AppSearchResult](https://developer.android.com/reference/android/app/appsearch/AppSearchResult)with failure reason code and error information. This value cannot be`null`. <br />                                                                                                                                                                                            |