@RequiresFeature(enforcement = "androidx.appsearch.app.Features#isFeatureSupported", name = Features.JOIN_SPEC_AND_QUALIFIED_ID)
@Document(name = "builtin:Nickname")
@ExperimentalAppSearchApi
public class Nickname


Represents an nickname document for another GenericDocument.

A nickname document has an indexed string field and a qualified id type that allows us to add aliases to the document referenced to by the qualified id type. This way we can match that document by a string that does not even appear in the original document.

Example: Adding a nickname "config" to the settings MobileApplication document

// Assume that the MobileApplication schema and Nickname schema have been set and settings is
// indexed as a MobileApplication document.

// Create the qualified ID for a settings app.
String settingsAppQualifiedId = DocumentIdUtil.createQualifiedId(
    "android",
    "apps-db",
    "apps",
    "com.android.settings");

// Index the "config" nickname document, linking it to the document for settings.
Nickname configNickname = new Nickname.Builder("configNs", "configId", settingsAppQualifiedId)
    .setAlternateNames(Collections.singletonList("config"))
    .build();

AppSearchBatchResult<String, Void> putResult = appSearchSession.putAsync(
    new PutDocumentsRequest.Builder().addDocuments(configNickname).build()).get();

// Assert the nickname has been indexed
if (!putResult.isSuccess()) {
   return;
}

SearchSpec nestedSearchSpec = new SearchSpec.Builder()
    .addFilterDocumentClass(Nickname.class)
    .build();

JoinSpec joinSpec = new JoinSpec.Builder("referencedQualifiedId")
    .setNestedSearch("config", nestedSearchSpec)
    .build();

SearchSpec searchSpec = new SearchSpec.Builder()
    .addFilterDocumentClass(MobileApplication.class)
    .setJoinSpec(joinSpec)
    .setRankingStrategy("len(this.childrenRankingSignals()) > 0")
    .build();

SearchResults searchResults = appSearchSession.search("", searchSpec);

// This will contain one SearchResult for the settings MobileApplication document.
List<SearchResult> results = searchResults.getNextPageAsync().get();
See also
JoinSpec

Summary

Nested types

public final class Nickname.Builder

Builder for Nickname.

Public methods

boolean
@NonNull List<String>

Returns alternative names for a document.

@NonNull String

Returns the appsearch document id for this item.

@NonNull String

Returns the namespace (or logical grouping) for this item.

@NonNull String

Returns the qualified id of the document these nicknames are for.

int

Public methods

equals

public boolean equals(Object o)

getAlternateNames

Added in 1.2.0-alpha01
public @NonNull List<StringgetAlternateNames()

Returns alternative names for a document. These are indexed. For example, you might have the alternative name "pay" for a wallet app.

getId

Added in 1.2.0-alpha01
public @NonNull String getId()

Returns the appsearch document id for this item.

getNamespace

Added in 1.2.0-alpha01
public @NonNull String getNamespace()

Returns the namespace (or logical grouping) for this item.

getReferencedQualifiedId

Added in 1.2.0-alpha01
public @NonNull String getReferencedQualifiedId()

Returns the qualified id of the document these nicknames are for.

A qualified id is a string generated by package, database, namespace, and document id.

See also
DocumentIdUtil

hashCode

public int hashCode()