SearchQueryConverter class base
A parser for a user-friendly dialect of SQLite FTS5 expressions.
The goal of this parser is to translate a user-typed full-text search query into an SQLite FTS5 query. The grammar accepted by the parser has several differences from SQLite's, as well as other points of attention:
- barewords are sequences of Unicode BMP letters and numbers including
underscores and inner non-consecutive hyphens (e.g.,
well-known); - barewords must not match reserved keywords, which are the uppercase
AND,OR,NOT, andNEAR(however, e.g.,AND-ORis a valid bareword); - initial tokens are not supported;
- explicit bareword prefix tokens are not supported;
- each bareword term is an implicit prefix token and, when translated,
invariably becomes a quoted prefix token where underscores and hyphens are
replaced with spaces (e.g.,
well-knownbecomes"well known"*); - the
NOToperator has the lowest precedence (e.g.,wheat OR maize NOT weedis translated to("wheat"* OR "maize"*) NOT "weed"*); - the explicit
ANDoperator can be replaced with the implicit one in all cases without changing the precedence; - column names are defined as barewords;
- only a single column in a given column expression is supported;
- there must be no intermediate spacing characters when a quoted string is
appended with
*, when a column is prepended with-, and when a column is appended with:(e.g.,-musketeers: "d'art"*is correct).
Queries generated by this parser search only within FTS columns listed in the columns property. This is achieved by automatically enclosing the resulting query within a column expression with columns from that property.
Column expressions accepted by the parser, instead of real column names, must use column aliases defined as keys in the aliases property. During translation, a column alias in a column expression is substituted with a list of real FTS columns associated in the aliases map with the alias.
In the following example, SearchQueryConverter is used to translate a
query, namely shakespear NOT authors:shakespear, that requires the
presence of the shakespear bareword prefix token in any of the
ftsColumns and the absence of the same token in the authors column
alias, which is resolved to authors and pseudonyms:
var ftsColumns = ["titles", "authors", "pseudonyms", "description"];
var ftsConverter = SearchQueryConverter(
columns: ftsColumns,
aliases: {
"title": ["titles"],
"authors": ["authors", "pseudonyms"],
"about": ["description"],
},
);
String fts = "shakespear NOT authors:shakespear";
try {
FtsQ ftsQuery = ftsConverter.run(fts);
log.info("SUCCESS:\n`${ftsQuery.str}`");
} on ParserException catch (exception) {
log.info("FAILURE:", exception);
}
/* SUCCESS:
`({"titles" "authors" "pseudonyms" "description"}:
("shakespear"* NOT ({"authors" "pseudonyms"}: "shakespear"*)))` */
See also: SearchQueryConverter.new and run.
Constructors
-
SearchQueryConverter({required List<
String> columns, required Map<String, List< aliases, MessageResolver messageResolver = defaultMessageResolver})String> > - Creates a new SearchQueryConverter with the given arguments.
Properties
-
aliases
→ Map<
String, List< String> > -
A Map that defines custom column names recognized by this parser.
final
-
columns
→ List<
String> -
A list of the actual names of the FTS table columns.
final
- converter → FtsParser
-
The prebuilt default composite parser starting at start.
latefinal
- hashCode → int
-
The hash code for this object.
no setterinherited
- messageResolver → MessageResolver
-
The callback to obtain an error message for a parser error code.
final
- runtimeType → Type
-
A representation of the runtime type of the object.
no setterinherited
Methods
-
build(
) → FtsParser - Builds the default composite parser starting at start.
-
buildFrom<
T> (Parser< T> parser) → Parser<T> -
Builds a composite parser starting with the specified
parser.inherited -
noSuchMethod(
Invocation invocation) → dynamic -
Invoked when a nonexistent method or property is accessed.
inherited
-
run(
String input) → FtsQ -
Converts an
inputsearch query into a native SQLite FTS5 expression. -
start(
) → FtsParser - Returns the starting production of this definition.
-
toString(
) → String -
A string representation of this object.
inherited
Operators
-
operator ==(
Object other) → bool -
The equality operator.
inherited