-
Notifications
You must be signed in to change notification settings - Fork 40
Fuzzy Search
When you're displaying contacts, you often want to allow users to search through the list of contacts. The fuzzy matching utility makes it easy to build a fuzzy search feature into your contact selection screen.
OHFuzzyMatchingUtility *fuzzyMatchingUtility = [[OHFuzzyMatchingUtility alloc] init];
First, you need to set the allContacts
property to the set of contacts you want to search through.
[dataSource.onContactsDataSourceReadySignal addObserver:self callback:^(typeof(self) self) {
fuzzyMatchingUtility.allContacts = self.dataSource.contacts;
}];
Now we can get the results of a search query by calling the contactsMatchingQuery:
method. This will return all contacts who has a match to the query string either in full name or one of its contact fields.
NSOrderedSet<OHContact *> *results = [fuzzyMatchingUtility contactsMatchingQuery:query];
The fuzzy matching utility can optionally take a scoring block to perform scoring analysis on the query string. The scoring block takes as input a query string and a nominee string, and returns an NSInteger score of how "good" the match is.
For example, we could write a very simple scoring function that checks if the string is an exact match, and if so awards it a higher score.
fuzzyMatchingUtility.scoringBlock = ^NSInteger (NSString *query, NSString *nominee) {
if ([query isEqualToString:nominee]) {
return 5;
}
return 1;
};
If there is a scoring block, results will be returned in order of score (highest scores first). If the scoring block is nil, results will be returned in order of the contacts in allContacts
.