mirror of
https://github.com/groue/GRDB.swift
synced 2026-11-28 04:24:20 +00:00
FTS4 and FTS5 options
This commit is contained in:
1 parent
ce0f62d598
commit
e300546a32
8 files changed
+119
-36
No files matched your search
@@ -31,11 +31,11 @@ public struct FTS3Tokenizer {
|
||||
}
|
||||
if let separators = separators {
|
||||
// TODO: test "=" and "\"", "(" and ")" as separators, with both FTS3Pattern(matchingAnyTokenIn:tokenizer:) and Database.create(virtualTable:using:)
|
||||
options.append("separators=" + separators.map { String($0) }.joined(separator: ""))
|
||||
options.append("separators=" + separators.sorted().map { String($0) }.joined(separator: ""))
|
||||
}
|
||||
if let tokenCharacters = tokenCharacters {
|
||||
// TODO: test "=" and "\"", "(" and ")" as tokenCharacters, with both FTS3Pattern(matchingAnyTokenIn:tokenizer:) and Database.create(virtualTable:using:)
|
||||
options.append("tokenchars=" + tokenCharacters.map { String($0) }.joined(separator: ""))
|
||||
options.append("tokenchars=" + tokenCharacters.sorted().map { String($0) }.joined(separator: ""))
|
||||
}
|
||||
return FTS3Tokenizer("unicode61", options: options)
|
||||
}
|
||||
|
||||
+39
-12
@@ -35,9 +35,13 @@ public struct FTS4 : VirtualTableModule {
|
||||
var arguments: [String] = []
|
||||
|
||||
for column in definition.columns {
|
||||
arguments.append(column.name)
|
||||
if !column.isIndexed {
|
||||
arguments.append("notindexed=\(column.name)")
|
||||
if column.isLanguageId {
|
||||
arguments.append("languageid=\"\(column.name)\"")
|
||||
} else {
|
||||
arguments.append(column.name)
|
||||
if !column.isIndexed {
|
||||
arguments.append("notindexed=\(column.name)")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -61,12 +65,12 @@ public struct FTS4 : VirtualTableModule {
|
||||
arguments.append("uncompress=\"\(uncompress)\"")
|
||||
}
|
||||
|
||||
if let languageid = definition.languageid {
|
||||
arguments.append("languageid=\"\(languageid)\"")
|
||||
if let matchinfo = definition.matchinfo {
|
||||
arguments.append("matchinfo=\"\(matchinfo)\"")
|
||||
}
|
||||
|
||||
if let prefix = definition.prefix {
|
||||
arguments.append("prefix=\"\(prefix)\"")
|
||||
if let prefixes = definition.prefixes {
|
||||
arguments.append("prefix=\"\(prefixes.sorted().map { "\($0)" }.joined(separator: ","))\"")
|
||||
}
|
||||
|
||||
return arguments
|
||||
@@ -110,15 +114,21 @@ public final class FTS4TableDefinition : VirtualTableDefinition {
|
||||
/// See https://www.sqlite.org/fts3.html#the_compress_and_uncompress_options
|
||||
public var uncompress: String?
|
||||
|
||||
/// The FTS4 `languageid` option
|
||||
/// The FTS4 `matchinfo` option
|
||||
///
|
||||
/// See https://www.sqlite.org/fts3.html#the_languageid_option
|
||||
public var languageid: String?
|
||||
/// See https://www.sqlite.org/fts3.html#the_matchinfo_option
|
||||
public var matchinfo: String?
|
||||
|
||||
/// The FTS4 `prefix` option
|
||||
/// Support for the FTS5 `prefix` option
|
||||
///
|
||||
/// // CREATE VIRTUAL TABLE documents USING FTS4(content, prefix='2 4');
|
||||
/// db.create(virtualTable: "documents", using:FTS4()) { t in
|
||||
/// t.prefixes = [2, 4]
|
||||
/// t.column("content")
|
||||
/// }
|
||||
///
|
||||
/// See https://www.sqlite.org/fts3.html#the_prefix_option
|
||||
public var prefix: String?
|
||||
public var prefixes: Set<Int>?
|
||||
|
||||
/// Appends a table column.
|
||||
///
|
||||
@@ -147,10 +157,12 @@ public final class FTS4TableDefinition : VirtualTableDefinition {
|
||||
public final class FTS4ColumnDefinition {
|
||||
fileprivate let name: String
|
||||
fileprivate var isIndexed: Bool
|
||||
fileprivate var isLanguageId: Bool
|
||||
|
||||
init(name: String) {
|
||||
self.name = name
|
||||
self.isIndexed = true
|
||||
self.isLanguageId = false
|
||||
}
|
||||
|
||||
/// Excludes the column from the full-text index.
|
||||
@@ -167,4 +179,19 @@ public final class FTS4ColumnDefinition {
|
||||
self.isIndexed = false
|
||||
return self
|
||||
}
|
||||
|
||||
/// Uses the column as the Int32 language id hidden column.
|
||||
///
|
||||
/// try db.create(virtualTable: "persons", using: FTS4()) { t in
|
||||
/// t.column("a")
|
||||
/// t.column("lid").asLanguageId()
|
||||
/// }
|
||||
///
|
||||
/// See https://www.sqlite.org/fts3.html#the_languageid_option
|
||||
///
|
||||
/// - returns: Self so that you can further refine the column definition.
|
||||
@discardableResult public func asLanguageId() -> Self {
|
||||
self.isLanguageId = true
|
||||
return self
|
||||
}
|
||||
}
|
||||
+23
-5
@@ -52,11 +52,19 @@
|
||||
}
|
||||
|
||||
if let content = definition.content {
|
||||
arguments.append("content=\"\(content)\"")
|
||||
arguments.append("content=\(content.sqlExpression.sql)")
|
||||
}
|
||||
|
||||
if let prefix = definition.prefix {
|
||||
arguments.append("prefix=\"\(prefix)\"")
|
||||
if let prefixes = definition.prefixes {
|
||||
arguments.append("prefix=\(prefixes.map { "\($0)" }.joined(separator: " ").sqlExpression.sql)")
|
||||
}
|
||||
|
||||
if let columnSize = definition.columnSize {
|
||||
arguments.append("columnSize=\(columnSize)")
|
||||
}
|
||||
|
||||
if let detail = definition.detail {
|
||||
arguments.append("detail=\(detail)")
|
||||
}
|
||||
|
||||
return arguments
|
||||
@@ -90,10 +98,20 @@
|
||||
/// See https://www.sqlite.org/fts5.html#external_content_and_contentless_tables
|
||||
public var content: String?
|
||||
|
||||
/// The FTS5 `prefix` option
|
||||
/// Support for the FTS5 `prefix` option
|
||||
///
|
||||
/// See https://www.sqlite.org/fts5.html#prefix_indexes
|
||||
public var prefix: String?
|
||||
public var prefixes: Set<Int>?
|
||||
|
||||
/// Support for the FTS5 `columnsize` option
|
||||
///
|
||||
/// https://www.sqlite.org/fts5.html#the_columnsize_option
|
||||
public var columnSize: Int?
|
||||
|
||||
/// Support for the FTS5 `detail` option
|
||||
///
|
||||
/// https://www.sqlite.org/fts5.html#the_detail_option
|
||||
public var detail: String?
|
||||
|
||||
/// TODO: columnsize, detail
|
||||
|
||||
|
||||
@@ -46,11 +46,11 @@ public struct FTS5Tokenizer {
|
||||
}
|
||||
if let separators = separators, !separators.isEmpty {
|
||||
// TODO: test "=" and "\"", "(" and ")" as separators, with both FTS3Pattern(matchingAnyTokenIn:tokenizer:) and Database.create(virtualTable:using:)
|
||||
options.append(contentsOf: ["separators", separators.map { String($0) }.joined(separator: "").sqlExpression.sql])
|
||||
options.append(contentsOf: ["separators", separators.sorted().map { String($0) }.joined(separator: "").sqlExpression.sql])
|
||||
}
|
||||
if let tokenCharacters = tokenCharacters, !tokenCharacters.isEmpty {
|
||||
// TODO: test "=" and "\"", "(" and ")" as tokenCharacters, with both FTS3Pattern(matchingAnyTokenIn:tokenizer:) and Database.create(virtualTable:using:)
|
||||
options.append(contentsOf: ["tokenchars", tokenCharacters.map { String($0) }.joined(separator: "").sqlExpression.sql])
|
||||
options.append(contentsOf: ["tokenchars", tokenCharacters.sorted().map { String($0) }.joined(separator: "").sqlExpression.sql])
|
||||
}
|
||||
return FTS5Tokenizer("unicode61", options: options)
|
||||
}
|
||||
|
||||
@@ -3057,15 +3057,15 @@ Generally speaking, FTS5 is better than FTS4 which improves on FTS3. But this do
|
||||
| **English Stemming and Ascii case insensitivity** | X | X | X |
|
||||
| **English Stemming and Unicode case insensitivity** | | | X |
|
||||
| **English Stemming and Latin diacritics insensitivity** | | | X |
|
||||
| **Spell checking** (have "alamaba" match "alabama") | | ¹ | ? |
|
||||
| **Synonyms** (have "1st" match "first") | | | |
|
||||
| **Spell checking** (have "alamaba" match "alabama") | | ¹ | ¹ |
|
||||
| **Synonyms** (have "1st" match "first") | ¹ | ¹ | ¹ |
|
||||
| **Pinyin and Romaji** (have "romaji" match "ローマ字") | | | |
|
||||
| **Stop words** (don't index, and don't match words like "and" and "the") | | | |
|
||||
| :bowtie: Other Features | | | |
|
||||
| **Ranking** (sort results by relevance) | ¹ | ¹ | X |
|
||||
| **Snippets** (display a few words around a match) | X | X | X |
|
||||
|
||||
¹ Requires extra setup (but at least SQLite documentation talks about it)
|
||||
¹ Requires extra setup, possibly involving using the C language (but at least SQLite documentation talks about it).
|
||||
|
||||
For a full feature list, read the SQLite documentation. Some missing features can be achieved with extra application code.
|
||||
|
||||
@@ -3155,14 +3155,24 @@ try db.create(virtualTable: "books", using: FTS4()) { t in
|
||||
FTS4 supports [options](https://www.sqlite.org/fts3.html#fts4_options):
|
||||
|
||||
```swift
|
||||
// CREATE VIRTUAL TABLE books USING fts4(
|
||||
// content,
|
||||
// uuid,
|
||||
// content="",
|
||||
// compress=zip,
|
||||
// uncompress=unzip,
|
||||
// prefix="2,4",
|
||||
// notindexed=uuid,
|
||||
// languageid=lid
|
||||
// )
|
||||
try db.create(virtualTable: "documents", using: FTS4()) { t in
|
||||
t.content = ""
|
||||
t.compress = "zip"
|
||||
t.uncompress = "unzip"
|
||||
t.languageid = "lid"
|
||||
t.prefix = "2,4"
|
||||
t.column("uuid").notIndexed()
|
||||
t.prefixes = [2, 4]
|
||||
t.column("content")
|
||||
t.column("uuid").notIndexed()
|
||||
t.column("lid").asLanguageId()
|
||||
}
|
||||
```
|
||||
|
||||
@@ -3346,11 +3356,21 @@ try db.create(virtualTable: "books", using: FTS5()) { t in
|
||||
FTS5 supports [options](https://www.sqlite.org/fts5.html#fts5_table_creation_and_initialization):
|
||||
|
||||
```swift
|
||||
// CREATE VIRTUAL TABLE books USING fts5(
|
||||
// content,
|
||||
// uuid UNINDEXED,
|
||||
// content='',
|
||||
// prefix='2 4',
|
||||
// columnsize=0,
|
||||
// detail=column
|
||||
// )
|
||||
try db.create(virtualTable: "documents", using: FTS5()) { t in
|
||||
t.content = ""
|
||||
t.prefix = "2,4"
|
||||
t.column("uuid").notIndexed()
|
||||
t.column("content")
|
||||
t.column("uuid").notIndexed()
|
||||
t.content = ""
|
||||
t.prefixes = [2, 4]
|
||||
t.columnSize = 0
|
||||
t.detail = "column"
|
||||
}
|
||||
```
|
||||
|
||||
|
||||
@@ -102,7 +102,7 @@ class FTS3TableBuilderTests: GRDBTestCase {
|
||||
try db.create(virtualTable: "documents", using: FTS3()) { t in
|
||||
t.tokenizer = .unicode61(tokenCharacters: Set(".-".characters))
|
||||
}
|
||||
XCTAssertTrue(sqlQueries.contains("CREATE VIRTUAL TABLE \"documents\" USING fts3(tokenize=unicode61 \"tokenchars=.-\")") || sqlQueries.contains("CREATE VIRTUAL TABLE \"documents\" USING fts3(tokenize=unicode61 \"tokenchars=-.\")"))
|
||||
XCTAssertTrue(sqlQueries.contains("CREATE VIRTUAL TABLE \"documents\" USING fts3(tokenize=unicode61 \"tokenchars=-.\")"))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -102,7 +102,7 @@ class FTS4TableBuilderTests: GRDBTestCase {
|
||||
try db.create(virtualTable: "documents", using: FTS4()) { t in
|
||||
t.tokenizer = .unicode61(tokenCharacters: Set(".-".characters))
|
||||
}
|
||||
XCTAssertTrue(sqlQueries.contains("CREATE VIRTUAL TABLE \"documents\" USING fts4(tokenize=unicode61 \"tokenchars=.-\")") || sqlQueries.contains("CREATE VIRTUAL TABLE \"documents\" USING fts4(tokenize=unicode61 \"tokenchars=-.\")"))
|
||||
XCTAssertTrue(sqlQueries.contains("CREATE VIRTUAL TABLE \"documents\" USING fts4(tokenize=unicode61 \"tokenchars=-.\")"))
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -162,11 +162,13 @@ class FTS4TableBuilderTests: GRDBTestCase {
|
||||
t.content = ""
|
||||
t.compress = "zip"
|
||||
t.uncompress = "unzip"
|
||||
t.languageid = "lid"
|
||||
t.prefix = "2,4"
|
||||
t.matchinfo = "fts3"
|
||||
t.prefixes = [2, 4]
|
||||
t.column("content")
|
||||
t.column("lid").asLanguageId()
|
||||
}
|
||||
XCTAssertTrue(sqlQueries.contains("CREATE VIRTUAL TABLE \"documents\" USING fts4(content, content=\"\", compress=\"zip\", uncompress=\"unzip\", languageid=\"lid\", prefix=\"2,4\")"))
|
||||
print(sqlQueries)
|
||||
XCTAssertTrue(sqlQueries.contains("CREATE VIRTUAL TABLE \"documents\" USING fts4(content, languageid=\"lid\", content=\"\", compress=\"zip\", uncompress=\"unzip\", matchinfo=\"fts3\", prefix=\"2,4\")"))
|
||||
|
||||
try db.execute("INSERT INTO documents (docid, content, lid) VALUES (?, ?, ?)", arguments: [1, "abc", 0])
|
||||
XCTAssertEqual(Int.fetchOne(db, "SELECT COUNT(*) FROM documents WHERE documents MATCH ? AND lid=0", arguments: ["abc"])!, 1)
|
||||
|
||||
@@ -138,7 +138,7 @@ class FTS5TableBuilderTests: GRDBTestCase {
|
||||
t.tokenizer = .unicode61(tokenCharacters: Set(".-".characters))
|
||||
t.column("content")
|
||||
}
|
||||
XCTAssertTrue(sqlQueries.contains("CREATE VIRTUAL TABLE \"documents\" USING fts5(content, tokenize='unicode61 tokenchars ''.-''')") || sqlQueries.contains("CREATE VIRTUAL TABLE \"documents\" USING fts5(content, tokenize='unicode61 tokenchars ''-.''')"))
|
||||
XCTAssertTrue(sqlQueries.contains("CREATE VIRTUAL TABLE \"documents\" USING fts5(content, tokenize='unicode61 tokenchars ''-.''')"))
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -183,4 +183,20 @@ class FTS5TableBuilderTests: GRDBTestCase {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func testFTS5Options() {
|
||||
assertNoError {
|
||||
let dbQueue = try makeDatabaseQueue()
|
||||
try dbQueue.inDatabase { db in
|
||||
try db.create(virtualTable: "documents", using: FTS5()) { t in
|
||||
t.content = ""
|
||||
t.prefixes = [2, 4]
|
||||
t.columnSize = 0
|
||||
t.detail = "column"
|
||||
t.column("content")
|
||||
}
|
||||
XCTAssertTrue(sqlQueries.contains("CREATE VIRTUAL TABLE \"documents\" USING fts5(content, content='', prefix='2 4', columnSize=0, detail=column)"))
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user