Interface MatchingStrategy
- Functional Interface:
- This is a functional interface and can therefore be used as the assignment target for a lambda expression or method reference.
This abstraction supports various RAG use cases beyond simple document ID matching, including knowledge graph triples, API responses, and semantic matching.
-
Method Summary
Modifier and TypeMethodDescriptionstatic MatchingStrategyallOf(MatchingStrategy... strategies) Creates a composite strategy that matches only if ALL strategies match.static MatchingStrategyanyOf(MatchingStrategy... strategies) Creates a composite strategy that matches if ANY of the strategies match.static MatchingStrategybyContainment(boolean normalize) Matches string items by checking if one contains the other.static MatchingStrategyMatches items usingObjects.equals(Object, Object).static MatchingStrategyMatches Map items by comparing a specific field.static MatchingStrategyMatches Map items by comparing multiple fields.static MatchingStrategybyIdentifier(Function<Object, Object> identifierExtractor) Matches items by extracting an identifier and comparing with equality.static MatchingStrategyMatches string items case-insensitively.default longcountMatches(Collection<?> retrieved, Collection<?> expected) Counts how many retrieved items match any expected item.static MatchingStrategycustom(BiPredicate<Object, Object> predicate) Matches items using a custom predicate.static MatchingStrategyCreates an LLM-based matching strategy.booleanDetermines if a retrieved item matches an expected item.
-
Method Details
-
matches
Determines if a retrieved item matches an expected item.- Parameters:
retrieved- the item from the retrieval resultsexpected- the item from the ground truth- Returns:
- true if the items match
-
countMatches
Counts how many retrieved items match any expected item.Default implementation iterates through all pairs. Implementations may override for better performance with specific data structures.
- Parameters:
retrieved- the retrieved itemsexpected- the expected (ground truth) items- Returns:
- the number of retrieved items that match at least one expected item
-
byEquality
Matches items usingObjects.equals(Object, Object).Suitable for simple string IDs or objects with proper equals implementation.
-
byIdentifier
Matches items by extracting an identifier and comparing with equality.Useful when items are complex objects but have a unique identifier.
- Parameters:
identifierExtractor- function to extract identifier from an item- Returns:
- a matching strategy that compares extracted identifiers
-
byField
Matches Map items by comparing a specific field.Useful for JSON-like structures where items have an "id" or similar field.
- Parameters:
fieldName- the field name to compare- Returns:
- a matching strategy that compares the specified field
-
byFields
Matches Map items by comparing multiple fields.Useful for composite keys like knowledge graph triples (subject, predicate, object).
- Parameters:
fieldNames- the field names that must all match- Returns:
- a matching strategy that compares all specified fields
-
custom
Matches items using a custom predicate.- Parameters:
predicate- the matching predicate- Returns:
- a matching strategy using the predicate
-
caseInsensitive
Matches string items case-insensitively. -
byContainment
Matches string items by checking if one contains the other.Useful for partial text matching where retrieved chunks may be substrings or superstrings of expected content.
- Parameters:
normalize- whether to normalize whitespace and case- Returns:
- a matching strategy for containment matching
-
llmBased
Creates an LLM-based matching strategy.Uses an LLM to determine semantic equivalence between items. This is the most flexible but also most expensive option.
- Parameters:
judge- the LLM judge to use for matching- Returns:
- a matching strategy using LLM-based comparison
-
anyOf
Creates a composite strategy that matches if ANY of the strategies match.- Parameters:
strategies- the strategies to combine- Returns:
- a strategy that returns true if any sub-strategy matches
-
allOf
Creates a composite strategy that matches only if ALL strategies match.- Parameters:
strategies- the strategies to combine- Returns:
- a strategy that returns true only if all sub-strategies match
-