Class SearchRequest.Builder

java.lang.Object
org.springframework.ai.vectorstore.SearchRequest.Builder
Enclosing class:
SearchRequest

public static class SearchRequest.Builder extends Object
SearchRequest Builder.
  • Constructor Details

    • Builder

      public Builder()
  • Method Details

    • query

      public SearchRequest.Builder query(String query)
      Parameters:
      query - Text to use for embedding similarity comparison.
      Returns:
      this builder.
    • topK

      public SearchRequest.Builder topK(int topK)
      Parameters:
      topK - the top 'k' similar results to return.
      Returns:
      this builder.
    • similarityThreshold

      public SearchRequest.Builder similarityThreshold(double threshold)
      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

      public SearchRequest.Builder similarityThresholdAll()
      Sets disables the similarity threshold by setting it to 0.0 - all results are accepted.
      Returns:
      this builder.
    • filterExpression

      public SearchRequest.Builder filterExpression(@Nullable Filter.Expression expression)
      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 the Document.getMetadata() schema is:
      
       {
       "country": <Text>,
       "city": <Text>,
       "year": <Number>,
       "price": <Decimal>,
       "isActive": <Boolean>
       &#125;
       
      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:
      
       var 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))));
       
      The Filter.Expression is portable across all vector stores.
      The FilterExpressionBuilder is a DSL creating expressions programmatically:
      
       var b = new FilterExpressionBuilder();
       var exp = b.and(
       		b.eq("country", "UK"),
       		b.and(
       			b.gte("year", 2020),
       			b.eq("isActive", true)));
       
      The FilterExpressionTextParser converts textual, SQL like filter expression language into Filter.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

      public SearchRequest.Builder filterExpression(@Nullable String textExpression)
      Document metadata filter expression. For example if your Document.getMetadata() has a schema like:
      
       &#123;
       "country": <Text>,
       "city": <Text>,
       "year": <Number>,
       "price": <Decimal>,
       "isActive": <Boolean>
       &#125;
       
      then you can constrain the search result with metadata filter expressions like:
      
       country == 'UK' && year >= 2020 && isActive == true
       Or
       country == 'BG' && (city NOT IN ['Sofia', 'Plovdiv'] || price < 134.34)
       
      This ensures that the response contains only embeddings that match the specified filer criteria.
      The declarative, SQL like, filter syntax is portable across all vector stores supporting the filter search feature.
      The FilterExpressionTextParser is used to convert the text filter expression into Filter.Expression.
      Parameters:
      textExpression - declarative, portable, SQL like, metadata filter syntax. The 'null' value stands for no expression filters.
      Returns:
      this.builder
    • build

      public SearchRequest build()