GenericDocument


public class GenericDocument


Represents a document unit.

Documents contain structured data conforming to their AppSearchSchema type. Each document is uniquely identified by a namespace and a String ID within that namespace.

Documents are constructed either by using the GenericDocument.Builder or providing an annotated Document data class.

Summary

Nested types

public class GenericDocument.Builder<BuilderType extends GenericDocument.Builder>

The builder class for GenericDocument.

Protected constructors

Creates a new GenericDocument from an existing instance.

Public methods

boolean
static @NonNull GenericDocument

Converts an instance of a class annotated with \@Document into an instance of GenericDocument.

long

Returns the creation timestamp of the GenericDocument, in milliseconds.

@NonNull String

Returns the unique identifier of the GenericDocument.

@NonNull String

Returns the namespace of the GenericDocument.

@Nullable Object

Retrieves the property value with the given path as Object.

@Nullable AppSearchBlobHandle

Retrieves an AppSearchBlobHandle property by path.

@Nullable AppSearchBlobHandle[]

Retrieves a repeated AppSearchBlobHandle[] property by path.

boolean

Retrieves a boolean property by path.

@Nullable boolean[]

Retrieves a repeated boolean property by path.

@Nullable byte[]

Retrieves a byte[] property by path.

@Nullable byte[][]

Retrieves a byte[][] property by path.

@Nullable GenericDocument

Retrieves a GenericDocument property by path.

@Nullable GenericDocument[]

Retrieves a repeated GenericDocument property by path.

double

Retrieves a double property by path.

@Nullable double[]

Retrieves a repeated double property by path.

@Nullable EmbeddingVector

Retrieves an EmbeddingVector property by path.

@Nullable EmbeddingVector[]

Retrieves a repeated EmbeddingVector[] property by path.

long

Retrieves a long property by path.

@Nullable long[]

Retrieves a repeated long[] property by path.

@NonNull Set<String>

Returns the names of all properties defined in this document.

@Nullable String

Retrieves a String property by path.

@Nullable String[]

Retrieves a repeated String property by path.

@NonNull String

Returns the AppSearchSchema type of the GenericDocument.

int

Returns the score of the GenericDocument.

long

Returns the TTL (time-to-live) of the GenericDocument, in milliseconds.

int
@NonNull T
<T> toDocumentClass(@NonNull Class<T> documentClass)

Converts this GenericDocument into an instance of the provided document class.

@NonNull T
@ExperimentalAppSearchApi
<T> toDocumentClass(
    @NonNull Class<T> documentClass,
    @NonNull DocumentClassMappingContext documentClassMappingContext
)

Converts this GenericDocument into an instance of the provided document class.

@NonNull String

Protected constructors

GenericDocument

Added in 1.1.0
protected GenericDocument(@NonNull GenericDocument document)

Creates a new GenericDocument from an existing instance.

This method should be only used by constructor of a subclass.

Public methods

equals

public boolean equals(@Nullable Object other)

fromDocumentClass

Added in 1.1.0
public static @NonNull GenericDocument fromDocumentClass(@NonNull Object document)

Converts an instance of a class annotated with \@Document into an instance of GenericDocument.

Parameters
@NonNull Object document

An instance of a class annotated with \@Document.

Returns
@NonNull GenericDocument

an instance of GenericDocument produced by converting document.

Throws
androidx.appsearch.exceptions.AppSearchException

if no generated conversion class exists on the classpath for the given document class or an unexpected error occurs during conversion.

See also
GenericDocument

getCreationTimestampMillis

Added in 1.1.0
public long getCreationTimestampMillis()

Returns the creation timestamp of the GenericDocument, in milliseconds.

The value is in the currentTimeMillis time base.

getId

Added in 1.1.0
public @NonNull String getId()

Returns the unique identifier of the GenericDocument.

getNamespace

Added in 1.1.0
public @NonNull String getNamespace()

Returns the namespace of the GenericDocument.

getProperty

Added in 1.1.0
public @Nullable Object getProperty(@NonNull String path)

Retrieves the property value with the given path as Object.

A path can be a simple property name, such as those returned by getPropertyNames. It may also be a dot-delimited path through the nested document hierarchy, with nested GenericDocument properties accessed via '.' and repeated properties optionally indexed into via [n].

For example, given the following GenericDocument:

    (Message) {
        from: "sender@example.com"
        to: [{
            name: "Albert Einstein"
            email: "einstein@example.com"
          }, {
            name: "Marie Curie"
            email: "curie@example.com"
          }]
        tags: ["important", "inbox"]
        subject: "Hello"
    }

Here are some example paths and their results:

  • "from" returns "sender@example.com" as a String array with one element
  • "to" returns the two nested documents containing contact information as a GenericDocument array with two elements
  • "to[1]" returns the second nested document containing Marie Curie's contact information as a GenericDocument array with one element
  • "to[1].email" returns "curie@example.com"
  • "to[100].email" returns null as this particular document does not have that many elements in its "to" array.
  • "to.email" aggregates emails across all nested documents that have them, returning ["einstein@example.com", "curie@example.com"] as a String array with two elements.

If you know the expected type of the property you are retrieving, it is recommended to use one of the typed versions of this method instead, such as getPropertyString or getPropertyStringArray.

If the property was assigned as an empty array using one of the Builder#setProperty functions, this method will return an empty array. If no such property exists at all, this method returns null.

Parameters
@NonNull String path

The path to look for.

Returns
@Nullable Object

The entry with the given path as an object or null if there is no such path. The returned object will be one of the following types: String[], long[], double[], boolean[], byte[][], GenericDocument[].

getPropertyBlobHandle

Added in 1.1.0
@ExperimentalAppSearchApi
public @Nullable AppSearchBlobHandle getPropertyBlobHandle(@NonNull String path)

Retrieves an AppSearchBlobHandle property by path.

See getProperty for a detailed description of the path syntax.

See openBlobForReadAsync for how to use AppSearchBlobHandle to retrieve blob data.

Parameters
@NonNull String path

The path to look for.

Returns
@Nullable AppSearchBlobHandle

The first AppSearchBlobHandle associated with the given path or null if there is no such value or the value is of a different type.

getPropertyBlobHandleArray

Added in 1.1.0
@ExperimentalAppSearchApi
public @Nullable AppSearchBlobHandle[] getPropertyBlobHandleArray(@NonNull String path)

Retrieves a repeated AppSearchBlobHandle[] property by path.

See getProperty for a detailed description of the path syntax.

If the property has not been set via setPropertyBlobHandle, this method returns null.

If it has been set via setPropertyBlobHandle to an empty AppSearchBlobHandle[], this method returns an empty AppSearchBlobHandle[].

Parameters
@NonNull String path

The path to look for.

Returns
@Nullable AppSearchBlobHandle[]

The AppSearchBlobHandle[] associated with the given path, or null if no value is set or the value is of a different type.

getPropertyBoolean

Added in 1.1.0
public boolean getPropertyBoolean(@NonNull String path)

Retrieves a boolean property by path.

See getProperty for a detailed description of the path syntax.

Parameters
@NonNull String path

The path to look for.

Returns
boolean

The first boolean associated with the given path or default value false if there is no such value or the value is of a different type.

getPropertyBooleanArray

Added in 1.1.0
public @Nullable boolean[] getPropertyBooleanArray(@NonNull String path)

Retrieves a repeated boolean property by path.

See getProperty for a detailed description of the path syntax.

If the property has not been set via setPropertyBoolean, this method returns null.

If it has been set via setPropertyBoolean to an empty boolean[], this method returns an empty boolean[].

Parameters
@NonNull String path

The path to look for.

Returns
@Nullable boolean[]

The boolean[] associated with the given path, or null if no value is set or the value is of a different type.

getPropertyBytes

Added in 1.1.0
public @Nullable byte[] getPropertyBytes(@NonNull String path)

Retrieves a byte[] property by path.

See getProperty for a detailed description of the path syntax.

Parameters
@NonNull String path

The path to look for.

Returns
@Nullable byte[]

The first byte[] associated with the given path or null if there is no such value or the value is of a different type.

getPropertyBytesArray

Added in 1.1.0
public @Nullable byte[][] getPropertyBytesArray(@NonNull String path)

Retrieves a byte[][] property by path.

See getProperty for a detailed description of the path syntax.

If the property has not been set via setPropertyBytes, this method returns null.

If it has been set via setPropertyBytes to an empty byte[][], this method returns an empty byte[][].

Parameters
@NonNull String path

The path to look for.

Returns
@Nullable byte[][]

The byte[][] associated with the given path, or null if no value is set or the value is of a different type.

getPropertyDocument

Added in 1.1.0
public @Nullable GenericDocument getPropertyDocument(@NonNull String path)

Retrieves a GenericDocument property by path.

See getProperty for a detailed description of the path syntax.

Parameters
@NonNull String path

The path to look for.