Class SearchRequest.Builder
java.lang.Object
org.springframework.ai.vectorstore.SearchRequest.Builder
- Enclosing class:
- SearchRequest
SearchRequest Builder.
-
Constructor Summary
-
Method Summary
Modifier and TypeMethodDescriptionbuild()
filterExpression
(String textExpression) Document metadata filter expression.filterExpression
(Filter.Expression expression) Retrieves documents by query embedding similarity and matching the filters.similarityThreshold
(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.topK
(int topK)
-
Constructor Details
-
Builder
public Builder()
-
-
Method Details
-
query
- Parameters:
query
- Text to use for embedding similarity comparison.- Returns:
- this builder.
-
topK
- Parameters:
topK
- the top 'k' similar results to return.- Returns:
- this builder.
-
similarityThreshold
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.
-
similarityThresholdAll
Sets disables the similarity threshold by setting it to 0.0 - all results are accepted.- Returns:
- this builder.
-
filterExpression
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.
-
filterExpression
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
-
build
-