Skip to content

Commit 09dbd66

Browse files
authored
Merge pull request #27 from PSPDFKit/christoph/improve-fts-example
Improves FTS example with search options and previewTextHandler
2 parents dbd7582 + f9e930a commit 09dbd66

File tree

1 file changed

+23
-9
lines changed

1 file changed

+23
-9
lines changed

Examples/PSPDFCatalog/PSPDFCatalog/Catalog/Search/IndexedFullTextSearch.cs

+23-9
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,10 @@ public override void ViewDidAppear(bool animated)
5353
public void libraryDidFinishIndexing()
5454
{
5555
var searchstring = "";
56-
NSDictionary dic = null;
56+
57+
// You can set some options for the search
58+
var options = new NSMutableDictionary<NSObject, NSString>();
59+
options[PSPDFLibrarySearchResultsOptionsKeys.MatchExactPhrasesOnlyKey] = new NSString("YES");
5760

5861
var alert = UIAlertController.Create("", "Start Full-Text Search with String:", UIAlertControllerStyle.Alert);
5962

@@ -68,24 +71,35 @@ public void libraryDidFinishIndexing()
6871
// Create a new alert controller for our results (cheap way to display them on the view)
6972
var resultsAlert = UIAlertController.Create("", "", UIAlertControllerStyle.Alert);
7073

71-
library.FindDocumentUids(searchstring, dic, (string searchString, NSDictionary<NSString, NSIndexSet> resultSet) =>
74+
library.FindDocumentUids(searchstring, options, (string searchString, NSDictionary<NSString, NSIndexSet> resultSet) =>
7275
{
73-
string results = "";
7476
foreach ((var UID, var indexSet) in resultSet)
7577
{
76-
// Get all the results into a single string so we can display them all at once
77-
results += string.Format("Found the following matches for \"{0}\" in document {1}: {2}\n\n", searchstring, UID, indexSet);
78+
Console.WriteLine("Found the following matches for \"{0}\" in document {1}: {2}\n\n", searchstring, UID, indexSet);
7879
}
79-
// Need to present the alert controller on the UI thread otherwise it crashes
80+
}, (string searchString, NSDictionary < NSString, NSSet < PSPDFLibraryPreviewResult >> resultSet) =>
81+
{
82+
// Use the `previewTextHandler` for presenting preview text of the search results
8083
DispatchQueue.MainQueue.DispatchAsync(() =>
8184
{
82-
resultsAlert.Message = results;
85+
var previewTexts = "";
86+
foreach (var UID in resultSet.Keys)
87+
{
88+
// Get the results for each document
89+
var results = resultSet[UID];
90+
91+
foreach (PSPDFLibraryPreviewResult result in results)
92+
{
93+
// Get all the important information from each result
94+
previewTexts += string.Format("Document: \"{0}\"\nPage: {1}\nPreview Text: \"{2}\" \n\n",result.DocumentUid, (result.PageIndex + 1), result.PreviewText);
95+
}
96+
}
97+
98+
resultsAlert.Message = previewTexts;
8399
resultsAlert.AddAction(cancelAction);
84100
PresentViewController(resultsAlert, false, null);
85101
});
86-
87102
});
88-
89103
});
90104

91105
alert.AddAction(cancelAction);

0 commit comments

Comments
 (0)