Class FilterHelper

java.lang.Object
org.springframework.ai.vectorstore.filter.FilterHelper

public class FilterHelper extends Object
Helper class providing various boolean transformation.
Author:
Christian Tzolov
  • Method Details

    • negate

      public static Filter.Operand negate(Filter.Operand operand)
      Transforms the input expression into a semantically equivalent one with negation operators propagated thought the expression tree by following the negation rules:
              NOT(NOT(a)) = a
      
              NOT(a AND b) = NOT(a) OR NOT(b)
              NOT(a OR b) = NOT(a) AND NOT(b)
      
              NOT(a EQ b) = a NE b
              NOT(a NE b) = a EQ b
      
              NOT(a GT b) = a LTE b
              NOT(a GTE b) = a LT b
      
              NOT(a LT b) = a GTE b
              NOT(a LTE b) = a GT b
      
              NOT(a IN [...]) = a NIN [...]
              NOT(a NIN [...]) = a IN [...]
       
      Parameters:
      operand - Filter expression to negate.
      Returns:
      Returns an negation of the input expression.
    • expandIn

      public static void expandIn(Filter.Expression exp, StringBuilder context, FilterExpressionConverter filterExpressionConverter)
      Expands the IN into a semantically equivalent boolean expressions of ORs of EQs. Useful for providers that don't provide native IN support. For example the
       foo IN ["bar1", "bar2", "bar3"]
       
      expression is equivalent to
       foo == "bar1" || foo == "bar2" || foo == "bar3" (e.g. OR(foo EQ "bar1" OR(foo EQ "bar2" OR(foo EQ "bar3")))
       
      Parameters:
      exp - input IN expression.
      context - Output native expression.
      filterExpressionConverter - FilterExpressionConverter used to compose the OR and EQ expanded expressions.
    • expandNin

      public static void expandNin(Filter.Expression exp, StringBuilder context, FilterExpressionConverter filterExpressionConverter)
      Expands the NIN (e.g. NOT IN) into a semantically equivalent boolean expressions of ANDs of NEs. Useful for providers that don't provide native NIN support.
      For example the
       foo NIN ["bar1", "bar2", "bar3"] (or foo NOT IN ["bar1", "bar2", "bar3"])
       
      express is equivalent to
       foo != "bar1" && foo != "bar2" && foo != "bar3" (e.g. AND(foo NE "bar1" AND( foo NE "bar2" OR(foo NE "bar3"))) )
       
      Parameters:
      exp - input NIN expression.
      context - Output native expression.
      filterExpressionConverter - FilterExpressionConverter used to compose the AND and NE expanded expressions.