Class SearchRequest
java.lang.Object
org.springframework.ai.vectorstore.SearchRequest
Similarity search request builder. Use the
query(String)
, defaults()
or from(SearchRequest)
factory methods to create a new SearchRequest
instance and then apply the 'with' methods to alter the default values.- Author:
- Christian Tzolov
-
Field Summary
Modifier and TypeFieldDescriptionstatic final int
Default value for the top 'k' similar results to return.static final double
Similarity threshold that accepts all search scores. -
Method Summary
Modifier and TypeMethodDescriptionstatic SearchRequest
defaults()
Create a newSearchRequest
builder instance with an empty embedding query string.boolean
static SearchRequest
from
(SearchRequest originalSearchRequest) Copy an existingSearchRequest
instance.getQuery()
double
int
getTopK()
boolean
int
hashCode()
static SearchRequest
Create a newSearchRequest
builder instance with specified embedding query string.toString()
withFilterExpression
(String textExpression) Document metadata filter expression.withFilterExpression
(Filter.Expression expression) Retrieves documents by query embedding similarity and matching the filters.withSimilarityThreshold
(double threshold) Similarity threshold score to filter the search response by.Sets disables the similarity threshold by setting it to 0.0 - all results are accepted.withTopK
(int topK)
-
Field Details
-
SIMILARITY_THRESHOLD_ACCEPT_ALL
public static final double SIMILARITY_THRESHOLD_ACCEPT_ALLSimilarity threshold that accepts all search scores. A threshold value of 0.0 means any similarity is accepted or disable the similarity threshold filtering. A threshold value of 1.0 means an exact match is required.- See Also:
-
DEFAULT_TOP_K
public static final int DEFAULT_TOP_KDefault value for the top 'k' similar results to return.- See Also:
-
query
-
-
Method Details
-
query
Create a newSearchRequest
builder instance with specified embedding query string.- Parameters:
query
- Text to use for embedding similarity comparison.- Returns:
- Returns new
SearchRequest
builder instance.
-
defaults
Create a newSearchRequest
builder instance with an empty embedding query string. Use thewithQuery(String query)
to set/update the embedding query text.- Returns:
- Returns new
SearchRequest
builder instance.
-
from
Copy an existingSearchRequest
instance.- Parameters:
originalSearchRequest
-SearchRequest
instance to copy.- Returns:
- Returns new
SearchRequest
builder instance.
-
withQuery
- Parameters:
query
- Text to use for embedding similarity comparison.- Returns:
- this builder.
-
withTopK
- Parameters:
topK
- the top 'k' similar results to return.- Returns:
- this builder.
-
withSimilarityThreshold
Similarity threshold score to filter the search response by. Only documents with similarity score equal or greater than the 'threshold' will be returned. Note that this is a post-processing step performed on the client not the server side. A threshold value of 0.0 means any similarity is accepted or disable the similarity threshold filtering. A threshold value of 1.0 means an exact match is required.- Parameters:
threshold
- The lower bound of the similarity score.- Returns:
- this builder.
-
withSimilarityThresholdAll
Sets disables the similarity threshold by setting it to 0.0 - all results are accepted.- Returns:
- this builder.
-
withFilterExpression
Retrieves documents by query embedding similarity and matching the filters. Value of 'null' means that no metadata filters will be applied to the search. For example if theDocument.getMetadata()
schema is:
you can constrain the search result to only UK countries with isActive=true and year equal or greater 2020. You can build this such metadata filter programmatically like this:{ "country": <Text>, "city": <Text>, "year": <Number>, "price": <Decimal>, "isActive": <Boolean> }
Thevar exp = new Filter.Expression(AND, new Expression(EQ, new Key("country"), new Value("UK")), new Expression(AND, new Expression(GTE, new Key("year"), new Value(2020)), new Expression(EQ, new Key("isActive"), new Value(true))));
Filter.Expression
is portable across all vector stores.
TheFilterExpressionBuilder
is a DSL creating expressions programmatically:
Thevar b = new FilterExpressionBuilder(); var exp = b.and( b.eq("country", "UK"), b.and( b.gte("year", 2020), b.eq("isActive", true)));
FilterExpressionTextParser
converts textual, SQL like filter expression language intoFilter.Expression
:var parser = new FilterExpressionTextParser(); var exp = parser.parse("country == 'UK' && isActive == true && year >=2020");
- Parameters:
expression
-Filter.Expression
instance used to define the metadata filter criteria. The 'null' value stands for no expression filters.- Returns:
- this builder.
-
withFilterExpression
Document metadata filter expression. For example if yourDocument.getMetadata()
has a schema like:
then you can constrain the search result with metadata filter expressions like:{ "country": <Text>, "city": <Text>, "year": <Number>, "price": <Decimal>, "isActive": <Boolean> }
This ensures that the response contains only embeddings that match the specified filer criteria.country == 'UK' && year >= 2020 && isActive == true Or country == 'BG' && (city NOT IN ['Sofia', 'Plovdiv'] || price < 134.34)
The declarative, SQL like, filter syntax is portable across all vector stores supporting the filter search feature.
TheFilterExpressionTextParser
is used to convert the text filter expression intoFilter.Expression
.- Parameters:
textExpression
- declarative, portable, SQL like, metadata filter syntax. The 'null' value stands for no expression filters.- Returns:
- this.builder
-
getQuery
-
getTopK
public int getTopK() -
getSimilarityThreshold
public double getSimilarityThreshold() -
getFilterExpression
-
hasFilterExpression
public boolean hasFilterExpression() -
toString
-
equals
-
hashCode
public int hashCode()
-