Swiftpack.co - cbess/CBSearchKit as Swift Package

Swiftpack.co is a collection of thousands of indexed Swift packages. Search packages.
See all packages published by cbess.
cbess/CBSearchKit v0.6.0
Simple and flexible full text search for iOS and Mac. Uses the sqlite3 FTS3/4 engine.
⭐️ 5
🕓 2 years ago
.package(url: "https://github.com/cbess/CBSearchKit.git", from: "v0.6.0")

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

GitHub

link
Stars: 5
Last commit: 1 year ago
Advertisement: IndiePitcher.com - Cold Email Software for Startups

Release Notes

Refresher
2 years ago

Swiftpack is being maintained by Petr Pavlik | @ptrpavlik | @swiftpackco | API | Analytics