hawkit / CBSearchKit

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

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

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

- (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<CBSIndexItem> 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<CBSIndexItem> 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 for more examples.

Soli Deo Gloria

About

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

License:MIT License


Languages

Language:Objective-C 85.3%Language:C 9.4%Language:Ruby 2.7%Language:Rich Text Format 1.3%Language:Swift 1.3%