Skip to content

Commit

Permalink
Merge branch 'release/1.0.10'
Browse files Browse the repository at this point in the history
  • Loading branch information
msqr committed Oct 12, 2015
2 parents e0e4539 + 41d082e commit 6efe8f4
Show file tree
Hide file tree
Showing 5 changed files with 95 additions and 28 deletions.
2 changes: 1 addition & 1 deletion BRFullTextSearch.podspec
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
Pod::Spec.new do |s|

s.name = "BRFullTextSearch"
s.version = "1.0.9"
s.version = "1.0.10"
s.summary = "iOS Objective-C full text search engine."
s.description = <<-DESC
This project provides a way to integrate full-text search
Expand Down
21 changes: 21 additions & 0 deletions BRFullTextSearch/BRSearchService.h
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,27 @@ typedef NS_ENUM (unsigned int, BRSearchSortType) {
/** A `NSError` domain for search services to use. */
extern NSString *const BRSearchServiceErrorDomain;

/**
A general exception name. The exact meaning is implementation specific.
@since 1.0.10
*/
extern NSString * const BRSearchServiceException;

/**
An exception name for when too many results of some kind are encountered. The exact meaning is implementation specific.
@since 1.0.10
*/
extern NSString * const BRSearchServiceTooManyResultsException;

/**
An exception name for when a query cannot be parsed properly.
@since 1.0.10
*/
extern NSString * const BRSearchServiceQueryParsingException;

/**
* A simple completion block callback.
*
Expand Down
6 changes: 6 additions & 0 deletions BRFullTextSearch/BRSearchService.m
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,9 @@
#import "BRSearchService.h"

NSString * const BRSearchServiceErrorDomain = @"BRFTSSearchService";

NSString * const BRSearchServiceException = @"BRFTSException";

NSString * const BRSearchServiceTooManyResultsException = @"BRFTSTooManyResults";

NSString * const BRSearchServiceQueryParsingException = @"BRFTSErrorParsingQuery";
4 changes: 4 additions & 0 deletions BRFullTextSearch/CLuceneSearchService.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,10 @@
* Queries will not see the result of index updates until after the update is committed. For batch
* updates that means at the completion of the batch operation.
*
* # Exceptions
*
* This service will translate @c CLuceneError C++ exceptions into Objective-C @c NSException instances.
*
* # Query syntax
*
* When searching with string queries, the
Expand Down
90 changes: 63 additions & 27 deletions BRFullTextSearch/CLuceneSearchService.mm
Original file line number Diff line number Diff line change
Expand Up @@ -358,9 +358,29 @@ - (void)flushBufferedDocuments:(CLuceneIndexUpdateContext *)updateContext {
}];
}

- (NSException *)exceptionForLuceneError:(CLuceneError)err userInfo:(NSDictionary *)dict {
NSException *result;
NSString *str = [NSString stringWithCLuceneString:err.twhat()];
NSMutableDictionary *userInfo = [[NSMutableDictionary alloc] initWithCapacity:4];
[userInfo addEntriesFromDictionary:dict];
userInfo[@"CLuceneErrorNumber"] = @(err.number());
if ( err.number() == CL_ERR_TooManyClauses ) {
result = [NSException exceptionWithName:BRSearchServiceTooManyResultsException reason:([str length] > 0 ? str : @"Too many query clauses.") userInfo:userInfo];
} else if ( err.number() == CL_ERR_Parse || err.number()==CL_ERR_TokenMgr ) {
result = [NSException exceptionWithName:BRSearchServiceQueryParsingException reason:([str length] > 0 ? str : @"Error parsing query.") userInfo:userInfo];
} else {
result = [NSException exceptionWithName:BRSearchServiceException reason:([str length] > 0 ? str : @"Unknown CLucene error.") userInfo:userInfo];
}
return result;
}

- (int)removeObjectsFromIndexMatchingPredicate:(NSPredicate *)predicate context:(id<BRIndexUpdateContext>)updateContext {
std::auto_ptr<Query> query = [self queryForPredicate:predicate analyzer:nil parent:nil];
return [self removeObjectsFromIndexWithQuery:query context:(CLuceneIndexUpdateContext *)updateContext];
try {
std::auto_ptr<Query> query = [self queryForPredicate:predicate analyzer:nil parent:nil];
return [self removeObjectsFromIndexWithQuery:query context:(CLuceneIndexUpdateContext *)updateContext];
} catch ( const CLuceneError &err ) {
@throw [self exceptionForLuceneError:err userInfo:@{@"query" : predicate}];
}
}

- (int)removeObjectsFromIndexWithQuery:(std::auto_ptr<Query>)query context:(id<BRIndexUpdateContext>)updateContext {
Expand Down Expand Up @@ -564,8 +584,12 @@ - (void)removeObjectsFromIndex:(BRSearchObjectType)type withIdentifiers:(NSSet *
}

// search for all matching objects to delete
std::auto_ptr<Query> query = [self queryForObjects:type withIdentifiers:identifiers];
[self removeObjectsFromIndexWithQuery:query queue:queue finished:finished];
try {
std::auto_ptr<Query> query = [self queryForObjects:type withIdentifiers:identifiers];
[self removeObjectsFromIndexWithQuery:query queue:queue finished:finished];
} catch ( const CLuceneError &err ) {
@throw [self exceptionForLuceneError:err userInfo:@{@"delete" : identifiers, @"type" : @(type)}];
}
}

- (int)removeObjectsFromIndexAndWait:(BRSearchObjectType)type withIdentifiers:(NSSet *)identifiers error:(NSError *__autoreleasing *)error {
Expand Down Expand Up @@ -598,8 +622,12 @@ - (int)removeObjectsFromIndexAndWait:(BRSearchObjectType)type withIdentifiers:(N
- (void)removeObjectsFromIndexMatchingPredicate:(NSPredicate *)predicate
queue:(dispatch_queue_t)finishedQueue
finished:(BRSearchServiceUpdateCallbackBlock)finished {
std::auto_ptr<Query> query = [self queryForPredicate:predicate analyzer:nil parent:nil];
[self removeObjectsFromIndexWithQuery:query queue:finishedQueue finished:finished];
try {
std::auto_ptr<Query> query = [self queryForPredicate:predicate analyzer:nil parent:nil];
[self removeObjectsFromIndexWithQuery:query queue:finishedQueue finished:finished];
} catch ( const CLuceneError &err ) {
@throw [self exceptionForLuceneError:err userInfo:@{@"query" : predicate}];
}
}

- (int)removeObjectsFromIndexMatchingPredicateAndWait:(NSPredicate *)predicate error:(NSError *__autoreleasing *)error {
Expand Down Expand Up @@ -632,34 +660,38 @@ - (int)removeObjectsFromIndexMatchingPredicateAndWait:(NSPredicate *)predicate e
if ( [query length] < 1 ) {
return nil;
}
std::auto_ptr<BooleanQuery> rootQuery(new BooleanQuery(false));
QueryParser parser([kBRSearchFieldNameValue asCLuceneString], [self defaultAnalyzer]);
for ( NSString *fieldName in generalTextFields ) {
try {
try {
std::auto_ptr<BooleanQuery> rootQuery(new BooleanQuery(false));
QueryParser parser([kBRSearchFieldNameValue asCLuceneString], [self defaultAnalyzer]);
for ( NSString *fieldName in generalTextFields ) {
Query *q = parser.parse([query asCLuceneString], [fieldName asCLuceneString], [self defaultAnalyzer]);
rootQuery.get()->add(q, true, BooleanClause::SHOULD);
} catch ( CLuceneError &ex ) {
NSLog(@"Error %d parsing query [%@]: %@", ex.number(), query, [NSString stringWithCLuceneString:ex.twhat()]);
}
std::auto_ptr<Hits> hits([self searcher]->search(rootQuery.get()));
std::auto_ptr<Sort> sort;
std::auto_ptr<Query> resultQuery(rootQuery);
return [[CLuceneSearchResults alloc] initWithHits:hits sort:sort query:resultQuery searcher:[self searcher]];
} catch ( const CLuceneError &err ) {
@throw [self exceptionForLuceneError:err userInfo:@{@"query" : query}];
}
std::auto_ptr<Hits> hits([self searcher]->search(rootQuery.get()));
std::auto_ptr<Sort> sort;
std::auto_ptr<Query> resultQuery(rootQuery);
return [[CLuceneSearchResults alloc] initWithHits:hits sort:sort query:resultQuery searcher:[self searcher]];
}

- (id<BRSearchResult>)findObject:(BRSearchObjectType)type withIdentifier:(NSString *)identifier {
NSString *idValue = [self idValueForType:type identifier:identifier];
Term *idTerm = new Term([kBRSearchFieldNameIdentifier asCLuceneString], [idValue asCLuceneString]);
std::auto_ptr<TermQuery> idQuery(new TermQuery(idTerm));
std::auto_ptr<Hits> hits([self searcher]->search(idQuery.get()));
CLuceneSearchResult *result = nil;
if ( hits->length() > 0 ) {
// return first match, taking owning the Hits pointer
result = [[[CLuceneSearchResult searchResultClassForDocument:hits->doc(0)] alloc] initWithOwnedHits:hits index:0];
try {
Term *idTerm = new Term([kBRSearchFieldNameIdentifier asCLuceneString], [idValue asCLuceneString]);
std::auto_ptr<TermQuery> idQuery(new TermQuery(idTerm));
std::auto_ptr<Hits> hits([self searcher]->search(idQuery.get()));
CLuceneSearchResult *result = nil;
if ( hits->length() > 0 ) {
// return first match, taking owning the Hits pointer
result = [[[CLuceneSearchResult searchResultClassForDocument:hits->doc(0)] alloc] initWithOwnedHits:hits index:0];
}
_CLLDECDELETE(idTerm);
return result;
} catch ( const CLuceneError &err ) {
@throw [self exceptionForLuceneError:err userInfo:@{@"query" : idValue}];
}
_CLLDECDELETE(idTerm);
return result;
}

- (id<BRSearchResults>)searchWithQuery:(std::auto_ptr<Query>)query
Expand Down Expand Up @@ -909,8 +941,12 @@ - (int)removeObjectsFromIndexMatchingPredicateAndWait:(NSPredicate *)predicate e
sortBy:(NSString *)sortFieldName
sortType:(BRSearchSortType)sortType
ascending:(BOOL)ascending {
std::auto_ptr<Query> query = [self queryForPredicate:predicate analyzer:[self defaultAnalyzer] parent:nil];
return [self searchWithQuery:query sortBy:sortFieldName sortType:sortType ascending:ascending];
try {
std::auto_ptr<Query> query = [self queryForPredicate:predicate analyzer:[self defaultAnalyzer] parent:nil];
return [self searchWithQuery:query sortBy:sortFieldName sortType:sortType ascending:ascending];
} catch ( const CLuceneError &err ) {
@throw [self exceptionForLuceneError:err userInfo:@{@"query" : predicate}];
}
}

@end

0 comments on commit 6efe8f4

Please sign in to comment.