Skip to content

#6027 - Exhaustive conversation search #6078

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions Signal.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -1234,6 +1234,7 @@
66FC638C29E9E9D200F00DAC /* TextCheckingDataItem.swift in Sources */ = {isa = PBXBuildFile; fileRef = 66FC638B29E9E9D200F00DAC /* TextCheckingDataItem.swift */; };
66FC638E29EDABAC00F00DAC /* SearchDisplayConfigurations.swift in Sources */ = {isa = PBXBuildFile; fileRef = 66FC638D29EDABAC00F00DAC /* SearchDisplayConfigurations.swift */; };
66FFDADC2C823C270079C0E7 /* BackupArchive+Contexts.swift in Sources */ = {isa = PBXBuildFile; fileRef = 66FFDADB2C823C270079C0E7 /* BackupArchive+Contexts.swift */; };
6FE6A7232E1472540004DBC0 /* FullTextSearchIndexerTest.swift in Sources */ = {isa = PBXBuildFile; fileRef = 6FE6A7222E1472540004DBC0 /* FullTextSearchIndexerTest.swift */; };
7203F5332D53B53000639949 /* UInt64+SSK.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7203F5322D53B52900639949 /* UInt64+SSK.swift */; };
7203F5352D53B83500639949 /* TimeInterval+SSK.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7203F5342D53B83000639949 /* TimeInterval+SSK.swift */; };
720547F22B9C8F9900E2CF2F /* AvatarModel.swift in Sources */ = {isa = PBXBuildFile; fileRef = 883A7FD1269F642F00841DF9 /* AvatarModel.swift */; };
Expand Down Expand Up @@ -5182,6 +5183,7 @@
67391FF368D9A60FC8B73F0E /* Pods-Signal.profiling.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = "Pods-Signal.profiling.xcconfig"; path = "Target Support Files/Pods-Signal/Pods-Signal.profiling.xcconfig"; sourceTree = "<group>"; };
675486AB8F0612FF2C717BAE /* Pods_SignalUI.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; includeInIndex = 0; path = Pods_SignalUI.framework; sourceTree = BUILT_PRODUCTS_DIR; };
6BB92957776B3173894CD3E9 /* Pods-SignalServiceKit.app store release.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = "Pods-SignalServiceKit.app store release.xcconfig"; path = "Target Support Files/Pods-SignalServiceKit/Pods-SignalServiceKit.app store release.xcconfig"; sourceTree = "<group>"; };
6FE6A7222E1472540004DBC0 /* FullTextSearchIndexerTest.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = FullTextSearchIndexerTest.swift; sourceTree = "<group>"; };
70377AAA1918450100CAF501 /* MobileCoreServices.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = MobileCoreServices.framework; path = System/Library/Frameworks/MobileCoreServices.framework; sourceTree = SDKROOT; };
7203F5322D53B52900639949 /* UInt64+SSK.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "UInt64+SSK.swift"; sourceTree = "<group>"; };
7203F5342D53B83000639949 /* TimeInterval+SSK.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "TimeInterval+SSK.swift"; sourceTree = "<group>"; };
Expand Down Expand Up @@ -13192,9 +13194,8 @@
F94261D7289B1B5400460798 /* Storage */ = {
isa = PBXGroup;
children = (
F97217F928DCA35F00113D9F /* Database */,
D9B95A9329E682CA00D7CB95 /* JobRecords */,
66485EB82CD17D5D00B8613F /* DbRollbackTests.swift */,
6FE6A7222E1472540004DBC0 /* FullTextSearchIndexerTest.swift */,
F94261DE289B1B5400460798 /* InteractionFinderTest.swift */,
C167F1E42A7162D700D4A9AF /* KyberPreKeyStoreImplTest.swift */,
F94261DD289B1B5400460798 /* ModelReadCacheTest.swift */,
Expand All @@ -13205,6 +13206,8 @@
F94261DB289B1B5400460798 /* SDSKeyValueStoreTest.swift */,
F94261E1289B1B5400460798 /* SignedPreKeyStoreImplTest.swift */,
50C38CAC2A8EB2610030A731 /* TimeGatedBatchTest.swift */,
F97217F928DCA35F00113D9F /* Database */,
D9B95A9329E682CA00D7CB95 /* JobRecords */,
);
name = Storage;
path = SignalServiceKit/tests/Storage;
Expand Down Expand Up @@ -18936,6 +18939,7 @@
F9426259289B1B5500460798 /* RemoteConfigManagerTests.swift in Sources */,
F945FE4D298481EA00C835C7 /* RingrtcFieldTrialsTest.swift in Sources */,
F942624E289B1B5500460798 /* SDSDatabaseStorageObservationTest.swift in Sources */,
6FE6A7232E1472540004DBC0 /* FullTextSearchIndexerTest.swift in Sources */,
F942624B289B1B5500460798 /* SDSDatabaseStorageTest.swift in Sources */,
F942624A289B1B5500460798 /* SDSKeyValueStoreTest.swift in Sources */,
662C44172A1D21D7001F83E2 /* SecureValueRecovery2Tests.swift in Sources */,
Expand Down
67 changes: 54 additions & 13 deletions SignalServiceKit/Storage/FullTextSearchIndexer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,7 @@ extension FullTextSearchIndexer {
public static let ftsTableName = "indexable_text_fts"

static let contentTableName = "indexable_text"
static let interactionTableName = "model_TSInteraction"
static let uniqueIdColumn = "uniqueId"
static let collectionColumn = "collection"
static let ftsContentColumn = "ftsIndexableContent"
Expand Down Expand Up @@ -219,6 +220,22 @@ extension FullTextSearchIndexer {
maxResults: Int,
tx: DBReadTransaction,
block: (_ message: TSMessage, _ snippet: String, _ stop: inout Bool) -> Void
) {
search(
for: searchText,
maxResults: maxResults,
threadUniqueId: nil,
tx: tx,
block: block
)
}

public static func search(
for searchText: String,
maxResults: Int,
threadUniqueId: String?,
tx: DBReadTransaction,
block: (_ message: TSMessage, _ snippet: String, _ stop: inout Bool) -> Void
) {
let query = buildQuery(for: searchText)

Expand All @@ -236,19 +253,43 @@ extension FullTextSearchIndexer {
// Determines the length of the snippet.
let numTokens: UInt = 15
let matchSnippet = "match_snippet"
let sql: String = """
SELECT
\(contentTableName).\(collectionColumn),
\(contentTableName).\(uniqueIdColumn),
SNIPPET(\(ftsTableName), \(indexOfContentColumnInFTSTable), '<\(matchTag)>', '</\(matchTag)>', '…', \(numTokens)) AS \(matchSnippet)
FROM \(ftsTableName)
LEFT JOIN \(contentTableName) ON \(contentTableName).rowId = \(ftsTableName).rowId
WHERE \(ftsTableName).\(ftsContentColumn) MATCH ?
ORDER BY rank
LIMIT \(maxResults)
"""

let cursor = try Row.fetchCursor(tx.database, sql: sql, arguments: [query])

let sql: String
let arguments: StatementArguments

if let threadUniqueId = threadUniqueId {
// When searching within a specific conversation, join with the interaction table to filter by thread
sql = """
SELECT
\(contentTableName).\(collectionColumn),
\(contentTableName).\(uniqueIdColumn),
SNIPPET(\(ftsTableName), \(indexOfContentColumnInFTSTable), '<\(matchTag)>', '</\(matchTag)>', '…', \(numTokens)) AS \(matchSnippet)
FROM \(ftsTableName)
LEFT JOIN \(contentTableName) ON \(contentTableName).rowId = \(ftsTableName).rowId
LEFT JOIN \(interactionTableName) ON \(interactionTableName).uniqueId = \(contentTableName).\(uniqueIdColumn)
WHERE \(ftsTableName).\(ftsContentColumn) MATCH ?
AND \(interactionTableName).uniqueThreadId = ?
ORDER BY rank
LIMIT \(maxResults)
"""
arguments = [query, threadUniqueId]
} else {
// Global search - use the original query
sql = """
SELECT
\(contentTableName).\(collectionColumn),
\(contentTableName).\(uniqueIdColumn),
SNIPPET(\(ftsTableName), \(indexOfContentColumnInFTSTable), '<\(matchTag)>', '</\(matchTag)>', '…', \(numTokens)) AS \(matchSnippet)
FROM \(ftsTableName)
LEFT JOIN \(contentTableName) ON \(contentTableName).rowId = \(ftsTableName).rowId
WHERE \(ftsTableName).\(ftsContentColumn) MATCH ?
ORDER BY rank
LIMIT \(maxResults)
"""
arguments = [query]
}

let cursor = try Row.fetchCursor(tx.database, sql: sql, arguments: arguments)
while let row = try cursor.next() {
let collection: String = row[collectionColumn]
guard collection == legacyCollectionName else {
Expand Down
Loading