Skip to content

Commit 3a83fe3

Browse files
committed
Update Readme, Add get-models.sh
1 parent 3c5c3d1 commit 3a83fe3

File tree

2 files changed

+139
-1
lines changed

2 files changed

+139
-1
lines changed

README.md

Lines changed: 93 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,95 @@
11
# Typesense Swift
22

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.

get-models.sh

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
curl https://raw.githubusercontent.com/typesense/typesense-api-spec/v0.22.0/openapi.yml > openapi.yml
2+
swagger-codegen generate -i openapi.yml -l swift5 -o output
3+
rm -rf Models
4+
cd output/SwaggerClient/Classes/Swaggers
5+
mv ./Models ../../../../
6+
cd ../../../../
7+
rm -rf output
8+
9+
cd Models
10+
11+
# Delete useless structs generated as a mistake by the code-gen
12+
rm OneOfSearchParametersMaxHits.swift
13+
rm OneOfMultisearchParametersMaxHits.swift
14+
15+
# Fix the maxHits type by defining it as a String Optional
16+
find . -name "SearchParameters.swift" -exec sed -i '' 's/maxHits: OneOfSearchParametersMaxHits\?/maxHits: String\?/g' {} \;
17+
find . -name "MultiSearchParameters.swift" -exec sed -i '' 's/maxHits: OneOfMultiSearchParametersMaxHits\?/maxHits: String\?/g' {} \;
18+
find . -name "MultiSearchCollectionParameters.swift" -exec sed -i '' 's/maxHits: Any\?/maxHits: String\?/g' {} \;
19+
20+
# Add Generics to SearchResult
21+
find . -name "SearchResult.swift" -exec sed -i '' 's/SearchResult:/SearchResult<T: Codable>:/g' {} \;
22+
find . -name "SearchResult.swift" -exec sed -i '' 's/groupedHits: \[SearchGroupedHit\]\?/groupedHits: \[SearchGroupedHit<T>\]\?/g' {} \;
23+
find . -name "SearchResult.swift" -exec sed -i '' 's/hits: \[SearchResultHit\]\?/hits: \[SearchResultHit<T>\]\?/g' {} \;
24+
25+
# Add Generics to MultiSearchResult
26+
find . -name "MultiSearchResult.swift" -exec sed -i '' 's/results: \[SearchResult\]/results: \[SearchResult<T>\]/g' {} \;
27+
find . -name "MultiSearchResult.swift" -exec sed -i '' 's/MultiSearchResult:/MultiSearchResult<T: Codable>:/g' {} \;
28+
29+
# Add Generics to SearchResultHit
30+
find . -name "SearchResultHit.swift" -exec sed -i '' 's/SearchResultHit:/SearchResultHit<T: Codable>:/g' {} \;
31+
32+
# Convert document parameter to a generic T
33+
find . -name "SearchResultHit.swift" -exec sed -i '' 's/document: \[String:Any\]\?/document: T?/g' {} \;
34+
35+
# Add Generics to SearchGroupedHit
36+
find . -name "SearchGroupedHit.swift" -exec sed -i '' 's/SearchGroupedHit:/SearchGroupedHit<T: Codable>:/g' {} \;
37+
find . -name "SearchGroupedHit.swift" -exec sed -i '' 's/hits: \[SearchResultHit\]/hits: \[SearchResultHit<T>\]/g' {} \;
38+
39+
# Convert matchedTokens to custom defined StringQuantum
40+
find . -name "SearchHighlight.swift" -exec sed -i '' 's/matchedTokens: \[Any\]\?/matchedTokens: StringQuantum\?/g' {} \;
41+
42+
43+
44+
45+
46+

0 commit comments

Comments
 (0)