Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/cbess/cbsearchkit

Simple and flexible full text search for iOS and Mac. Uses the sqlite3 FTS3/4 engine.
https://github.com/cbess/cbsearchkit

fts objective-c search sqlite3-fts3

Last synced: about 1 month ago
JSON representation

Simple and flexible full text search for iOS and Mac. Uses the sqlite3 FTS3/4 engine.

Awesome Lists containing this project

README

        

CBSearchKit
===========

Simple and flexible full text search for iOS and Mac. Using the sqlite3 FTS3/4 engine.

## Setup

- Link `libsqlite3.dylib`
- Add `pod 'CBSearchKit', :git => 'https://github.com/cbess/CBSearchKit.git', :tag => 'v0.6.0'` to `Podfile`
- Run `pod update`

## Example Usage

```objc
- (NSArray *)buildIndex {
// create in-memory index
self.indexer = [[CBSIndexer alloc] initWithDatabaseNamed:nil];

CBSIndexDocument *document = [CBSIndexDocument new];
document.indexItemIdentifier = @"one-id";
document.indexTextContents = @"this is one";
document.indexMeta = @{@"idx": @1, @"name": @"one"};

CBSIndexDocument *document2 = [CBSIndexDocument new];
document2.indexTextContents = @"this is two";
document2.indexMeta = @{@"idx": @2, @"name": @"two"};

CBSIndexDocument *document3 = [CBSIndexDocument new];
document3.indexTextContents = @"this is three";
document3.indexMeta = @{@"idx": @3, @"test": @"three", @"name": @"three"};

NSArray *documents = @[document, document2, document3];

XCTestExpectation *expectation = [self expectationWithDescription:@"build index"];
[self.indexer addItems:documents completionHandler:^(NSArray *indexItems, NSError *error) {
XCTAssertNil(error, @"unable to build the index");
XCTAssertEqual(indexItems.count, documents.count, @"wrong count indexed");

[expectation fulfill];
}];

[self waitForExpectationsWithTimeout:3 handler:nil];

return documents;
}

- (void)testSearch {
NSArray *indexedDocuments = [self buildIndex];

XCTAssertEqual([self.indexer itemCount], indexedDocuments.count, @"Bad count");

id oneDoc = indexedDocuments.firstObject;
static NSString * const searchText = @"one";

XCTestExpectation *expectation = [self expectationWithDescription:@"search index"];

CBSSearcher *searcher = [[CBSSearcher alloc] initWithIndexer:self.indexer];
searcher.orderType = CBSSearcherOrderTypeRelevance;
[searcher itemsWithText:searchText itemType:CBSIndexItemTypeIgnore completionHandler:^(NSArray *items, NSError *error) {
XCTAssertEqual(items.count, 1, @"Should be only one item");
XCTAssertNil(error, @"Error: %@", error);

id item = items.lastObject;
NSDictionary *meta = [item indexMeta];

XCTAssertNotNil(meta, @"No meta");
XCTAssertEqualObjects(meta[@"idx"], [oneDoc indexMeta][@"idx"], @"Wrong meta value");
XCTAssertEqualObjects([item indexItemIdentifier], [oneDoc indexItemIdentifier], @"Wrong index identifier");

[expectation fulfill];
}];

[self waitForExpectationsWithTimeout:7 handler:nil];
}
```

See [unit tests](CBSearchKitTests) for more examples.

[Soli Deo Gloria](http://perfectGod.com)