Text search example
Latest stable Based on Text release 0.2.1
The runnable example compares the builder and DSL, replaces risk phrases, and collects only the first Flow alert. It demonstrates four result shapes from one search capability.
Run it
Section titled “Run it”./gradlew :examples:text-search-examples:runRepresentative output:
builder=ACCOUNT_TAKEOVER, PAYMENT_RISKdsl=password reset, card declinedredacted=user requested [ACCOUNT_TAKEOVER] before [PAYMENT_RISK]flow=ACCOUNT_TAKEOVERSet iteration and formatting order should not be treated as a public contract; the example tests assert the contained values.
Builder path
Section titled “Builder path”buildRiskAutomaton() registers password reset → ACCOUNT_TAKEOVER and card declined → PAYMENT_RISK, enables case-insensitive matching, disables overlaps, and requires whitespace-separated boundaries.
The values are application categories, not replacement text. parseText returns both keywords and values, allowing the caller to preserve evidence and make a separate policy decision.
DSL path
Section titled “DSL path”The DSL builds an automaton with the same phrases and boundary policy. Its report records matched keywords to show that the DSL is configuration syntax over the same search model, not a different engine.
Change one phrase in both builders and verify both result sets before adopting a DSL migration.
Replacement path
Section titled “Replacement path”val redacted = builderAutomaton.replaceAll(logLine) { match -> "[${match.value}]"}The transform receives the accepted match. Replacement uses the resolved match set, so overlap policy affects what is redacted.
First Flow alert
Section titled “First Flow alert”collectFirstAlert runs on a limited Dispatchers.Default view, applies a five-second timeout, and collects matchesAsFlow(logLine).take(1). This is useful when the surrounding pipeline is already coroutine-based and later matches are irrelevant.
The example also contains a bounded no-match path. A no-match result is an empty list, not an exception.
Modify it safely
Section titled “Modify it safely”- Add a third phrase and value.
- Add a sentence where the phrase is embedded without the configured whitespace boundary.
- Compare
allowOverlaps = trueandfalsewith overlapping phrases. - Add a cancellation test around Flow collection.
Continue with the Text search module, testing guide, and benchmark interpretation.