SemanticSearchNode


@ExperimentalAppSearchApi
public final class SemanticSearchNode implements FunctionNode


FunctionNode that represents the semanticSearch function.

The semanticSearch function matches all documents that have at least one embedding vector with a matching model signature (see getModelSignature) and a similarity score within the range specified based on the provided metric.

This node can be used to build a query that contains the semanticSearch function. For example, the node SemanticSearchNode(0, -0.5, 0.5, DOT_PRODUCT) is equivalent to the query `semanticSearch(getEmbeddingParameter(0), -0.5, 0.5, "DOT_PRODUCT")`.

Summary

Public constructors

SemanticSearchNode(int vectorIndex)

Constructor for SemanticSearchNode representing the semanticSearch function in a query.

SemanticSearchNode(int vectorIndex, float lowerBound)

Constructor for SemanticSearchNode representing the semanticSearch function in a query.

SemanticSearchNode(int vectorIndex, float lowerBound, float upperBound)

Constructor for SemanticSearchNode representing the semanticSearch function in a query.

SemanticSearchNode(
    int vectorIndex,
    float lowerBound,
    float upperBound,
    int distanceMetric
)

Constructor for SemanticSearchNode representing the semanticSearch function in a query.

Public methods

boolean
int

Returns the distance metric used to calculated similarity between embedding vectors.

@NonNull String

Returns the name of the function represented by SemanticSearchNode.

float

Returns the lower bound of the range of values similarity scores must fall in.

float

Returns the upper bound of the range of values similarity scores must fall in.

int

Returns the index of the embedding vector used in semanticSearch.

int
void
setBounds(float lowerBound, float upperBound)

Sets the bounds of the range of values that semanticSearch will search against.

void
setDistanceMetric(int distanceMetric)

Sets how similarity is calculated between embedding vectors.

void
setVectorIndex(int vectorIndex)

Sets the index of the embedding vector that semanticSearch will use.

@NonNull String

Get the query string representation of SemanticSearchNode.

Inherited Constants

From androidx.appsearch.ast.FunctionNode
static final String
FUNCTION_NAME_GET_SEARCH_STRING_PARAMETER = "getSearchStringParameter"

Name of the query function represented by androidx.appsearch.ast.query.GetSearchStringParameterNode.

static final String

Name of the query function represented by androidx.appsearch.ast.query.HasPropertyNode.

static final String
FUNCTION_NAME_PROPERTY_DEFINED = "propertyDefined"

Name of the query function represented by androidx.appsearch.ast.query.PropertyDefinedNode.

static final String

Name of the query function represented by androidx.appsearch.ast.query.SearchNode.

static final String

Name of the query function represented by androidx.appsearch.ast.query.SemanticSearchNode.

Inherited methods

From androidx.appsearch.ast.Node
@NonNull List<Node>

Get a list of the node's child Nodes.

Public constructors

SemanticSearchNode

Added in 1.1.0-alpha07
public SemanticSearchNode(int vectorIndex)

Constructor for SemanticSearchNode representing the semanticSearch function in a query.

By default:

  • The default set by the user and returned by getDefaultEmbeddingSearchMetricType will be used to determine similarity between embedding vectors. If no default is set, cosine similarity will be used.
  • The upper bound on similarity scores for an embedding vector such that the associated document will be returned is positive infinity.
  • The lower bound on similarity scores for an embedding vector such that the associated document will be returned is negative infinity.

See SemanticSearchNode for an explanation of the parameters.

SemanticSearchNode

Added in 1.1.0-alpha07
public SemanticSearchNode(int vectorIndex, float lowerBound)

Constructor for SemanticSearchNode representing the semanticSearch function in a query.

By default:

  • The default set by the user and returned by getDefaultEmbeddingSearchMetricType will be used to determine similarity between embedding vectors. If no default is set, cosine similarity will be used.
  • The upper bound on similarity scores for an embedding vector such that the associated document will be returned is positive infinity.

See SemanticSearchNode for an explanation of the parameters.

SemanticSearchNode

Added in 1.1.0-alpha07
public SemanticSearchNode(int vectorIndex, float lowerBound, float upperBound)

Constructor for SemanticSearchNode representing the semanticSearch function in a query.

By default:

  • The default set by the user and returned by getDefaultEmbeddingSearchMetricType will be used to determine similarity between embedding vectors. If no default is set, cosine similarity will be used.

See SemanticSearchNode for an explanation of the parameters.

SemanticSearchNode

Added in 1.1.0-alpha07
public SemanticSearchNode(
    int vectorIndex,
    float lowerBound,
    float upperBound,
    int distanceMetric
)

Constructor for SemanticSearchNode representing the semanticSearch function in a query.

Parameters
int vectorIndex

The index of the embedding vector in the list of vectors returned by getEmbeddingParameters to use in the search.

float lowerBound

The lower bound on similarity score for a embedding vector such that the associated document will be returned.

float upperBound

The upper bound on similarity score for a embedding vector such that the associated document will be returned.

int distanceMetric

How distance between embedding vectors will be calculated.

Public methods

equals

public boolean equals(Object o)

getDistanceMetric

Added in 1.1.0-alpha07
public int getDistanceMetric()

Returns the distance metric used to calculated similarity between embedding vectors.

getFunctionName

Added in 1.1.0-alpha07
public @NonNull String getFunctionName()

Returns the name of the function represented by SemanticSearchNode.

getLowerBound

Added in 1.1.0-alpha07
public float getLowerBound()

Returns the lower bound of the range of values similarity scores must fall in.

getUpperBound

Added in 1.1.0-alpha07
public float getUpperBound()

Returns the upper bound of the range of values similarity scores must fall in.

getVectorIndex

Added in 1.1.0-alpha07
public int getVectorIndex()

Returns the index of the embedding vector used in semanticSearch.

hashCode

public int hashCode()

setBounds

Added in 1.1.0-alpha07
public void setBounds(float lowerBound, float upperBound)

Sets the bounds of the range of values that semanticSearch will search against.

Parameters
float lowerBound

The lower bound of the range of values.

float upperBound

The upper bound of the range of values.

setDistanceMetric

Added in 1.1.0-alpha07
public void setDistanceMetric(int distanceMetric)

Sets how similarity is calculated between embedding vectors.

Parameters
int distanceMetric

How similarity is calculated between embedding vectors.

setVectorIndex

Added in 1.1.0-alpha07
public void setVectorIndex(int vectorIndex)

Sets the index of the embedding vector that semanticSearch will use.

toString

public @NonNull String toString()

Get the query string representation of SemanticSearchNode.

The query string representation will be the function name, followed by the fields of SemanticSearchNode as arguments, surrounded by parentheses, but formatted in the following way:

  • The vector index will appear as an argument to the function `getEmbeddingParameter`.
  • The lower bound and upper bound will appear unchanged.
  • The distance metric will be mapped to its corresponding string literal representation. For example, if the distance metric is 1, then the corresponding string literal would be "COSINE".
For example, the node SemanticSearchNode(0, -1.5f, 2, SearchSpec.EMBEDDING_SEARCH_METRIC_TYPE_COSINE) will look like `semanticSearch(getEmbeddingParameter(0), -1.5, 2, "COSINE")`

If possible, default parameters will be left out of the query string. For example the node SemanticSearchNode(0) will look like `semanticSearch(getEmbeddingParameter(0))`. However if some defaults are set and unset, the defaults will be included in the query string. For example, if the user does something like this:

SemanticSearchNode semanticSearchNode = new SemanticSearchNode(0, -1, 1);
semanticSearchNode.setBounds(Float.NEGATIVE_INFINITY, 1);
Then the query string will look like `semanticSearch(getEmbeddingParameter(0), -Float.MAX_VALUE, 1)` where Float.MAX_VALUE is the max value of float.