SearchIndex constructor

SearchIndex({
  1. required int version,
  2. required String name,
  3. required SearchColumns columns,
  4. required SearchRowGetters getters,
  5. bool removeLatinDiacritics = true,
  6. String tokenCategories = ftsTokenCategories,
  7. String preprocessor = ftsPreprocessor,
})

Creates a description of an FTS index using the given parameters.

Every key in a SearchRow map returned by the getters must match the name of one of the columns, and the size of the SearchRow map must match the number of columns. It is expected that every Kind used as a key in the getters map is a String or a finite num.

The name argument must not match the reserved names tableMapping, tableScheme, and columnKind, nor must it, within the same database, match the name of any partial index, search index, or FTS5 shadow table.

The removeLatinDiacritics and tokenCategories parameters are used by the constructor to compose the tokenizer string. This string represents a "unicode61" tokenizer configuration for this FTS index and contains the following two options:

  • the remove_diacritics option, set to 2 if removeLatinDiacritics is true, or 0 otherwise;
  • the categories option, set to tokenCategories.

By default, removeLatinDiacritics is true, tokenCategories is ftsTokenCategories, and preprocessor is ftsPreprocessor.

The ftsTerm and ftsText functions may not be compatible with a non-default tokenCategories value; custom preprocessing may be required in this case. When removeLatinDiacritics is true, any text in this FTS index is searched insensitively to Latin diacritics, including text preprocessed by ftsTerm and ftsText.

See also: SchemeElement.new.

Implementation

SearchIndex({
  required super.version,
  required super.name,
  required this.columns,
  required this.getters,
  bool removeLatinDiacritics = true,
  String tokenCategories = ftsTokenCategories,
  this.preprocessor = ftsPreprocessor,
}) : tokenizer = (() {
       var rd = removeLatinDiacritics ? 2 : 0;
       var tc = sqlLiteral(tokenCategories);
       return "unicode61 remove_diacritics $rd categories $tc";
     })() {
  checkState(
    columns.isNotEmpty,
    message: () => "SearchIndex.new[name=$name] requires at least one column",
  );
  checkState(
    !lcName.startsWith("sqlite_"),
    message: () =>
        "SearchIndex.new[name=$name]: "
        "name starts with the reserved prefix `sqlite_`",
  );
  checkState(
    columns.none((var e) => e.lcName == lcName),
    message: () =>
        "SearchIndex.new[name=$name]: search index name must "
        "not coincide with the name of any of its columns",
  );
  checkState(
    getters.keys.every(isSqlValue),
    message: () =>
        "SearchIndex[name=$name]: keys of the getters map "
        "must be strings or finite nums",
  );
  if (columns.map((var e) => e.lcName) case var c
      when HashSet<String>.from(c).length != c.length)
    throw StateError("SearchIndex.new: coinciding column names ($c)");
}