|
1 | 1 | # Typesense Swift |
2 | 2 |
|
3 | | -A great new way to implement your searches on iOS using Typesense ⚡️🔍✨ Typesense Swift is a high level wrapper that helps you easily implement searching using Typesense. |
| 3 | +A great new way to implement your searches on iOS using [Typesense](https://github.com/typesense/typesense) ⚡️🔍✨ Typesense Swift is a high level wrapper that helps you easily implement searching using Typesense. |
| 4 | + |
| 5 | +## Installation |
| 6 | + |
| 7 | +Add `Typesense Swift` Swift Package to your project. You can refer [Apple's Documentation](https://developer.apple.com/documentation/swift_packages/adding_package_dependencies_to_your_app) to add Typesense Swift as a dependency to your iOS Project. You can also import Typesense into your own Swift Package by adding this line to dependencies array of `Package.swift`: |
| 8 | + |
| 9 | +```swift |
| 10 | + ... |
| 11 | + dependencies: [ |
| 12 | + .package(url: "https://github.com/typesense/typesense-swift", .upToNextMajor(from: "0.1.0"), |
| 13 | + ], |
| 14 | + ... |
| 15 | +``` |
| 16 | + |
| 17 | +## Usage |
| 18 | + |
| 19 | +### Setting up the client |
| 20 | + |
| 21 | +Import Typesense onto your Swift Project: |
| 22 | + |
| 23 | +```swift |
| 24 | + import Typesense |
| 25 | +``` |
| 26 | + |
| 27 | +Declare the Typesense nodes that are available as `Node` members: |
| 28 | + |
| 29 | +```swift |
| 30 | + let node1 = Node(host: "localhost", port: "8108", nodeProtocol: "http") |
| 31 | + let node2 = Node(host: "super-awesome.search", port: "8080", nodeProtocol: "https") //and so on |
| 32 | +``` |
| 33 | + |
| 34 | +Create a configuration and hence a client with the Nodes mentioned: |
| 35 | + |
| 36 | +```swift |
| 37 | + let myConfig = Configuration(nodes: [node1, node2], apiKey: "coolstuff") |
| 38 | + |
| 39 | + let client = Client(config: myConfig) |
| 40 | +``` |
| 41 | +You can use Typesense parameters like `nearestNode` and `connectionTimeoutSeconds` while creating the configuration. You can also pass in a `logger` parameter to debug the code like this: |
| 42 | + |
| 43 | +```swift |
| 44 | + let myConfig = Configuration(nodes: [node1, node2], apiKey: "coolstuff", logger: Logger(debugMode: true)) |
| 45 | +``` |
| 46 | + |
| 47 | +### Indexing documents |
| 48 | + |
| 49 | +You can create a collection by first defining a collection schema: |
| 50 | + |
| 51 | +```swift |
| 52 | + let myCoolSchema = CollectionSchema(name: "schools", fields: [Field(name: "school_name", type: "string"), Field(name: "num_students", type: "int32"), Field(name: "country", type: "string", facet: true)], defaultSortingField: "num_students") |
| 53 | + |
| 54 | + let (data, response) = try await client.collections.create(schema: myCoolSchema) |
| 55 | +``` |
| 56 | + |
| 57 | +Define the structure of your document as per your collection, and index it by inserting/upserting it to the collection: |
| 58 | + |
| 59 | +```swift |
| 60 | + struct School: Codable { |
| 61 | + var id: String |
| 62 | + var school_name: String |
| 63 | + var num_students: Int |
| 64 | + var country: String |
| 65 | + } |
| 66 | + |
| 67 | + let document = School(id: "7", school_name: "Hogwarts", num_students: 600, country: "United Kingdom") |
| 68 | + let documentData = try JSONEncoder().encode(document) |
| 69 | + let (data, response) = try await client.collection(name: "schools").documents().create(document: documentData) |
| 70 | + //or |
| 71 | + let (data, response) = try await client.collection(name: "schools").documents().upsert(document: documentData) |
| 72 | + |
| 73 | +``` |
| 74 | +You can perform CRUD actions to `Collections` and `Documents` that belong to a certain collection. You can also use `.importBatch()` on the `documents()` method to import and index a batch of documents (in .jsonl format). |
| 75 | + |
| 76 | +### Searching |
| 77 | + |
| 78 | +Define your [search parameters](https://typesense.org/docs/0.22.1/api/documents.html#arguments) clearly and then perform the search operation by mentioning your Document Type: |
| 79 | + |
| 80 | +```swift |
| 81 | + let searchParameters = SearchParameters(q: "hog", queryBy: "school_name", filterBy: "num_students:>500", sortBy: "num_students:desc") |
| 82 | + |
| 83 | + let (data, response) = try await client.collection(name: "schools").documents().search(searchParameters, for: School.self) |
| 84 | +``` |
| 85 | +This returns a `SearchResult` object as the data, which can be further parsed as desired. |
| 86 | + |
| 87 | +## Contributing |
| 88 | + |
| 89 | +Issues and pull requests are welcome on GitHub at [Typesense Swift](https://github.com/typesense/typesense-swift). Do note that the Models used in the Swift client are generated by [Swagger-Codegen](https://github.com/swagger-api/swagger-codegen) and are automated to be modified in order to prevent major errors. So please do use the shell script that is provided in the repo to generate the models: |
| 90 | + |
| 91 | +```shell |
| 92 | + sh get-models.sh |
| 93 | +``` |
| 94 | + |
| 95 | +The generated Models (inside the Models directory) are to be used inside the Models directory of the source code as well. Models need to be generated as and when the [Typesense-Api-Spec](https://github.com/typesense/typesense-api-spec) is updated. |
0 commit comments