Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 9 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@ import Typesense
Declare the Typesense nodes that are available as `Node` members:

```swift
let node1 = Node(host: "localhost", port: "8108", nodeProtocol: "http")
let node2 = Node(host: "super-awesome.search", port: "8080", nodeProtocol: "https") //and so on
let node1 = Node(url: "http://localhost:8108") // or
let node2 = Node(host: "xxx-1.a1.typesense.net", port: "443", nodeProtocol: "https")
```

Create a configuration and hence a client with the Nodes mentioned:
Expand Down Expand Up @@ -181,12 +181,18 @@ let (data, response) = try await client.stopwords().retrieve()
let (data, response) = try await client.stopword("stopword_set1").retrieve()
```

### Delete a preset
### Delete a stopwords set

```swift
let (data, response) = try await client.stopword("stopword_set1").delete()
```

### Retrieve debug information

```swift
let (data, response) = try await client.operations().getDebug()
```

## Contributing

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:
Expand Down
5 changes: 4 additions & 1 deletion Sources/Typesense/ApiCall.swift
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,10 @@ struct ApiCall {

//Get URL for a node combined with it's end point
func uriFor(endpoint: String, node: Node) -> String {
return "\(node.nodeProtocol)://\(node.host):\(node.port)/\(endpoint)"
if let url = node.url{
return "\(url)/\(endpoint)"
}
return "\(node.nodeProtocol!)://\(node.host!):\(node.port!)/\(endpoint)"
}

//Get the next healthy node from the given nodes
Expand Down
7 changes: 1 addition & 6 deletions Sources/Typesense/Documents.swift
Original file line number Diff line number Diff line change
Expand Up @@ -75,12 +75,7 @@ public struct Documents {
}

if let _prefix = searchParameters._prefix {
var fullString = ""
for item in _prefix {
fullString.append(String(item))
fullString.append(",")
}
searchQueryParams.append(URLQueryItem(name: "prefix", value: String(fullString.dropLast())))
searchQueryParams.append(URLQueryItem(name: "prefix", value: _prefix))
}

if let _infix = searchParameters._infix {
Expand Down
9 changes: 6 additions & 3 deletions Sources/Typesense/Models/CollectionSchema.swift
Original file line number Diff line number Diff line change
Expand Up @@ -17,29 +17,32 @@ public struct CollectionSchema: Codable {
public var fields: [Field]
/** The name of an int32 / float field that determines the order in which the search results are ranked when a sort_by clause is not provided during searching. This field must indicate some kind of popularity. */
public var defaultSortingField: String?
/** List of symbols or special characters to be used for splitting the text into individual words in addition to space and new-line characters. */
/** List of symbols or special characters to be used for splitting the text into individual words in addition to space and new-line characters. */
public var tokenSeparators: [String]?
/** Enables experimental support at a collection level for nested object or object array fields. This field is only available if the Typesense server is version `0.24.0.rcn34` or later. */
public var enableNestedFields: Bool?
/** List of symbols or special characters to be indexed. */
public var symbolsToIndex: [String]?
public var voiceQueryModel: VoiceQueryModelCollectionConfig?

public init(name: String, fields: [Field], defaultSortingField: String? = nil, tokenSeparators: [String]? = nil, enableNestedFields: Bool? = nil, symbolsToIndex: [String]? = nil) {
public init(name: String, fields: [Field], defaultSortingField: String? = nil, tokenSeparators: [String]? = nil, enableNestedFields: Bool? = nil, symbolsToIndex: [String]? = nil, voiceQueryModel: VoiceQueryModelCollectionConfig? = nil) {
self.name = name
self.fields = fields
self.defaultSortingField = defaultSortingField
self.tokenSeparators = tokenSeparators
self.enableNestedFields = enableNestedFields
self.symbolsToIndex = symbolsToIndex
self.voiceQueryModel = voiceQueryModel
}

public enum CodingKeys: String, CodingKey {
public enum CodingKeys: String, CodingKey {
case name
case fields
case defaultSortingField = "default_sorting_field"
case tokenSeparators = "token_separators"
case enableNestedFields = "enable_nested_fields"
case symbolsToIndex = "symbols_to_index"
case voiceQueryModel = "voice_query_model"
}

}
16 changes: 16 additions & 0 deletions Sources/Typesense/Models/DebugRetrieveSchema.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import Foundation



public struct DebugRetrieveSchema: Codable {

public var state: Int
public var version: String

public init(state: Int, version: String) {
self.state = state
self.version = version
}


}
9 changes: 8 additions & 1 deletion Sources/Typesense/Models/Field.swift
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,14 @@ public struct Field: Codable {
public var locale: String?
public var sort: Bool?
public var _infix: Bool?
public var reference: String?
public var numDim: Int?
public var drop: Bool?
/** Whether to store the image on disk. */
public var store: Bool?
public var embed: FieldEmbed?

public init(name: String, type: String, _optional: Bool? = nil, facet: Bool? = nil, index: Bool? = nil, locale: String? = nil, sort: Bool? = nil, _infix: Bool? = nil, numDim: Int? = nil, drop: Bool? = nil, embed: FieldEmbed? = nil) {
public init(name: String, type: String, _optional: Bool? = nil, facet: Bool? = nil, index: Bool? = nil, locale: String? = nil, sort: Bool? = nil, _infix: Bool? = nil, reference: String? = nil, numDim: Int? = nil, drop: Bool? = nil, store: Bool? = nil, embed: FieldEmbed? = nil) {
self.name = name
self.type = type
self._optional = _optional
Expand All @@ -32,8 +35,10 @@ public struct Field: Codable {
self.locale = locale
self.sort = sort
self._infix = _infix
self.reference = reference
self.numDim = numDim
self.drop = drop
self.store = store
self.embed = embed
}

Expand All @@ -46,8 +51,10 @@ public struct Field: Codable {
case locale
case sort
case _infix = "infix"
case reference
case numDim = "num_dim"
case drop
case store
case embed
}

Expand Down
6 changes: 5 additions & 1 deletion Sources/Typesense/Models/MultiSearchParameters.swift
Original file line number Diff line number Diff line change
Expand Up @@ -116,8 +116,10 @@ public struct MultiSearchParameters: Codable {
public var stopwords: String?
/** Comma separated string of nested facet fields whose parent object should be returned in facet response. */
public var facetReturnParent: String?
/** The base64 encoded audio file in 16 khz 16-bit WAV format. */
public var voiceQuery: String?

public init(q: String? = nil, queryBy: String? = nil, queryByWeights: String? = nil, textMatchType: String? = nil, _prefix: String? = nil, _infix: String? = nil, maxExtraPrefix: Int? = nil, maxExtraSuffix: Int? = nil, filterBy: String? = nil, sortBy: String? = nil, facetBy: String? = nil, maxFacetValues: Int? = nil, facetQuery: String? = nil, numTypos: String? = nil, page: Int? = nil, perPage: Int? = nil, limit: Int? = nil, offset: Int? = nil, groupBy: String? = nil, groupLimit: Int? = nil, includeFields: String? = nil, excludeFields: String? = nil, highlightFullFields: String? = nil, highlightAffixNumTokens: Int? = nil, highlightStartTag: String? = nil, highlightEndTag: String? = nil, snippetThreshold: Int? = nil, dropTokensThreshold: Int? = nil, typoTokensThreshold: Int? = nil, pinnedHits: String? = nil, hiddenHits: String? = nil, overrideTags: String? = nil, highlightFields: String? = nil, preSegmentedQuery: Bool? = nil, preset: String? = nil, enableOverrides: Bool? = nil, prioritizeExactMatch: Bool? = nil, prioritizeTokenPosition: Bool? = nil, prioritizeNumMatchingFields: Bool? = nil, enableTyposForNumericalTokens: Bool? = nil, exhaustiveSearch: Bool? = nil, searchCutoffMs: Int? = nil, useCache: Bool? = nil, cacheTtl: Int? = nil, minLen1typo: Int? = nil, minLen2typo: Int? = nil, vectorQuery: String? = nil, remoteEmbeddingTimeoutMs: Int? = nil, remoteEmbeddingNumTries: Int? = nil, facetStrategy: String? = nil, stopwords: String? = nil, facetReturnParent: String? = nil) {
public init(q: String? = nil, queryBy: String? = nil, queryByWeights: String? = nil, textMatchType: String? = nil, _prefix: String? = nil, _infix: String? = nil, maxExtraPrefix: Int? = nil, maxExtraSuffix: Int? = nil, filterBy: String? = nil, sortBy: String? = nil, facetBy: String? = nil, maxFacetValues: Int? = nil, facetQuery: String? = nil, numTypos: String? = nil, page: Int? = nil, perPage: Int? = nil, limit: Int? = nil, offset: Int? = nil, groupBy: String? = nil, groupLimit: Int? = nil, includeFields: String? = nil, excludeFields: String? = nil, highlightFullFields: String? = nil, highlightAffixNumTokens: Int? = nil, highlightStartTag: String? = nil, highlightEndTag: String? = nil, snippetThreshold: Int? = nil, dropTokensThreshold: Int? = nil, typoTokensThreshold: Int? = nil, pinnedHits: String? = nil, hiddenHits: String? = nil, overrideTags: String? = nil, highlightFields: String? = nil, preSegmentedQuery: Bool? = nil, preset: String? = nil, enableOverrides: Bool? = nil, prioritizeExactMatch: Bool? = nil, prioritizeTokenPosition: Bool? = nil, prioritizeNumMatchingFields: Bool? = nil, enableTyposForNumericalTokens: Bool? = nil, exhaustiveSearch: Bool? = nil, searchCutoffMs: Int? = nil, useCache: Bool? = nil, cacheTtl: Int? = nil, minLen1typo: Int? = nil, minLen2typo: Int? = nil, vectorQuery: String? = nil, remoteEmbeddingTimeoutMs: Int? = nil, remoteEmbeddingNumTries: Int? = nil, facetStrategy: String? = nil, stopwords: String? = nil, facetReturnParent: String? = nil, voiceQuery: String? = nil) {
self.q = q
self.queryBy = queryBy
self.queryByWeights = queryByWeights
Expand Down Expand Up @@ -170,6 +172,7 @@ public struct MultiSearchParameters: Codable {
self.facetStrategy = facetStrategy
self.stopwords = stopwords
self.facetReturnParent = facetReturnParent
self.voiceQuery = voiceQuery
}

public enum CodingKeys: String, CodingKey {
Expand Down Expand Up @@ -225,6 +228,7 @@ public struct MultiSearchParameters: Codable {
case facetStrategy = "facet_strategy"
case stopwords
case facetReturnParent = "facet_return_parent"
case voiceQuery = "voice_query"
}

}
6 changes: 5 additions & 1 deletion Sources/Typesense/Models/SearchParameters.swift
Original file line number Diff line number Diff line change
Expand Up @@ -121,8 +121,10 @@ public struct SearchParameters: Codable {
public var stopwords: String?
/** Comma separated string of nested facet fields whose parent object should be returned in facet response. */
public var facetReturnParent: String?
/** The base64 encoded audio file in 16 khz 16-bit WAV format. */
public var voiceQuery: String?

public init(q: String? = nil, queryBy: String? = nil, queryByWeights: String? = nil, textMatchType: String? = nil, _prefix: String? = nil, _infix: String? = nil, maxExtraPrefix: Int? = nil, maxExtraSuffix: Int? = nil, filterBy: String? = nil, sortBy: String? = nil, facetBy: String? = nil, maxFacetValues: Int? = nil, facetQuery: String? = nil, numTypos: String? = nil, page: Int? = nil, perPage: Int? = nil, limit: Int? = nil, offset: Int? = nil, groupBy: String? = nil, groupLimit: Int? = nil, includeFields: String? = nil, excludeFields: String? = nil, highlightFullFields: String? = nil, highlightAffixNumTokens: Int? = nil, highlightStartTag: String? = nil, highlightEndTag: String? = nil, enableHighlightV1: Bool? = nil, snippetThreshold: Int? = nil, dropTokensThreshold: Int? = nil, typoTokensThreshold: Int? = nil, pinnedHits: String? = nil, hiddenHits: String? = nil, overrideTags: String? = nil, highlightFields: String? = nil, splitJoinTokens: String? = nil, preSegmentedQuery: Bool? = nil, preset: String? = nil, enableOverrides: Bool? = nil, prioritizeExactMatch: Bool? = nil, maxCandidates: Int? = nil, prioritizeTokenPosition: Bool? = nil, prioritizeNumMatchingFields: Bool? = nil, enableTyposForNumericalTokens: Bool? = nil, exhaustiveSearch: Bool? = nil, searchCutoffMs: Int? = nil, useCache: Bool? = nil, cacheTtl: Int? = nil, minLen1typo: Int? = nil, minLen2typo: Int? = nil, vectorQuery: String? = nil, remoteEmbeddingTimeoutMs: Int? = nil, remoteEmbeddingNumTries: Int? = nil, facetStrategy: String? = nil, stopwords: String? = nil, facetReturnParent: String? = nil) {
public init(q: String? = nil, queryBy: String? = nil, queryByWeights: String? = nil, textMatchType: String? = nil, _prefix: String? = nil, _infix: String? = nil, maxExtraPrefix: Int? = nil, maxExtraSuffix: Int? = nil, filterBy: String? = nil, sortBy: String? = nil, facetBy: String? = nil, maxFacetValues: Int? = nil, facetQuery: String? = nil, numTypos: String? = nil, page: Int? = nil, perPage: Int? = nil, limit: Int? = nil, offset: Int? = nil, groupBy: String? = nil, groupLimit: Int? = nil, includeFields: String? = nil, excludeFields: String? = nil, highlightFullFields: String? = nil, highlightAffixNumTokens: Int? = nil, highlightStartTag: String? = nil, highlightEndTag: String? = nil, enableHighlightV1: Bool? = nil, snippetThreshold: Int? = nil, dropTokensThreshold: Int? = nil, typoTokensThreshold: Int? = nil, pinnedHits: String? = nil, hiddenHits: String? = nil, overrideTags: String? = nil, highlightFields: String? = nil, splitJoinTokens: String? = nil, preSegmentedQuery: Bool? = nil, preset: String? = nil, enableOverrides: Bool? = nil, prioritizeExactMatch: Bool? = nil, maxCandidates: Int? = nil, prioritizeTokenPosition: Bool? = nil, prioritizeNumMatchingFields: Bool? = nil, enableTyposForNumericalTokens: Bool? = nil, exhaustiveSearch: Bool? = nil, searchCutoffMs: Int? = nil, useCache: Bool? = nil, cacheTtl: Int? = nil, minLen1typo: Int? = nil, minLen2typo: Int? = nil, vectorQuery: String? = nil, remoteEmbeddingTimeoutMs: Int? = nil, remoteEmbeddingNumTries: Int? = nil, facetStrategy: String? = nil, stopwords: String? = nil, facetReturnParent: String? = nil, voiceQuery: String? = nil) {
self.q = q
self.queryBy = queryBy
self.queryByWeights = queryByWeights
Expand Down Expand Up @@ -178,6 +180,7 @@ public struct SearchParameters: Codable {
self.facetStrategy = facetStrategy
self.stopwords = stopwords
self.facetReturnParent = facetReturnParent
self.voiceQuery = voiceQuery
}

public enum CodingKeys: String, CodingKey {
Expand Down Expand Up @@ -236,6 +239,7 @@ public struct SearchParameters: Codable {
case facetStrategy = "facet_strategy"
case stopwords
case facetReturnParent = "facet_return_parent"
case voiceQuery = "voice_query"
}

}
7 changes: 5 additions & 2 deletions Sources/Typesense/Models/SearchResultRequestParams.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,17 +14,20 @@ public struct SearchResultRequestParams: Codable {
public var collectionName: String
public var q: String
public var perPage: Int
public var voiceQuery: SearchResultRequestParamsVoiceQuery?

public init(collectionName: String, q: String, perPage: Int) {
public init(collectionName: String, q: String, perPage: Int, voiceQuery: SearchResultRequestParamsVoiceQuery? = nil) {
self.collectionName = collectionName
self.q = q
self.perPage = perPage
self.voiceQuery = voiceQuery
}

public enum CodingKeys: String, CodingKey {
public enum CodingKeys: String, CodingKey {
case collectionName = "collection_name"
case q
case perPage = "per_page"
case voiceQuery = "voice_query"
}

}
24 changes: 24 additions & 0 deletions Sources/Typesense/Models/SearchResultRequestParamsVoiceQuery.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
//
// SearchResultRequestParamsVoiceQuery.swift
//
// Generated by swagger-codegen
// https://github.com/swagger-api/swagger-codegen
//

import Foundation



public struct SearchResultRequestParamsVoiceQuery: Codable {

public var transcribedQuery: String?

public init(transcribedQuery: String? = nil) {
self.transcribedQuery = transcribedQuery
}

public enum CodingKeys: String, CodingKey {
case transcribedQuery = "transcribed_query"
}

}
25 changes: 25 additions & 0 deletions Sources/Typesense/Models/VoiceQueryModelCollectionConfig.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
//
// VoiceQueryModelCollectionConfig.swift
//
// Generated by swagger-codegen
// https://github.com/swagger-api/swagger-codegen
//

import Foundation


/** Configuration for the voice query model */

public struct VoiceQueryModelCollectionConfig: Codable {

public var modelName: String?

public init(modelName: String? = nil) {
self.modelName = modelName
}

public enum CodingKeys: String, CodingKey {
case modelName = "model_name"
}

}
8 changes: 1 addition & 7 deletions Sources/Typesense/MultiSearch.swift
Original file line number Diff line number Diff line change
Expand Up @@ -31,13 +31,7 @@ public struct MultiSearch {
}

if let _prefix = commonParameters._prefix {
var fullString = ""
for item in _prefix {
fullString.append(String(item))
fullString.append(",")
}

searchQueryParams.append(URLQueryItem(name: "prefix", value: String(fullString.dropLast())))
searchQueryParams.append(URLQueryItem(name: "prefix", value: _prefix))
}

if let _infix = commonParameters._infix {
Expand Down
24 changes: 16 additions & 8 deletions Sources/Typesense/Node.swift
Original file line number Diff line number Diff line change
@@ -1,20 +1,28 @@
public struct Node: CustomStringConvertible {
var host: String
var port: String
var nodeProtocol: String
var host: String?
var port: String?
var nodeProtocol: String?
var url: String?
var isHealthy: Bool = false
var lastAccessTimeStamp: Int64 = 0

public init(host: String, port: String, nodeProtocol: String) {

public init(host: String? = nil, port: String? = nil, nodeProtocol: String? = nil, url: String? = nil) {
if url == nil && (host == nil || port == nil || nodeProtocol == nil) {
fatalError("Node `url` or `nodeProtocol` and `host` and `port` must be set!")
}
self.host = host
self.port = port
self.nodeProtocol = nodeProtocol
self.url = url
}

public var description: String {
return "Node: \(nodeProtocol)://\(host):\(port)"
if let url = self.url {
return "Node: \(url)"
}
return "Node: \(nodeProtocol!)://\(host!):\(port!)"
}

public var healthStatus: String {
return isHealthy ? "Healthy" : "Unhealthy"
}
Expand Down
9 changes: 9 additions & 0 deletions Sources/Typesense/Operations.swift
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,15 @@ public struct Operations {
return (data, response)
}

public func getDebug() async throws -> (DebugRetrieveSchema?, URLResponse?) {
let (data, response) = try await apiCall.get(endPoint: "debug")
if let result = data {
let decodedData = try decoder.decode(DebugRetrieveSchema.self, from: result)
return (decodedData, response)
}
return (nil, response)
}

public func vote() async throws -> (SuccessStatus?, URLResponse?) {
let (data, response) = try await apiCall.post(endPoint: "\(RESOURCEPATH)/vote", body: Data())
if let result = data {
Expand Down
Loading