Class QueryConstraint
java.lang.Object
io.fluxzero.common.api.search.constraints.PathConstraint
io.fluxzero.common.api.search.constraints.QueryConstraint
- All Implemented Interfaces:
Constraint
A
Constraint that parses a human-friendly query string into a structured tree of constraints.
This class allows users to write intuitive search strings like:
"error & (critical | urgent) & !resolved" ""exact phrase" | partial* & !excluded"
The query string supports:
- Terms: space-separated words, optionally with wildcards (*).
- Exact phrases: quoted strings (e.g.,
"full text"). - Operators:
&(AND),|(OR),!(NOT), and parentheses for grouping. - Lookahead behavior: Controlled via
lookAheadForAllTermsto mimic user-friendly search behavior.
Wildcards:
- A trailing
*enables prefix-matching (e.g.,hat*matcheshatter). - A leading
*enables postfix-matching (e.g.,*hatmatcheschat). - Both allows infix-matching (e.g.,
*hat*matchesgroupchatting).
Usage Examples
// Match documents containing both "order" and "cancel"
QueryConstraint.query("order cancel");
// Match documents with "error" or "failure"
QueryConstraint.query("error | failure");
// Match documents where "payment" appears but not "test"
QueryConstraint.query("payment !test");
// Match documents containing a phrase
QueryConstraint.query("\"order received\"");
If a lookAheadForAllTerms flag is set to true, then every term is treated as a look-ahead match
(e.g., "chat" will match "chatbot", "chatter", etc.). The query is applied over the configured document
paths, or across all paths if none are set.
Internally, the query is decomposed into a structure of ContainsConstraint, NotConstraint,
AllConstraint, and AnyConstraint nodes that evaluate against document fields.
Performance Considerations
-
Queries using a prefix or postfix wildcard (e.g.,
user*or*name) are resolved at the data store level and typically fast. -
Queries 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, butto*or*toare not. - When possible, prefer longer terms and structure queries to enable efficient execution via the underlying document store.
- See Also:
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionbooleanOverridesConstraint.matches(Document)to evaluate a decomposed constraint tree.protected booleanmatches(Document.Entry entry, Document document) Not supported directly on a single document entry.static ConstraintCreates aQueryConstraintwith optional lookahead behavior for terms.static ConstraintCreates aQueryConstraintthat parses and evaluates the given query string.Methods inherited from class PathConstraint
checkPathBeforeEntry, getPaths, hasPathConstraint, withPathsMethods inherited from class Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, waitMethods inherited from interface Constraint
and, decompose, or
-
Constructor Details
-
QueryConstraint
public QueryConstraint()
-
-
Method Details
-
query
Creates aQueryConstraintthat parses and evaluates the given query string.- Parameters:
query- the human-readable query stringpaths- one or more document paths to apply the query to- Returns:
- a parsed and decomposed constraint
-
query
Creates aQueryConstraintwith optional lookahead behavior for terms.- Parameters:
query- the query stringlookAheadForAllTerms- if true, all terms will behave like lookaheads (i.e., include trailing wildcard logic)paths- the paths to apply the query to
-
matches
OverridesConstraint.matches(Document)to evaluate a decomposed constraint tree. The parsing is done lazily and cached for reuse.- Specified by:
matchesin interfaceConstraint- Overrides:
matchesin classPathConstraint- Parameters:
document- the document to test- Returns:
trueif the constraint matches the document
-
matches
Not supported directly on a single document entry. ThrowsUnsupportedOperationException.- Specified by:
matchesin classPathConstraint- Parameters:
entry- the document entry to evaluate- Returns:
trueif the entry satisfies the condition;falseotherwise
-