Class ContainsConstraint

java.lang.Object
io.fluxcapacitor.common.api.search.constraints.PathConstraint
io.fluxcapacitor.common.api.search.constraints.ContainsConstraint
All Implemented Interfaces:
Constraint

public class ContainsConstraint extends PathConstraint
A constraint that matches document entries containing the specified phrase, with optional wildcard-like flexibility.

This constraint can simulate different types of text searches based on the configuration:

  • prefixSearch = true: Matches entries that end with the phrase (e.g., *day matches "Monday", "Tuesday").
  • postfixSearch = true: Matches entries that start with the phrase (e.g., mon* matches "Monday", "Monster").
  • Both prefixSearch and postfixSearch: Matches entries that contain the phrase anywhere (e.g., *mon* matches "Monday", "Common", "Commoner").
  • Both prefixSearch and postfixSearch disabled (default): Matches the full word exactly as it appears (normalized and case-insensitive).

If splitInTerms is true, the input phrase is split into individual normalized terms, and each term is matched independently using the same search behavior. All terms must be found in the same document entry for the match to succeed.

Matching is performed against the normalized form of each document entry using regular expressions if filtering is done in-memory. When using Flux Platform, this constraint is evaluated directly within the backing data store whenever possible.

If no paths are provided, the constraint matches all paths within the document.

Performance Considerations

  • Terms using a prefix or postfix wildcard (e.g., user* or *name) are resolved at the data store level and typically fast.
  • Terms using both prefix and postfix wildcards (e.g., *log*) are not natively supported by the backend and require in-memory filtering, which can be slow for large result sets.
  • Similarly, prefix or postfix matches with terms shorter than 3 characters are also evaluated in-memory.
    Note: This limitation does not apply to complete word matches (i.e., without wildcards). For example, "to" as an exact term is efficient, but to* or *to are not.
  • When possible, prefer longer terms and structure queries to enable efficient execution via the underlying document store.
See Also:
  • Constructor Details

    • ContainsConstraint

      public ContainsConstraint()
  • Method Details

    • contains

      public static Constraint contains(String phrase, String... paths)
      Create a basic ContainsConstraint that checks if the given phrase exists anywhere in the entry, using default full-word matching (i.e., neither prefix nor postfix logic).
      Parameters:
      phrase - the phrase to match
      paths - optional field paths to restrict the search to
      Returns:
      a Constraint that matches if the phrase is found
    • contains

      public static Constraint contains(String phrase, boolean prefixSearch, boolean postfixSearch, String... paths)
      Create a constraint that allows prefix and/or postfix matching of a given phrase.

      This provides fine-grained control over how terms are matched:

      • prefixSearch = true: allows matches at the start of words
      • postfixSearch = true: allows matches at the end of words
      Parameters:
      phrase - the phrase to match
      prefixSearch - whether to allow entries that end with the phrase, e.g.: *hat
      postfixSearch - whether to allow entries that start with the phrase, e.g.: hat*
      paths - optional field paths to restrict the match to
      Returns:
      a Constraint using the specified match settings
    • contains

      public static Constraint contains(String phrase, boolean prefixSearch, boolean postfixSearch, boolean splitInTerms, String... paths)
      Create a constraint that optionally splits the input phrase into individual terms and combines the resulting constraints using AllConstraint.

      This is especially useful when you want all words in a multi-word search query to be matched.

      Parameters:
      phrase - the full phrase to optionally split and match
      prefixSearch - whether to allow entries that end with the phrase, e.g.: *hat
      postfixSearch - whether to allow entries that start with the phrase, e.g.: hat*
      splitInTerms - if true, splits the phrase into individual normalized terms
      paths - optional field paths to restrict the match
      Returns:
      a compound AllConstraint if multiple terms, or a single ContainsConstraint
    • matches

      protected boolean matches(Document.Entry entry)
      Checks whether a single document entry matches the constraint. The entry is converted to a normalized phrase string, and the compiled regular expression is applied.
      Specified by:
      matches in class PathConstraint
      Parameters:
      entry - the document entry
      Returns:
      true if the entry matches the pattern, otherwise false